机器学习 实验三

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
# 导入必要的库
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, cross_val_score, cross_validate
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
import seaborn as sns
 
# (1)加载iris数据集,并留出1/3的样本作为测试集
iris = load_iris()
X = iris.data
y = iris.target
 
# 使用留出法,留出1/3的样本作为测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=42, stratify=y)
# (2)使用训练集训练分类带有预剪枝和后剪枝的决策树模型
# 预剪枝可以通过设置max_depth参数来实现
# 后剪枝可以通过设置min_samples_split和min_samples_leaf参数来实现
clf_pre_pruning = DecisionTreeClassifier(max_depth=3, random_state=42)
clf_pre_pruning.fit(X_train, y_train)
 
# (3)使用五折交叉验证对模型性能进行评估和选择
scores = cross_validate(clf_pre_pruning, X_train, y_train, cv=5,
                        scoring=['accuracy', 'precision_macro', 'recall_macro', 'f1_macro'])
 
# 打印交叉验证结果
print("五折交叉验证结果:")
print(f"准确度:{scores['test_accuracy'].mean():.4f} ± {scores['test_accuracy'].std():.4f}")
print(f"精度:{scores['test_precision_macro'].mean():.4f} ± {scores['test_precision_macro'].std():.4f}")
print(f"召回率:{scores['test_recall_macro'].mean():.4f} ± {scores['test_recall_macro'].std():.4f}")
print(f"F1值:{scores['test_f1_macro'].mean():.4f} ± {scores['test_f1_macro'].std():.4f}")
 
# (4)使用测试集测试模型的性能
y_pred = clf_pre_pruning.predict(X_test)
 
# 计算测试集的性能指标
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
 
# 打印测试集的性能指标
print("测试集性能指标:")
print(f"准确度:{accuracy:.4f}")
print(f"精度:{precision:.4f}")
print(f"召回率:{recall:.4f}")
print(f"F1值:{f1:.4f}")
 
# 绘制混淆矩阵
cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=iris.target_names, yticklabels=iris.target_names)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

  

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