Python案例如何用Scikit-learn做AUC计算

wen python案例 2

本文目录导读:

Python案例如何用Scikit-learn做AUC计算

  1. 基础二分类AUC计算
  2. 多分类AUC计算
  3. 交叉验证中的AUC计算
  4. 完整的示例:比较不同模型
  5. 实际案例:二分类任务完整流程
  6. 重要注意事项:

我来介绍几种用Scikit-learn计算AUC的方法:

基础二分类AUC计算

import numpy as np
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
# 创建示例数据
X, y = make_classification(n_samples=1000, n_features=20, 
                           n_informative=2, n_redundant=10,
                           random_state=42)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)
# 训练模型
model = LogisticRegression()
model.fit(X_train, y_train)
# 获取预测概率(注意是正类的概率)
y_pred_prob = model.predict_proba(X_test)[:, 1]
# 计算AUC
auc_score = roc_auc_score(y_test, y_pred_prob)
print(f"AUC Score: {auc_score:.4f}")
# 获取ROC曲线数据
fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)

多分类AUC计算

from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import label_binarize
# 创建多分类数据
X, y = make_classification(n_samples=1000, n_features=20, 
                           n_classes=3, n_clusters_per_class=1,
                           random_state=42)
# 二值化标签
y_bin = label_binarize(y, classes=[0, 1, 2])
n_classes = y_bin.shape[1]
# 划分数据
X_train, X_test, y_train, y_test = train_test_split(
    X, y_bin, test_size=0.3, random_state=42
)
# 训练多分类模型
model = OneVsRestClassifier(LogisticRegression())
model.fit(X_train, y_train)
y_pred_prob = model.predict_proba(X_test)
# 计算每个类别的AUC
for i in range(n_classes):
    auc = roc_auc_score(y_test[:, i], y_pred_prob[:, i])
    print(f"AUC for class {i}: {auc:.4f}")
# 计算宏平均AUC
macro_auc = roc_auc_score(y_test, y_pred_prob, multi_class='ovr', average='macro')
print(f"Macro-average AUC: {macro_auc:.4f}")
# 计算微平均AUC
micro_auc = roc_auc_score(y_test, y_pred_prob, multi_class='ovr', average='micro')
print(f"Micro-average AUC: {micro_auc:.4f}")

交叉验证中的AUC计算

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold
# 使用交叉验证计算AUC
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
auc_scores = cross_val_score(model, X, y_bin, cv=cv, 
                             scoring='roc_auc_ovr')
print("Cross-validation AUC scores:", auc_scores)
print(f"Mean AUC: {auc_scores.mean():.4f} (±{auc_scores.std() * 2:.4f})")

完整的示例:比较不同模型

import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
# 创建数据
X, y = make_classification(n_samples=1000, n_features=20, 
                           random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42
)
# 定义要比较的模型
models = {
    'Logistic Regression': LogisticRegression(),
    'Random Forest': RandomForestClassifier(n_estimators=100),
    'SVM': SVC(probability=True)  # 需要probability=True才能获取概率
}
# 比较不同模型的AUC
results = {}
plt.figure(figsize=(10, 8))
for name, model in models.items():
    model.fit(X_train, y_train)
    y_prob = model.predict_proba(X_test)[:, 1]
    # 计算AUC
    auc = roc_auc_score(y_test, y_prob)
    results[name] = auc
    # 获取ROC曲线数据
    fpr, tpr, _ = roc_curve(y_test, y_prob)
    # 绘制ROC曲线
    plt.plot(fpr, tpr, label=f'{name} (AUC = {auc:.3f})', lw=2)
# 绘制对角线(随机分类器)
plt.plot([0, 1], [0, 1], 'k--', lw=2, label='Random')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')'ROC Curves Comparison')
plt.legend(loc="lower right")
plt.grid(True)
plt.show()
# 打印结果
print("\nModel Performance (AUC):")
for name, auc in sorted(results.items(), key=lambda x: x[1], reverse=True):
    print(f"{name}: {auc:.4f}")

实际案例:二分类任务完整流程

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
# 创建示例数据(模拟真实场景)
np.random.seed(42)
n_samples = 500
# 创建特征
data = {
    'age': np.random.randint(18, 80, n_samples),
    'income': np.random.normal(50000, 20000, n_samples),
    'education_years': np.random.randint(8, 20, n_samples),
    'credit_score': np.random.randint(300, 850, n_samples),
    'debt_ratio': np.random.uniform(0, 1, n_samples)
}
df = pd.DataFrame(data)
# 创建目标变量(是否贷款违约)
# 基于一些规则生成
df['default'] = (
    (df['credit_score'] < 600) & 
    (df['debt_ratio'] > 0.5)
).astype(int)
# 添加一些随机性和缺失值
df.loc[np.random.choice(n_samples, 50), 'income'] = np.nan
df['default'] = df['default'] | (np.random.random(n_samples) < 0.1)
# 特征和目标
X = df.drop('default', axis=1)
y = df['default']
# 划分数据
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)
# 创建预处理和模型管道
pipeline = Pipeline([
    ('imputer', SimpleImputer(strategy='median')),
    ('scaler', StandardScaler()),
    ('classifier', LogisticRegression(random_state=42))
])
# 训练模型
pipeline.fit(X_train, y_train)
# 预测概率
y_probs = pipeline.predict_proba(X_test)[:, 1]
# 计算AUC
auc = roc_auc_score(y_test, y_probs)
print(f"Test AUC Score: {auc:.4f}")
# 获取特征重要性(如果是决策树/随机森林等)
if hasattr(pipeline.named_steps['classifier'], 'coef_'):
    coefficients = pipeline.named_steps['classifier'].coef_[0]
    features = X.columns
    feature_importance = pd.DataFrame({
        'feature': features,
        'coefficient': coefficients,
        'abs_coefficient': np.abs(coefficients)
    }).sort_values('abs_coefficient', ascending=False)
    print("\nFeature Importance (by coefficient):")
    print(feature_importance)

重要注意事项:

  1. 输入要求roc_auc_score 需要预测的概率值,而不是类别标签
  2. 概率方向:确保传入的是正类(通常为1)的概率
  3. 数据平衡:AUC对不平衡数据相对稳健
  4. 多分类:使用multi_class参数指定计算方法('ovr'或'ovo')

这些示例涵盖了从基础到进阶的AUC计算方法,适用于不同的应用场景。

抱歉,评论功能暂时关闭!