如何在naive_bayes MultinomialNB 中计算feature_log_prob_

How to calculate feature_log_prob_ in the naive_bayes MultinomialNB

这是我的代码:

# Load libraries
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
# Create text
text_data = np.array(['Tim is smart!',
                      'Joy is the best',
                      'Lisa is dumb',
                      'Fred is lazy',
                      'Lisa is lazy'])
# Create target vector
y = np.array([1,1,0,0,0])
# Create bag of words
count = CountVectorizer()
bag_of_words = count.fit_transform(text_data)    # 

# Create feature matrix
X = bag_of_words.toarray()

mnb = MultinomialNB(alpha = 1, fit_prior = True, class_prior = None)
mnb.fit(X,y)

print(count.get_feature_names())
# output:['best', 'dumb', 'fred', 'is', 'joy', 'lazy', 'lisa', 'smart', 'the', 'tim']


print(mnb.feature_log_prob_) 
# output 
[[-2.94443898 -2.2512918  -2.2512918  -1.55814462 -2.94443898 -1.84582669
  -1.84582669 -2.94443898 -2.94443898 -2.94443898]
 [-2.14006616 -2.83321334 -2.83321334 -1.73460106 -2.14006616 -2.83321334
  -2.83321334 -2.14006616 -2.14006616 -2.14006616]]

我的问题是:
假设单词:"best":class 1 : -2.14006616.
的概率 得到这个分数的计算公式是什么。

我正在使用 LOG (P(best|y=class=1)) -> Log(1/2) -> 无法获得 -2.14006616

documentation we can infer that feature_log_prob_ corresponds to the empirical log probability of features given a class. Let's take an example feature "best" for the purpose of this illustration, the log probability of this feature for class 1 is -2.14006616 (as you pointed out), now if we were to convert it into actual probability score it will be np.exp(1)**-2.14006616 = 0.11764. Let's take one more step back to see how and why the probability of "best" in class 1 is 0.11764. As per the documentation of Multinomial Naive Bayes 中,我们看到这些概率是使用以下公式计算的:

其中,分子大致对应特征"best"在训练集中class1(本例中我们感兴趣的)出现的次数,分母对应于 class 1 的所有特征的总数。此外,我们添加了一个小的平滑值,alpha 以防止概率变为零,n 对应于特征总数,即词汇量。为我们的示例计算这些数字,

N_yi = 1  # "best" appears only once in class `1`
N_y = 7   # There are total 7 features (count of all words) in class `1`
alpha = 1 # default value as per sklearn
n = 10    # size of vocabulary

Required_probability = (1+1)/(7+1*10) = 0.11764

您可以用类似的方式对任何给定的特征进行数学运算,class。

希望对您有所帮助!