python-3.x - 多项式朴素贝叶斯 softmax 改变

标签 python-3.x machine-learning scikit-learn deep-learning cross-entropy

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

clf=MultinomialNB()
print(clf.fit(X_train,Y_train))
clf.predict_proba(X_test[0])

结果我得到了每个类的概率值向量,加起来为 1。我知道这是因为 softmax 交叉熵函数。

array ( [ [ 0.01245064, 0.02346781, 0.84694063, 0.03238112, 0.01833107, 0.03103464, 0.03539408 ] ] )

我的问题是,在预测时我需要有binary_cross_entropy,以便我获得每个类别在0到1之间相互独立的概率值向量。那么在 scikit-learn 中进行预测时如何更改函数呢?

最佳答案

您可以使用以下方法获得每个类别的对数可能性:

_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 

关于python-3.x - 多项式朴素贝叶斯 softmax 改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53690588/

相关文章:

python-3.x - 如何在文本分类的拆分数据测试和训练中修复 'typeerror: only integer scalar arrays can be converted to a scalar index'

python - 有一个不以 return 语句结尾的函数是 pythonic 吗

python - 为什么要显式调用 asyncio.StreamWriter.drain?

python - 判断两行中第一行是否为标题

Scikit-Learn:使用 DBSCAN 预测新点

python - load_digits() 和 fetch_mldata ("MNIST Original"有什么区别)

python - 如何将数据帧的第一行读取为数据行而不是标题

python - PyTorch 在用线性模型逼近平方函数时不收敛

python - sklearn 数据分割的随机状态参数问题

machine-learning - 使用 scikit learn 预测网站上的优质内容