python - How to get ROC curve for decision tree? -
i trying find roc curve , auroc curve decision tree. code 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) output:
error() 'decisiontreeclassifier' object has no attribute 'decision_function' basically, error coming while finding y_score. please explain y_score , how solve problem?
first of all, decisiontreeclassifier has no attribute decision_function.
if guess structure of code , saw example
in case classifier not decision tree onevsrestclassifier supports decision_function method.
you can see available attributes of decisiontreeclassifier here
a possible way binarize classes , compute auc each class:
example:
from sklearn.metrics import roc_curve, auc sklearn.model_selection import train_test_split sklearn.preprocessing import label_binarize sklearn.tree import decisiontreeclassifier 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() 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 , roc area fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel()) roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]) #roc curve specific class here class 2 roc_auc[2] result
0.94852941176470573
Comments
Post a Comment