from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, cross_validate
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import make_scorer, accuracy_score, precision_score, recall_score, f1_score, classification_report
# (1) 从 scikit-learn 库中加载 iris 数据集,使用留出法留出 1/3 的样本作为测试集(注意同分布取样)
# 加载数据集
iris = load_iris()
X, y = iris.data, iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=42, stratify=y)
# (2) 使用训练集训练对数几率回归(逻辑回归)分类算法
# 创建逻辑回归模型实例
model = LogisticRegression(solver='lbfgs', max_iter=200)
# 训练模型
model.fit(X_train, y_train)
# (3) 使用五折交叉验证对模型性能(准确度、精度、召回率和 F1 值)进行评估和选择
# 定义多个评分器
scoring = {
'accuracy': make_scorer(accuracy_score),
'precision': make_scorer(precision_score, average='weighted'),
'recall': make_scorer(recall_score, average='weighted'),
'f1': make_scorer(f1_score, average='weighted')
}
# 使用交叉验证评估模型
scores = cross_validate(model, X_train, y_train, cv=5, scoring=scoring)
# 输出各项指标的平均值
print("五折交叉验证结果:")
for key in scores:
if key.startswith('test_'):
print(f"{key[5:]}: {scores[key].mean():.4f}") # 去掉前缀 'test_' 来显示指标名称
# (4) 使用测试集,测试模型的性能,对测试结果进行分析,完成实验报告中实验二的部分
# 预测测试集标签
y_pred = model.predict(X_test)
# 打印分类报告
report = classification_report(y_test, y_pred, target_names=iris.target_names, output_dict=True)
print("\n测试集上的分类报告:")
for label, metrics in report.items():
if isinstance(metrics, dict):
print(f"类别 {label}:")
for metric, value in metrics.items():
print(f" {metric}: {value:.4f}")
else:
print(f"{label}: {value:.4f}")
# 打印混淆矩阵
from sklearn.metrics import confusion_matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("\n混淆矩阵:")
print(conf_matrix)