【问题标题】:Getting class probabilities p(c|x) for each data point using sklearn使用 sklearn 获取每个数据点的类概率 p(c|x)
【发布时间】:2023-03-20 21:13:01
【问题描述】:

我们如何获得每个测试数据点的类别概率?有一些分类器确实包含“predict_proba()”函数,该函数返回数据点所属数据类的概率。

但是https://sklearn-lvq.readthedocs.io/en/stable/rslvq.html#中没有定义这样的函数

我需要计算每个班级的班级概率,以便可以应用拒绝选项。思路是计算 p(c|x) 并检查该值是否小于阈值,则应拒绝该数据点。

【问题讨论】:

  • 最好在交叉验证时询问。
  • 欢迎来到 StackOverflow!快速澄清:您链接的文档是“scikit-learn-compatible”项目,而不是“scikit-learn”。如果没有对该方法的概率解释,作者可能没有实现predict_proba 方法。在这种情况下,最好直接联系包的作者(通过电子邮件或在 GitHub 存储库上打开问题)。

标签: scikit-learn probability


【解决方案1】:

你可以试试这个: 方法一:覆盖predict函数如下

from sklearn_lvq import RslvqModel
from sklearn.utils.validation import check_is_fitted
from sklearn.utils import validation
import numpy as np

class RslvqModel_custom(RslvqModel):

    def predict(self, x):
        """Predict class membership index for each input sample.

        This function does classification on an array of
        test vectors X.


        Parameters
        ----------
        x : array-like, shape = [n_samples, n_features]


        Returns
        -------
        C : array, shape = (n_samples,)
            Returns predicted values.
        """
        check_is_fitted(self, ['w_', 'c_w_'])
        x = validation.check_array(x)
        if x.shape[1] != self.w_.shape[1]:
            raise ValueError("X has wrong number of features\n"
                             "found=%d\n"
                             "expected=%d" % (self.w_.shape[1], x.shape[1]))

        def foo(e):
            fun = np.vectorize(lambda w: self._costf(e, w),
                               signature='(n)->()')
            pred = fun(self.w_)
            return pred

        predictions = np.vectorize(foo, signature='(n)->(n)')(x)
        sum = np.sum(predictions, axis=1).reshape(predictions.shape[0], 1)
        return predictions/sum

np.random.seed(1)
print(__doc__)
nb_ppc = 100
x = np.append(
    np.random.multivariate_normal([0, 0], np.eye(2) / 2, size=nb_ppc),
    np.random.multivariate_normal([5, 0], np.eye(2) / 2, size=nb_ppc), axis=0)
y = np.append(np.zeros(nb_ppc), np.ones(nb_ppc), axis=0)

rslvq = RslvqModel_custom(initial_prototypes=[[5,0,0],[0,0,1]]) #_custom
model = rslvq.fit(x, y)
predictions = model.predict([[3.67, 6.50], [4.97, 1.49], [1.14, -4.3]])

print('============================================================')
print('Predictions: ', predictions)
print('-------------------------------------------------------------')

输出:

Predictions:  [[0.5977081  0.4022919 ]
 [0.945568   0.054432  ]
 [0.33533978 0.66466022]]

方法2:或者你可以简单地使用内置的后验方法:

for data in [[3.67, 6.50], [4.97, 1.49], [1.14, -4.3]]:
    print('Class 0:  posterior: ', model.posterior(0, data))
    print('Class 1:  posterior: ', model.posterior(1, data))
    print('='*100)

OUTPUT:

-------------------------------------------------------------
Class 0:  posterior:  [[0.5977081]]
Class 1:  posterior:  [[0.4022919]]
========================================================
Class 0:  posterior:  [[0.945568]]
Class 1:  posterior:  [[0.054432]]
========================================================
Class 0:  posterior:  [[0.33533978]]
Class 1:  posterior:  [[0.66466022]]
========================================================

【讨论】:

  • 您好,感谢您提供这些方法。好吧,我对方法 2 有点困惑。内置的后验方法不返回概率,而是一个比率。 "return s1 / s2" 这些结果真的是类概率吗?
猜你喜欢
  • 2018-04-19
  • 2021-06-26
  • 2021-11-04
  • 2013-12-15
  • 2017-07-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 2016-12-17
相关资源
最近更新 更多