随机森林导入数据用kfold分层抽样后用下列画roc_curve曲线三分类python代码mse = mean_squared_error(y_test, y_pred1) rmse = math.sqrt(mse) print('FNN深度森林RMSE:', rmse) print('FNN深度森林Accuracy:', accuracy_score(y_test, y_pred1)) mse = mean_squared_error(y_test_fuzzy, y_pred) rmse = math.sqrt(mse) print('深度森林RMSE:', rmse) print('深度森林Accuracy:', accuracy_score(y_test_fuzzy, y_pred)) fpr = dict() tpr = dict() roc_auc = dict() for i in range(3): # 遍历三个类别 fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_pred1[:, 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_pred1.ravel()) roc_auc["micro"] = auc(fpr["micro"], tpr["micro"]) # Compute macro-average ROC curve and ROC area(方法一) # First aggregate all false positive rates all_fpr = np.unique(np.concatenate([fpr[i] for i in range(3)])) # Then interpolate all ROC curves at this points mean_tpr = np.zeros_like(all_fpr) for i in range(3): mean_tpr += interp(all_fpr, fpr[i], tpr[i]) # Finally average it and compute AUC mean_tpr /= 3 fpr["macro"] = all_fpr tpr["macro"] = mean_tpr roc_auc["macro"] = auc(fpr["macro"], tpr["macro"]) # Plot all ROC curves lw = 2 plt.figure() plt.plot(fpr["micro"], tpr["micro"], label='micro-average ROC curve (area = {0:0.2f})' ''.format(roc_auc["micro"]), color='deeppink', linestyle=':', linewidth=4) plt.plot(fpr["macro"], tpr["macro"], label='macro-average ROC curve (area = {0:0.2f})' ''.format(roc_auc["macro"]), color='navy', linestyle=':', linewidth=4) colors = cycle(['aqua', 'darkorange', 'cornflowerblue']) for i, color in zip(range(3), colors): plt.plot(fpr[i], tpr[i], color=color, lw=lw, label='ROC curve of class {0} (area = {1:0.2f})' ''.format(i, roc_auc[i])) plt.plot([0, 1], [0, 1], 'k--', lw=lw) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('DF-F') plt.legend(loc="lower right")
时间: 2024-02-06 20:03:44 浏览: 187
ROC.zip_crowdv82_python_python roc曲线_roc 数据_roc曲线python
5星 · 资源好评率100%
这是一个随机森林在三分类问题上使用kfold分层抽样后进行评估的Python代码,其中包括了计算RMSE、Accuracy和绘制ROC曲线的步骤。其中,使用了sklearn库中的mean_squared_error、accuracy_score、roc_curve和auc函数来计算RMSE、Accuracy和ROC曲线的数据。在绘制ROC曲线时,采用了方法一和方法二两种方法,分别计算了macro-average和micro-average的ROC曲线。
阅读全文