【代码模版】分类评估指标之混淆矩阵与ROC/AUC代码模版

# Precision, Recall, F1
from sklearn.metrics import precision_score, recall_score, f1_score
print("precision score: ", precision_score(y_true, y_predict))
print("recall score: ", recall_score(y_true, y_predict))
print("f1 score: ", f1_score(y_true, y_predict))
# Compute Area Under the Receiver Operating Characteristic Curve (ROC AUC) from prediction scores.
# we need a y_scores which is similar to y_predict
from sklearn.model_selection import cross_val_predict
y_scores = cross_val_predict(model_name, x_train, y_train, cv=3,
                             method="decision_function")
from sklearn.metrics import roc_auc_score
print("roc/auc score: ", roc_auc_score(y_true, y_scores))
# show ROC/AUC
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_true, y_scores)
def plot_roc_curve(fpr, tpr, label=None):
    plt.plot(fpr, tpr, linewidth=2, label=label)
    plt.plot([0, 1], [0, 1], 'k--')
    plt.axis([0, 1, 0, 1])
    plt.xlabel('False Positive Rate', fontsize=16)
    plt.ylabel('True Positive Rate', fontsize=16)

plt.figure(figsize=(8, 6))
plot_roc_curve(fpr, tpr)
plt.show()
发布了40 篇原创文章 · 获赞 0 · 访问量 1706

猜你喜欢

转载自blog.csdn.net/weixin_44680262/article/details/104684716