TextBlob实现文本分类
- 一个使用TextBlob进行Naive Bayes classifier
- 参考:https://textblob.readthedocs.io/en/dev/classifiers.html#classifiers
1.准备数据集:训练集和测试集
train = [
... ('I love this sandwich.', 'pos'),
... ('this is an amazing place!', 'pos'),
... ('I feel very good about these beers.', 'pos'),
... ('this is my best work.', 'pos'),
... ("what an awesome view", 'pos'),
... ('I do not like this restaurant', 'neg'),
... ('I am tired of this stuff.', 'neg'),
... ("I can't deal with this", 'neg'),
... ('he is my sworn enemy!', 'neg'),
... ('my boss is horrible.', 'neg')
... ]
test = [
... ('the beer was good.', 'pos'),
... ('I do not enjoy my job', 'neg'),
... ("I ain't feeling dandy today.", 'neg'),
... ("I feel amazing!", 'pos'),
... ('Gary is a friend of mine.', 'pos'),
... ("I can't believe I'm doing this.", 'neg')
... ]
2.创建朴素贝叶斯分类器
from textblob.classifiers import NaiveBayesClassifier
3.把训练丢进去训练
nb_model = NaiveBayesClassifier(train)
4.预测新来的样本
dev_sen = "This is an amazing library!"
print(nb_model.classify(dev_sen))
pos
也可以计算属于某一类的概率
dev_sen_prob = nb_model.prob_classify(dev_sen)
print(dev_sen_prob.prob("pos"))
0.980117820324005
5.计算模型在测试集上的精确度
print(nb_model.accuracy(test))
0.8333333333333334
欢迎关注【AI小白入门】,这里分享Python、机器学习、深度学习、自然语言处理、人工智能等技术,关注前沿技术,求职经验等,陪有梦想的你一起成长。