我正在使用 DecisionTree.jl 的 ScikitLearn 风格包来为 RDatasets 之一的二元分类问题创建随机森林模型数据集(请参阅 DecisionTree.jl 主页底部,了解 ScikitLearn 风格的含义)。我也在使用 MLBase模型评估包。
我已经为我的数据构建了一个随机森林模型,并希望为此模型创建 ROC 曲线。阅读可用的文档,我确实了解 ROC 曲线在理论上是什么。我只是不知道如何为特定模型创建一个。
来自Wikipedia page我在下面用粗体斜体标记的第一句话的最后一部分是引起我困惑的部分:“在统计学中,接收者操作特征(ROC)或 ROC 曲线是一个图形图,说明了二元分类器系统因为其辨别阈值是不同的。”整篇文章中有更多关于阈值的内容,但这仍然让我对二元分类问题感到困惑。阈值是多少以及如何改变它?
此外,在MLBase documentation on ROC Curves中它说“根据给定的分数和阈值 thres 计算 ROC 实例或 ROC 曲线(ROC 实例的向量)”。但实际上并没有在其他任何地方提到这个阈值。
下面给出了我的项目的示例代码。基本上,我想为随机森林创建一条 ROC 曲线,但我不确定如何或是否合适。
using DecisionTree
using RDatasets
using MLBase
quakes_data = dataset("datasets", "quakes");
# Add in a binary column as feature column for classification
quakes_data[:MagGT5] = convert(Array{Int32,1}, quakes_data[:Mag] .> 5.0)
# Getting features and labels where label = 1 is mag > 1 and label = 2 is mag <= 5
features = convert(Array, quakes_data[:, [1:3;5]]);
labels = convert(Array, quakes_data[:, 6]);
labels[labels.==0] = 2
# Create a random forest model with the tuning parameters I want
r_f_model = RandomForestClassifier(nsubfeatures = 3, ntrees = 50, partialsampling=0.7, maxdepth = 4)
# Train the model in-place on the dataset (there isn't a fit function without the in-place functionality)
DecisionTree.fit!(r_f_model, features, labels)
# Apply the trained model to the test features data set (here I haven't partitioned into training and test)
r_f_prediction = convert(Array{Int64,1}, DecisionTree.predict(r_f_model, features))
# Applying the model to the training set and looking at model stats
TrainingROC = roc(labels, r_f_prediction) #getting the stats around the model applied to the train set
# p::T # positive in ground-truth
# n::T # negative in ground-truth
# tp::T # correct positive prediction
# tn::T # correct negative prediction
# fp::T # (incorrect) positive prediction when ground-truth is negative
# fn::T # (incorrect) negative prediction when ground-truth is positive
我还读过this问题并没有发现它真的有帮助。
最佳答案
二元分类的任务是给出 0
/1
(或 true
/false
、 red
/blue
)标签到新的、未标记的数据点。大多数分类算法旨在输出连续的实值。对于具有已知或预测标签的点,该值被优化为更高 1
,对于具有已知或预测标签的点则更低 0
。使用此值生成 0
/1
预测时,会使用额外的阈值。值高于阈值的点预计将被标记为 1
(对于低于阈值的情况,预测 0
标签)。
为什么这个设置有用?因为,有时会错误预测 0
而不是1
成本比较高,那么可以将阈值设低,使得算法输出预测1
更频繁。
在预测的极端情况下0
而不是1
应用程序不需要任何成本,您可以将阈值设置为无穷大,使其始终输出 0
(这显然是最好的解决方案,因为它不产生任何成本)。
阈值技巧无法消除分类器中的错误 - 现实世界问题中没有任何分类器是完美的或没有噪声的。它能做的是改变 0
之间的比率-当-真的- 1
错误和 1
-当-真的- 0
最终分类错误。
随着阈值的增加,更多的点将被分类为 0
标签。考虑一个图表,其中分数分类为 0
在 x 轴上,以及带有 0
的点的分数-当-真的- 1
y 轴上的误差。对于每个阈值,在此图表上为生成的分类器绘制一个点。为所有阈值绘制一个点,您将得到一条曲线。这是 ROC 曲线(的某种变体),它总结了分类器的能力。分类质量的常用指标是该图表的 AUC 或曲线下面积,但事实上,整个曲线在应用中可能会引起兴趣。
这样的摘要出现在许多有关机器学习的文本中,只需通过 google 查询即可。
希望这能够澄清阈值的作用及其与 ROC 曲线的关系。
关于machine-learning - Julia 中的随机森林和 ROC 曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39990136/