计算AUC和绘制ROC曲线

来源:互联网 发布:java在线笔试题 编辑:程序博客网 时间:2024/05/20 18:52
import csvimport numpy import matplotlib.pyplot as pltfrom sklearn.svm import SVCfrom sklearn.metrics import  roc_auc_score,roc_curveimport pandas as pd
with open('F:/1.csv', 'r') as Data:     read1 = csv.reader(Data)     read1= numpy.array(list(read1), dtype='float64')     CreditSocringData.close()X=read1[0:read1.shape[0],0:read1.shape[1]-1] Y=read1[0:read1.shape[0],read1.shape[1]-1]    X_train,X_test,y_train,y_test=train_test_split(X,Y,test_size = 0.1, random_state=0) 
clf= SVC(kernel='rbf',probability=True) clf.fit(X_train, y_train)y_true, y_predict = y_test, clf.predict(X_test)
y_pred_pro = clf.predict_proba(X_test) y_scores = pd.DataFrame(y_pred_pro,columns=clf.classes_.tolist())[1].values  auc_value = roc_auc_score(y_ture,y_scores)fpr, tpr, thresholds = roc_curve(y_ture,y_scores,pos_label=1.0) plt.figure(figsize=(6,4))     plt.plot(fpr,tpr,color='blue',linewidth=2,label='AUC (area:%0.4f)'%auc_value)  plt.plot([0, 1], [0, 1], color='black',linewidth=2, linestyle='--')  plt.xlim([0.0, 1.0])  plt.ylim([0.0, 1.0])  plt.xlabel('False Positive Rate')  plt.ylabel('True Positive Rate')  plt.title('ROC')  plt.legend(loc="lower right")      plt.show() 
原创粉丝点击