首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多项式朴素贝叶斯软件

多项式朴素贝叶斯软件
EN

Stack Overflow用户
提问于 2018-12-09 08:27:08
回答 1查看 270关注 0票数 1

在scikit学习中,我使用MultinomialNB对标记的文本数据进行多类分类。我在预测时使用了multinomialNB的“multinomialNB”特性

代码语言:javascript
复制
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之间的每个类的概率值的向量。那么,我如何改变功能,同时做科学预测-学习?

EN

回答 1

Stack Overflow用户

发布于 2018-12-09 19:00:57

您可以通过以下方法获得每个类的日志可能性:

代码语言:javascript
复制
_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只是通过规范上面的函数来工作。

代码语言:javascript
复制
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 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53690588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档