预测 classes 或 class 概率?

Predict classes or class probabilities?

我目前正在使用 H2O 处理分类问题数据集。我正在 python 3.6 环境中使用 H2ORandomForestEstimator 对其进行测试。我注意到预测方法的结果给出了 0 到 1 之间的值(我假设这是概率)。

在我的数据集中,目标属性是数字,即 True 值为 1,False 值为 0。我确保将目标属性的类型转换为类别,我是仍然得到相同的结果。

然后我修改了代码,仍然在 H2OFrame 上使用 asfactor() 方法将目标列转换为因子,结果没有任何变化。

但是,当我将目标属性中的值分别更改为 True 和 False(分别表示 1 和 0)时,我得到了预期的结果(即)输出是分类而不是概率。

原则上&理论上,硬&软class化(即返回classes&概率 分别)是不同的方法,每个方法都有自己的优点和缺点。例如,考虑以下内容,来自论文 Hard or Soft Classification? Large-margin Unified Machines:

Margin-based classifiers have been popular in both machine learning and statistics for classification problems. Among numerous classifiers, some are hard classifiers while some are soft ones. Soft classifiers explicitly estimate the class conditional probabilities and then perform classification based on estimated probabilities. In contrast, hard classifiers directly target on the classification decision boundary without producing the probability estimation. These two types of classifiers are based on different philosophies and each has its own merits.

也就是说,在实践中,今天使用的大多数 classifier,包括随机森林(我能想到的唯一例外是 SVM 系列)实际上是 soft classifiers:它们在下面实际产生的是类似概率的度量,随后与隐式 threshold(通常在二进制情况下默认为 0.5)相结合, 提供硬性 class 成员资格,例如 0/1True/False.

What is the right way to get the classified prediction result?

对于初学者来说,从概率到困难总是有可能的 classes,但反之则不然。

一般来说,考虑到您的 classifier 实际上是一个 soft ififications,最终硬 classifications (True/False) 给这个过程带来了“黑匣子”的味道,这在原则上是不受欢迎的;直接处理产生的概率,并且(重要!)明确控制 决策阈值 应该是这里的首选方法。根据我的经验,这些是新从业者经常忘记的微妙之处;例如考虑以下内容,来自交叉验证线程 Reduce Classification probability threshold:

the statistical component of your exercise ends when you output a probability for each class of your new sample. Choosing a threshold beyond which you classify a new observation as 1 vs. 0 is not part of the statistics any more. It is part of the decision component.

除了像上面这样的“软”论点(双关语意想不到的)之外,在某些情况下您需要直接处理潜在的概率和阈值,即默认阈值的情况0.5 的二进制 classification 会让你误入歧途,尤其是当你的 classes 不平衡时;有关此类情况的具体示例,请参阅我在 (及其中的链接)中的回答。

老实说,我对您报告的 H2O 的行为感到相当惊讶(我个人没有使用过),即输出的种类受输入的表示形式的影响;事实并非如此,如果确实如此,我们可能会遇到设计不良的问题。例如,比较 scikit-learn 中的随机森林 classifier,它包括两种不同的方法,predict and predict_proba,以分别获得硬 classifications 和底层概率(并检查文档,很明显,predict 的输出是基于 概率估计 ,之前已经计算过了)。

If probabilities are the outcomes for numerical target values, then how do I handle it in case of a multiclass classification?

除了简单的阈值不再有意义之外,原则上没有什么新东西;再次,来自 scikit-learn 中的随机森林 predict 文档:

the predicted class is the one with highest mean probability estimate

也就是说,对于 3 classes (0, 1, 2),你得到 [p0, p1, p2] 的估计(根据概率规则,元素总和为 1),并且predicted class 是概率最高的那个,例如class #1 对于 [0.12, 0.60, 0.28] 的情况。这是一个 和 3-class 鸢尾花数据集(它用于 GBM 算法和 R 中,但基本原理是相同的)。

添加到@desertnaut 的回答中,并且由于您将此问题标记为 Python,以下是您处理问题最后一部分的方式:

If probabilities are the outcomes for numerical target values, then how do I handle it in case of a multiclass classification?

y_pred = np.argmax(prob, axis=1)

这会将 (num_examples, n_classes) 概率值数组转换为 (num_examples, ) 预测 类 数组。