我使用以下代碼創建ROC曲線:
probs = model.predict_proba(X)[::,1]
auc = metrics.roc_auc_score(y, probs)
fper, tper, thresholds = roc_curve(y, probs)
plt.plot(fper, tper, label= model_name + " (auc = %0.3f)" % auc, color=color)
plt.plot([0, 1], [0, 1], color='black', linestyle='--')
plt.xlabel('False Positive Rate', fontsize=15)
plt.ylabel('True Positive Rate', fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.show()
盡管如此,這樣我只能為一個模型創建ROC,但是我怎么能用這個代碼,sa來表示幾個模型的ROC曲線,而不是像上面這樣的一個模型?
現在還不清楚你想要什么樣的曲線圖,你是要求ROC曲線在多個獨立的曲線圖中還是相互重疊?
如果需要多個繪圖,請檢查以下函數:https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subplots_demo.html
下面是他們給出的一個例子,說明如何使用
plt.subplots
在一個圖形上放置4個繪圖:因此,對于您來說,您需要創建子圖,而不是使用
plt.plot(fper, tper, label= model_name + " (auc = %0.3f)" % auc, color=color)
繪制,而是執行axs[i, j].plot(fper, tper, label= model_name + " (auc = %0.3f)" % auc, color=color)
如果希望ROC曲線覆蓋在同一繪圖上,則matplotlib默認情況下會這樣做。只需按這里描述的方式繪制所有數據:如何在一個圖形中為不同的繪圖獲得不同的彩色線條?。