在scikit学习中,我使用MultinomialNB对标记的文本数据进行多类分类。我在预测时使用了multinomialNB的“multinomialNB”特性
clf=MultinomialNB()
print(clf.fit(X_train,Y_train))
clf.predict_proba(X_test[0])因此,我得到了每个类的概率值向量,它加到1,我知道这是因为softmax交叉熵函数。
数组([ 0.01245064,0.02346781,0.84694063,0.03238112,0.01833107,0.03103464,0.03539408 ])
我在这里的问题是,在预测时,我需要有binary_cross_entropy,这样我就可以得到0到1之间的每个类的概率值的向量。那么,我如何改变功能,同时做科学预测-学习?
发布于 2018-12-09 19:00:57
您可以通过以下方法获得每个类的日志可能性:
_joint_log_likelihood(self, X):
"""Compute the unnormalized posterior log probability of X
I.e. ``log P(c) + log P(x|c)`` for all rows x of X, as an array-like of
shape [n_classes, n_samples].
Input is passed to _joint_log_likelihood as-is by predict,
predict_proba and predict_log_proba.
""" 朴素贝叶斯predict_log_proba只是通过规范上面的函数来工作。
def predict_log_proba(self, X):
"""
Return log-probability estimates for the test vector X.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Returns
-------
C : array-like, shape = [n_samples, n_classes]
Returns the log-probability of the samples for each class in
the model. The columns correspond to the classes in sorted
order, as they appear in the attribute `classes_`.
"""
jll = self._joint_log_likelihood(X)
# normalize by P(x) = P(f_1, ..., f_n)
log_prob_x = logsumexp(jll, axis=1)
return jll - np.atleast_2d(log_prob_x).T https://stackoverflow.com/questions/53690588
复制相似问题