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('差评'),为我给这段代码增加一个输出判断结果准确率的功能
时间: 2024-01-22 22:17:35 浏览: 110
feat_extr.rar_.ana_extr_extraction_feature extraction_feature_ex
你可以将原始数据分为训练集和测试集,使用训练集训练模型,使用测试集测试模型,并计算准确率。例如,可以使用sklearn中的train_test_split函数将数据分为训练集和测试集,然后使用准确率作为评估指标。下面是修改后的代码:
```python
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
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'))
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))
# 预测新数据的类别
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('差评')
```
其中,`train_test_split`函数中的参数`test_size`表示测试集占总数据集的比例。在这个例子中,设置为0.2表示将20%的数据作为测试集,80%的数据作为训练集。
阅读全文