The following is a sample code that uses Python to build a logistic regression model and perform 5-fold cross-validation, while plotting the ROC curve and the average ROC curve for each validation:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
# 生成一些示例数据
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
# 初始化逻辑回归模型
lr = LogisticRegression()
# 初始化交叉验证对象
cv = StratifiedKFold(n_splits=5)
# 初始化用于存储每次验证的ROC曲线数据的列表
roc_curves = []
# 进行交叉验证
for train_index, test_index in cv.split(X, y):
# 划分训练集和测试集
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# 训练模型
lr.fit(X_train, y_train)
# 预测概率
y_scores = lr.predict_proba(X_test)[:, 1]
# 计算ROC曲线数据
fpr, tpr, _ = roc_curve(y_test, y_scores)
roc_auc = auc(fpr, tpr)
# 绘制ROC曲