【发布时间】:2018-12-23 02:40:33
【问题描述】:
我试图通过使用 sklearn 提供的文档来绘制 ROC 曲线。我的数据在 CSV 文件中,看起来像这样。它有两个类“好”和“坏”
我的 CSV 文件的屏幕截图
我的代码是这样的
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
import sys
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB
# Import some data to play with
df = pd.read_csv("E:\\autodesk\\TTI ROC curve.csv")
X =df[['TTI','Max TemperatureF','Mean TemperatureF','Min TemperatureF',' Min Humidity']].values
y = df['TTI_Category'].as_matrix()
# Binarize the output
y = label_binarize(y, classes=['Good','Bad'])
n_classes = y.shape[1]
# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.5,
random_state=0)
# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
# Compute ROC curve and ROC area for each class
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"])
plt.figure()
lw = 2
plt.plot(fpr[2], tpr[2], color='darkorange',
lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()enter code here
如果我运行此代码,系统会告诉我 random_state 未定义。所以我把它改成了random_state=true。然后系统告诉我
plt.plot(fpr[2], tpr[2], color='darkorange', KeyError: 2 <matplotlib.figure.Figure at 0xd8bff60>
如果我打印出 n_classes。系统告诉我它是“1”,如果我在文档中打印出 n_classes,它会显示 3。我不确定这是否是问题所在。有人对此回溯有答案吗?
【问题讨论】:
-
您应该提供一些有关代码引发哪些错误的信息。此外,不要指望人们会通过文档“逐行”指导您。
-
谢谢 Danlele。如果我运行此代码,系统会告诉我 random_state 未定义。所以我把它改成了random_state=true。然后系统告诉我“plt.plot(fpr[2], tpr[2], color='darkorange', KeyError: 2
”。有人对这个回溯有答案吗?跨度> -
哦,顺便说一下,如果我打印出 n_classes。系统告诉我它是“1”。我不确定这是否是问题所在。
-
请不要将 cmets 空间用于此类澄清和附加信息 - 改为编辑和更新您的帖子!
-
感谢 Desertnaut。我是堆栈溢出的新手。感谢您让我知道格式问题。
标签: python dataframe machine-learning scikit-learn roc