[Machine Learning Practice] ROC Curve Drawing for Classification Algorithm Evaluation (Multiple Model Comparison)

1. Code

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_curve, roc_auc_score
from sklearn.model_selection import train_test_split

# 生成样本数据
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)

# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 初始化分类器
classifiers = [
    ('Logistic Regression', LogisticRegression()),
    ('Decision Tree', DecisionTreeClassifier()),
    ('Random Forest', RandomForestClassifier())
]

# 遍历每个分类器,训练并绘制ROC曲线
for name, classifier in classifiers:
    classifier.fit(X_train, y_train)
    y_pred_proba = classifier.predict_proba(X_test)[:,1]
    fpr, tpr, _ = roc_curve(y_test, y_pred_proba)
    auc = roc_auc_score(y_test, y_pred_proba)
    plt.plot(fpr, tpr, label=f'{
      
      name} (AUC = {
      
      auc:.2f})')

# 绘制基准线
plt.plot([0, 1], [0, 1], 'k--', label='Random Guess')

# 设置图例、标题、坐标轴标签等信息
plt.legend()
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.show()

2. Results

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44109827/article/details/129727291