机器学习 实验二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)

  

posted @   财神给你送元宝  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示