如何二值化 RandomForest 以在 python 中绘制 ROC? [英] How to binarize RandomForest to plot a ROC in python?

查看:41
本文介绍了如何二值化 RandomForest 以在 python 中绘制 ROC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 21 节课.我正在使用随机森林.我想绘制 ROC 曲线,所以我检查了 scikit ROC 与 SVM

I have 21 classes. I am using RandomForest. I want to plot a ROC curve, so I checked the example in scikit ROC with SVM

该示例使用 SVM.SVM 具有参数,如:概率和决策函数形状,而 RF 没有.

The example uses SVM. SVM has parameters like: probability and decision_function_shape which RF does not.

那么我怎样才能对 RandomForest 进行二值化并绘制 ROC?

So how can I binarize RandomForest and plot a ROC?

谢谢

编辑

创建虚假数据.所以有 20 个特征和 21 个类(每个类 3 个样本).

To create the fake data. So there are 20 features and 21 classes (3 samples for each class).

df = pd.DataFrame(np.random.rand(63, 20))
label = np.arange(len(df)) // 3 + 1 
df['label']=label
df


#TO TRAIN THE MODEL: IT IS A STRATIFIED SHUFFLED SPLIT
clf = make_pipeline(RandomForestClassifier())   
xSSSmean10 = []
for i in range(10):
    sss = StratifiedShuffleSplit(y, 10, test_size=0.1, random_state=i) 
    scoresSSS = cross_validation.cross_val_score(clf, x, y , cv=sss)

    xSSSmean10.append(scoresSSS.mean())
result_list.append(xSSSmean10)
print("") 

推荐答案

对于多标签随机森林,您的 21 个标签中的每一个都有一个二元分类,您可以为 21 个类中的每一个创建 ROC 曲线.您的 y_train 应该是每个标签的 0 和 1 矩阵.

For multilabel random forest, each of your 21 labels has a binary classification, and you can create a ROC curve for each of the 21 classes. Your y_train should be a matrix of 0 and 1 for each label.

假设您拟合了来自 sklearn 的多标签随机森林并将其命名为 rf,并且在测试列车拆分后具有 X_test 和 y_test.您可以使用以下方法在 python 中为您的第一个标签绘制 ROC 曲线:

Assume you fit a multilabel random forest from sklearn and called it rf, and have a X_test and y_test after a test train split. You can plot the ROC curve in python for your first label using this:

from sklearn import metrics 
probs = rf.predict_proba(X_test)
fpr, tpr, threshs = metrics.roc_curve(y_test['name_of_your_first_tag'],probs[0][:,1])

希望这会有所帮助.如果你提供你的代码和数据,我可以更具体地写这个.

Hope this helps. If you provide your code and data I could write this more specifically.

这篇关于如何二值化 RandomForest 以在 python 中绘制 ROC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆