import osimport jiebaimport numpy as npfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import accuracy_score# 定义常量data_dir = './data'stopwords_path = './stopwords.txt'category_names = ['文学', '教育', '计算机', '医学', '体育']# 加载停用词stopwords = set()with open(stopwords_path, 'r', encoding='utf-8') as f: for line in f: stopwords.add(line.strip())# 加载语料库,构建训练集和测试集train_data = []train_labels = []test_data = []test_labels = []for i, category_name in enumerate(category_names): category_dir = os.path.join(data_dir, category_name) file_names = os.listdir(category_dir) for j, file_name in enumerate(file_names): with open(os.path.join(category_dir, file_name), 'r', encoding='utf-8') as f: content = f.read() words = [word for word in jieba.cut(content) if word not in stopwords] if j < 3: test_data.append(words) test_labels.append(i) else: train_data.append(words) train_labels.append(i)# 构建词汇表vocab = set()for document in train_data: vocab |= set(document)vocab = list(vocab)vocab.sort()# 构建文档向量def document2vector(document, vocab): vector = np.zeros(len(vocab)) for word in document: if word in vocab: vector[vocab.index(word)] += 1 return vectortrain_vectors = np.array([document2vector(document, vocab) for document in train_data])test_vectors = np.array([document2vector(document, vocab) for document in test_data])# 训练朴素贝叶斯分类器clf = MultinomialNB()clf.fit(train_vectors, train_labels)# 测试分类器predicted_labels = clf.predict(test_vectors)# 评估分类器accuracy = accuracy_score(test_labels, predicted_labels)print('Accuracy:', accuracy)
时间: 2023-06-16 18:02:06 浏览: 157
这段代码实现了一个基于朴素贝叶斯分类器的文本分类器,主要分为以下几个步骤:
1. 加载停用词文件,并存储在一个集合中。
2. 加载语料库,并将每个文档进行分词,去除停用词,并将前三个文档作为测试集,其余的作为训练集。
3. 构建词汇表,将训练集中的所有词语去重后按字典序排序后存储在一个列表中。
4. 将每个文档转化为词向量,其长度为词汇表的长度,每个位置表示对应词汇在该文档中出现的次数。
5. 使用训练集训练一个朴素贝叶斯分类器,并使用测试集测试该分类器的准确率。
需要注意的是,该代码中使用的是多项式朴素贝叶斯分类器(MultinomialNB),因此假设每个词汇的出现次数是服从多项式分布的。同时,该代码中使用的是中文分词工具jieba,因此需要确保该工具已被正确安装。
相关问题
Visual Studio Code能否import pandas as pdimport numpy as npfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfilename = 'D:/111/20news-18828.tar/20newsgroups.srv'with open(filename, 'r', encoding='UTF-8') as file: A = file.readlines()data = []for line in A: category, text = line.split('\t') data.append((category, text))df = pd.DataFrame(data, columns=['category', 'text'])# 分割数据集为训练集和测试集X_train, X_test, y_train, y_test = train_test_split(df['text'], df['category'], random_state=42)# 对文本进行特征提取tfidf = TfidfVectorizer(stop_words='english')X_train_tfidf = tfidf.fit_transform(X_train.astype(str))X_test_tfidf = tfidf.transform(X_test.astype(str))# 使用多项式朴素贝叶斯模型进行训练和预测clf = MultinomialNB()clf.fit(X_train_tfidf, y_train)y_pred = clf.predict(X_test_tfidf)# 输出模型的准确率print('Accuracy:', accuracy_score(y_test, y_pred))显示结果
Visual Studio Code可以import pandas as pd、import numpy as np、from sklearn.feature_extraction.text import TfidfVectorizer、from sklearn.naive_bayes import MultinomialNB、from sklearn.metrics import accuracy_score、from sklearn.model_selection import train_test_split等库,并运行你提供的代码。如果你有任何问题或者错误信息,请提供相关信息以便更好地帮助你。
import pandas as pdimport numpy as npfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.metrics import accuracy_scorefrom sklearn.model_selection import train_test_splitfilename = 'D:/111/20news-18828.tar/20newsgroups.srv'with open(filename, 'r', encoding='UTF-8') as file: A = file.readlines()data = []for line in A: category, text = line.split('\t') data.append((category, text))df = pd.DataFrame(data, columns=['category', 'text'])# 分割数据集为训练集和测试集X_train, X_test, y_train, y_test = train_test_split(df['text'], df['category'], random_state=42)# 对文本进行特征提取tfidf = TfidfVectorizer(stop_words='english')X_train_tfidf = tfidf.fit_transform(X_train.astype(str))X_test_tfidf = tfidf.transform(X_test.astype(str))# 使用多项式朴素贝叶斯模型进行训练和预测clf = MultinomialNB()clf.fit(X_train_tfidf, y_train)y_pred = clf.predict(X_test_tfidf)# 输出模型的准确率print('Accuracy:', accuracy_score(y_test, y_pred))在终端上指出“conda”无法显示结果
这是因为代码中没有使用print()函数来输出结果。你可以在代码末尾添加以下代码:print('Accuracy:', accuracy_score(y_test, y_pred)),这样就可以在终端上看到模型的准确率了。另外,如果你使用的是Jupyter Notebook等交互式环境,可以直接运行代码块来查看输出结果。
阅读全文