添加功能到Multinomial朴素贝叶斯分类器 - Python

时间:2017-08-09 07:07:43

标签: python python-3.x dictionary machine-learning scikit-learn

使用Scikit的MultinomialNB()在Python中学习,我想要的不仅是文档中的单词特征,还有情感词典(意思是单词列表而不是Python数据类型)。

假设这些是要训练的文件

train_data = ['i hate who you welcome for','i adore him with all my heart','i can not forget his warmest welcome for me','please forget all these things! this house smells really weird','his experience helps a lot to complete all these tedious things', 'just ok', 'nothing+special today']

train_labels = ['Nega','Posi','Posi','Nega','Posi','Other','Other']

psentidict = ['welcome','adore','helps','complete','fantastic']
nsentidict = ['hate','weird','tedious','forget','abhor']
osentidict = ['ok','nothing+special']

我可以训练下面的列表

from sklearn import naive_bayes
from sklearn.pipeline import Pipeline

text_clf = Pipeline([('vect', CountVectorizer()), 
                     ('clf', naive_bayes.MultinomialNB(alpha = 1.0)),]) 

text_clf = text_clf.fit(train_data, train_labels)

即使我根据相应的标签计算所有令牌来训练数据,我也想使用我的情感词典作为额外的分类功能。

这是因为通过字典训练的特征,可以预测OOV(词汇表外)。只有笨拙的拉普拉斯平滑(alpha = 1.0),整体精确度才会受到严重限制。

test_data = 'it is fantastic'
predicted_labels = text_clf.predict(test_data)

添加词典功能后,虽然每个令牌都不在训练文档中,但可以预测上面的句子。

如何将psentidictnsentidictosentidict的功能添加到Multinomial Naive Bayes分类器? (像文件一样训练它们可能会扭曲测量值,因此我认为最好找到另一种方法)

1 个答案:

答案 0 :(得分:1)

我相信没有其他方法可以包含您的Multinomial Naive Bayes模型的功能。这只是因为您想要将某种标签与特征相关联(例如,对于psentidict中的值等,为正面'等等)。这只能通过使用所述功能和标签对您的模型进行训练来实现。你可以做的是,改进模型,通过创建具有所述特征的句子,而不是直接使用单词,例如,对于单词“恨”,你可以改为使用'我全心全意地恨你#39;并将情绪添加为“否定”,而不仅仅是使用“厌恶”和“消极”这对“#”;'因此,您已为数据集创建了更多此类示例。

希望this link有所帮助。

相关问题