问题描述
我有一个评论数据集,其类别标签为正面/负面.我正在将朴素贝叶斯应用于该评论数据集.首先,我正在转换成词袋.这里sorted_data['Text']是评论,final_counts是一个稀疏矩阵
I have a dataset of reviews which has a class label of positive/negative. I am applying Naive Bayes to that reviews dataset. Firstly, I am converting into Bag of words. Here sorted_data['Text'] is reviews and final_counts is a sparse matrix
count_vect = CountVectorizer()
final_counts = count_vect.fit_transform(sorted_data['Text'].values)
我正在将数据拆分为训练数据集和测试数据集.
I am splitting the data into train and test dataset.
X_1, X_test, y_1, y_test = cross_validation.train_test_split(final_counts, labels, test_size=0.3, random_state=0)
我应用朴素贝叶斯算法如下
I am applying the naive bayes algorithm as follows
optimal_alpha = 1
NB_optimal = BernoulliNB(alpha=optimal_aplha)
# fitting the model
NB_optimal.fit(X_tr, y_tr)
# predict the response
pred = NB_optimal.predict(X_test)
# evaluate accuracy
acc = accuracy_score(y_test, pred) * 100
print('
The accuracy of the NB classifier for k = %d is %f%%' % (optimal_aplha, acc))
这里的 X_test 是测试数据集,其中 pred 变量告诉我们 X_test 中的向量是正类还是负类.
Here X_test is test dataset in which pred variable gives us whether the vector in X_test is positive or negative class.
X_test 形状为(54626 行,82343 维)
pred 的长度是 54626
我的问题是我想获得每个向量中概率最高的单词,以便我可以通过单词了解为什么它预测为正类或负类.因此,如何得到每个向量中概率最高的词?
My question is I want to get the words with highest probability in each vector so that I can get to know by the words that why it predicted as positive or negative class. Therefore, how to get the words which have highest probability in each vector?
推荐答案
您可以使用 coefs_
或 feature_log_prob_
从拟合模型中获取每个单词的重要性属性.例如
You can get the important of each word out of the fit model by using the coefs_
or feature_log_prob_
attributes. For example
neg_class_prob_sorted = NB_optimal.feature_log_prob_[0, :].argsort()[::-1]
pos_class_prob_sorted = NB_optimal.feature_log_prob_[1, :].argsort()[::-1]
print(np.take(count_vect.get_feature_names(), neg_class_prob_sorted[:10]))
print(np.take(count_vect.get_feature_names(), pos_class_prob_sorted[:10]))
为您的每个班级打印前 10 个最具预测性的单词.
Prints the top 10 most predictive words for each of your classes.
这篇关于如何在朴素贝叶斯中获得特征重要性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!