【发布时间】:2017-02-20 18:08:21
【问题描述】:
我正在使用 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 的问题,并没有发现它真的很有帮助。
【问题讨论】:
标签: machine-learning julia random-forest decision-tree roc