9.26
(1)从 scikit-learn 库中加载 iris 数据集,使用留出法留出 1/3 的样本作为测试集(注
意同分布取样);
(2)使用训练集训练朴素贝叶斯分类算法;
(3)使用五折交叉验证对模型性能(准确度、精度、召回率和 F1 值)进行评估和选
择;
(4)使用测试集,测试模型的性能,对测试结果进行分析,完成实验报告中实验六的
部分。
import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split, cross_val_score, KFold from sklearn.naive_bayes import GaussianNB from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score # 加载iris数据集 iris = load_iris() X = iris.data y = iris.target # 使用留出法划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=42, stratify=y) # 训练朴素贝叶斯模型 nb = GaussianNB() nb.fit(X_train, y_train) # 五折交叉验证评估模型性能 kf = KFold(n_splits=5, shuffle=True, random_state=42) cv_scores = cross_val_score(nb, X_train, y_train, cv=kf, scoring='accuracy') precision_scores = cross_val_score(nb, X_train, y_train, cv=kf, scoring='precision_macro') recall_scores = cross_val_score(nb, X_train, y_train, cv=kf, scoring='recall_macro') f1_scores = cross_val_score(nb, X_train, y_train, cv=kf, scoring='f1_macro') print("五折交叉验证准确度:", cv_scores.mean()) print("五折交叉验证精度:", precision_scores.mean()) print("五折交叉验证召回率:", recall_scores.mean()) print("五折交叉验证F1值:", f1_scores.mean()) # 使用测试集测试模型性能 y_pred = nb.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("测试集准确度:", accuracy) print("测试集精度:", precision) print("测试集召回率:", recall) print("测试集F1值:", f1) # 测试结果分析 # 比较交叉验证和测试集的评估指标 if abs(accuracy - cv_scores.mean()) < 0.1: print("模型的泛化能力较好,在训练集交叉验证和测试集上的性能较为接近。") else: print("模型可能存在过拟合或欠拟合问题,训练集交叉验证和测试集上的性能差异较大。") # 分析各类别样本的预测情况 from collections import Counter class_pred_count = Counter(y_pred) class_true_count = Counter(y_test) for class_label in range(3): class_accuracy = accuracy_score(y_test[y_test == class_label], y_pred[y_test == class_label]) print(f"类别 {class_label} 的准确度: {class_accuracy}") if class_pred_count[class_label] == 0: print(f"类别 {class_label} 在预测结果中未出现,可能存在对该类别的学习不足问题。") elif class_true_count[class_label] == 0: print(f"测试集中没有类别 {class_label} 的样本,可能数据划分存在问题。")

浙公网安备 33010602011771号