如何获得决策树的ROC曲线? [英] How to get ROC curve for decision tree?

查看:834
本文介绍了如何获得决策树的ROC曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为决策树找到 ROC曲线 AUROC曲线。我的代码是这样的

I am trying to find ROC curve and AUROC curve for decision tree. My code was something like

clf.fit(x,y)
y_score = clf.fit(x,y).decision_function(test[col])
pred = clf.predict_proba(test[col])
print(sklearn.metrics.roc_auc_score(actual,y_score))
fpr,tpr,thre = sklearn.metrics.roc_curve(actual,y_score)

输出:

 Error()
'DecisionTreeClassifier' object has no attribute 'decision_function'

基本上,发现 y_score 时出现错误。请解释什么是 y_score 以及如何解决此问题?

basically, the error is coming up while finding the y_score. Please explain what is y_score and how to solve this problem?

推荐答案

首先, DecisionTreeClassifier 没有属性 decision_function

如果从您的代码结构中猜出,您会看到示例

If I guess from the structure of your code , you saw this example

在这种情况下,分类器不是决策树,而是支持Decision_function方法的OneVsRestClassifier。

In this case the classifier is not the decision tree but it is the OneVsRestClassifier that supports the decision_function method.

您可以看到 DecisionTreeClassifier 在这里

一种可行的方法是二进制化类,然后计算每个类的auc:

A possible way to do it is to binarize the classes and then compute the auc for each class:

示例:

from sklearn import datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.tree import DecisionTreeClassifier
from scipy import interp


iris = datasets.load_iris()
X = iris.data
y = iris.target

y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5, random_state=0)

classifier = DecisionTreeClassifier()

y_score = classifier.fit(X_train, y_train).predict(X_test)

fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
    fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])

# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

#ROC curve for a specific class here for the class 2
roc_auc[2]

结果

0.94852941176470573

这篇关于如何获得决策树的ROC曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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