from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer from sklearn.naive_bayes import MultinomialNB import jieba good_comments = [] bad_comments = [] with open('D:\PyCharmProjects\爬虫测试\好评.txt', 'r', encoding='gbk') as f: for line in f.readlines(): good_comments.append(line.strip('\n')) # print(good_comments) with open('D:\PyCharmProjects\爬虫测试\差评.txt', 'r', encoding='gbk') as f: for line in f.readlines(): bad_comments.append(line.strip('\n')) # print(bad_comments) # stopwords = [] with open('StopWords.txt', 'r', encoding='utf-8') as f: stopwords = f.read().splitlines() good_words = [] for line in good_comments: words = jieba.cut(line, cut_all=False) words = [w for w in words if w not in stopwords] good_words.append(' '.join(words)) bad_words = [] for line in bad_comments: words = jieba.cut(line, cut_all=False) words = [w for w in words if w not in stopwords] bad_words.append(' '.join(words)) # 将文本转换为向量 vectorizer = CountVectorizer() X = vectorizer.fit_transform(good_words + bad_words) y = [1] * len(good_words) + [0] * len(bad_words) # 训练模型 clf = MultinomialNB() clf.fit(X, y) # 测试模型 test_text = '味道很差,不建议购买,还有不少烂的!' test_words = ' '.join(jieba.cut(test_text, cut_all=False)) test_vec = vectorizer.transform([test_words]) pred = clf.predict(test_vec) if pred[0] == 1: print('好评') else: print('差评'),为我给这段代码增加一个输出准确率的功能
时间: 2023-12-23 14:06:31 浏览: 167
可以交叉验证来计算模型的准确率。具体操作如下:
```
from sklearn.model_selection import cross_val_score
scores = cross_val_score(clf, X, y, cv=5)
print('准确率:', scores.mean())
```
其中 `cross_val_score` 的参数说明:
- `clf`:分类器
- `X`:特征矩阵
- `y`:标签向量
- `cv`:交叉验证的次数
输出的结果是交叉验证得到的准确率的平均值。
相关问题
import pandas as pd import matplotlib import numpy as np import matplotlib.pyplot as plt import jieba as jb import re from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.feature_selection import chi2 import numpy as np from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extraction.text import TfidfTransformer from sklearn.naive_bayes import MultinomialNB def sigmoid(x): return 1 / (1 + np.exp(-x)) import numpy as np #定义删除除字母,数字,汉字以外的所有符号的函数 def remove_punctuation(line): line = str(line) if line.strip()=='': return '' rule = re.compile(u"[^a-zA-Z0-9\u4E00-\u9FA5]") line = rule.sub('',line) return line def stopwordslist(filepath): stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()] return stopwords df = pd.read_csv('./online_shopping_10_cats/online_shopping_10_cats.csv') df=df[['cat','review']] df = df[pd.notnull(df['review'])] d = {'cat':df['cat'].value_counts().index, 'count': df['cat'].value_counts()} df_cat = pd.DataFrame(data=d).reset_index(drop=True) df['cat_id'] = df['cat'].factorize()[0] cat_id_df = df[['cat', 'cat_id']].drop_duplicates().sort_values('cat_id').reset_index(drop=True) cat_to_id = dict(cat_id_df.values) id_to_cat = dict(cat_id_df[['cat_id', 'cat']].values) #加载停用词 stopwords = stopwordslist("./online_shopping_10_cats/chineseStopWords.txt") #删除除字母,数字,汉字以外的所有符号 df['clean_review'] = df['review'].apply(remove_punctuation) #分词,并过滤停用词 df['cut_review'] = df['clean_review'].apply(lambda x: " ".join([w for w in list(jb.cut(x)) if w not in stopwords])) tfidf = TfidfVectorizer(norm='l2', ngram_range=(1, 2)) features = tfidf.fit_transform(df.cut_review) labels = df.cat_id X_train, X_test, y_train, y_test = train_test_split(df['cut_review'], df['cat_id'], random_state = 0) count_vect = CountVectorizer() X_train_counts = count_vect.fit_transform(X_train) tfidf_transformer = TfidfTransformer() X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts) 已经写好以上代码,请补全train和test函数
以下是train和test函数的代码:
```python
def train(X_train_tfidf, y_train):
clf = MultinomialNB().fit(X_train_tfidf, y_train)
return clf
def test(clf, X_test):
X_test_counts = count_vect.transform(X_test)
X_test_tfidf = tfidf_transformer.transform(X_test_counts)
y_pred = clf.predict(X_test_tfidf)
return y_pred
```
train函数使用MultinomialNB()方法来拟合特征矩阵和标签,返回训练后的分类器clf。
test函数使用训练好的分类器clf来预测X_test的标签,返回预测结果y_pred。在预测之前,需要使用之前定义的count_vect和tfidf_transformer对X_test进行向量化和tf-idf转换。
给以下这段代码加上结果可视化的功能:from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB import jieba from sklearn.model_selection import train_test_split import numpy as np import matplotlib.pyplot as plt good_comments = [] bad_comments = [] with open('D:\PyCharmProjects\爬虫测试\好评.txt', 'r', encoding='gbk') as f: for line in f.readlines(): good_comments.append(line.strip('\n')) with open('D:\PyCharmProjects\爬虫测试\差评.txt', 'r', encoding='gbk') as f: for line in f.readlines(): bad_comments.append(line.strip('\n')) with open('StopWords.txt', 'r', encoding='utf-8') as f: stopwords = f.read().splitlines() good_words = [] for line in good_comments: words = jieba.cut(line, cut_all=False) words = [w for w in words if w not in stopwords] good_words.append(' '.join(words)) bad_words = [] for line in bad_comments: words = jieba.cut(line, cut_all=False) words = [w for w in words if w not in stopwords] bad_words.append(' '.join(words)) # 将文本转换为向量 vectorizer = CountVectorizer() X = vectorizer.fit_transform(good_words + bad_words) y = [1] * len(good_words) + [0] * len(bad_words) # 将数据分为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) # 训练模型 clf = MultinomialNB() clf.fit(X_train, y_train) # 测试模型并计算准确率 pred = clf.predict(X_test) accuracy = sum(pred == y_test) / len(y_test) print('准确率:{:.2%}'.format(accuracy)) # 预测新数据的类别 with open('测试评论.txt', 'r', encoding='utf-8') as f: count = 0 for line in f.readlines(): count += 1 test_text = line.strip('\n') test_words = ' '.join(jieba.cut(test_text, cut_all=False)) test_vec = vectorizer.transform([test_words]) pred = clf.predict(test_vec) if pred[0] == 1: print(count, '好评') else: print(count, '差评')
以下是加上结果可视化的代码:
```
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import jieba
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt
good_comments = []
bad_comments = []
# 读取好评和差评文本
with open('D:\PyCharmProjects\爬虫测试\好评.txt', 'r', encoding='gbk') as f:
for line in f.readlines():
good_comments.append(line.strip('\n'))
with open('D:\PyCharmProjects\爬虫测试\差评.txt', 'r', encoding='gbk') as f:
for line in f.readlines():
bad_comments.append(line.strip('\n'))
with open('StopWords.txt', 'r', encoding='utf-8') as f:
stopwords = f.read().splitlines()
# 对好评和差评文本进行分词和去除停用词
good_words = []
for line in good_comments:
words = jieba.cut(line, cut_all=False)
words = [w for w in words if w not in stopwords]
good_words.append(' '.join(words))
bad_words = []
for line in bad_comments:
words = jieba.cut(line, cut_all=False)
words = [w for w in words if w not in stopwords]
bad_words.append(' '.join(words))
# 将文本转换为向量
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(good_words + bad_words)
y = [1] * len(good_words) + [0] * len(bad_words)
# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 训练模型
clf = MultinomialNB()
clf.fit(X_train, y_train)
# 测试模型并计算准确率
pred = clf.predict(X_test)
accuracy = sum(pred == y_test) / len(y_test)
print('准确率:{:.2%}'.format(accuracy))
# 可视化结果
fig, ax = plt.subplots()
ax.bar(['差评', '好评'], [len(bad_comments), len(good_comments)], color=['red', 'green'])
ax.set_xlabel('评论类型')
ax.set_ylabel('评论数量')
ax.set_title('评论数量统计')
plt.show()
# 预测新数据的类别
with open('测试评论.txt', 'r', encoding='utf-8') as f:
count = 0
for line in f.readlines():
count += 1
test_text = line.strip('\n')
test_words = ' '.join(jieba.cut(test_text, cut_all=False))
test_vec = vectorizer.transform([test_words])
pred = clf.predict(test_vec)
if pred[0] == 1:
print(count, '好评')
else:
print(count, '差评')
```
运行以上代码后,会得到一个柱状图,用于展示好评和差评的数量统计。同时,在预测新数据的类别时,也会在控制台输出每一条评论的分类结果。
阅读全文