机器学习 实验一

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
# 导入必要的库
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score, KFold, cross_val_predict
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
 
# (1) 使用 pandas 从本地读取 Iris 数据集
# data = pd.read_csv('iris.csv')
# print(data)  # 打印数据集
 
# (2) 从 scikit-learn 直接加载 Iris 数据集
iris = load_iris()
print("Iris 数据集:")
print(iris.data)  # 打印数据集
 
# (3) 实现五折交叉验证进行模型训练
# 定义模型
model = LogisticRegression(max_iter=200)
 
# 定义交叉验证方法
kf = KFold(n_splits=5, shuffle=True, random_state=42)
 
# 进行五折交叉验证
scores = cross_val_score(model, iris.data, iris.target, cv=kf, scoring='accuracy')
 
# 计算预测值
y_pred = cross_val_predict(model, iris.data, iris.target, cv=kf)
 
# (4) 计算并输出模型的准确度、精度、召回率和 F1 值
# 输出交叉验证的准确度
print("交叉验证准确度分数:", scores)
print("平均准确度:", scores.mean())
 
# 输出分类报告
target_names_chinese = ['山鸢尾', '变色鸢尾', '维吉尼亚鸢尾']
report_chinese = classification_report(iris.target, y_pred, target_names=target_names_chinese)
 
# 解析分类报告,提取精度、召回率和 F1 值
report_lines = report_chinese.split('\n')
for line in report_lines:
    if line.strip() and not line.startswith('avg') and not line.startswith('micro') and not line.startswith('macro') and not line.startswith('weighted'):
        parts = line.split()
        if len(parts) == 5:
            class_name = parts[0]
            precision = float(parts[1])
            recall = float(parts[2])
            f1_score = float(parts[3])
            support = int(parts[4])
            print(f"类别: {class_name}, 精度: {precision:.2f}, 召回率: {recall:.2f}, F1 值: {f1_score:.2f}, 支持数: {support}")
 
print("中文分类报告:\n", report_chinese)

  

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