2024.12.4(周三)
import numpy as np from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold from sklearn.svm import SVC from sklearn.datasets import load_iris from sklearn.metrics import precision_score, recall_score, f1_score, accuracy_score from sklearn.model_selection import cross_validate # 1. 加载 Iris 数据集 iris = load_iris() X = iris.data y = iris.target # 2. 使用留出法分割数据集(1/3 测试集,2/3 训练集) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=42, stratify=y) # 3. 训练支持向量机(SVM)分类算法 svm_model = SVC(kernel='linear', random_state=42) svm_model.fit(X_train, y_train) # 4. 使用五折交叉验证评估模型性能(准确度、精度、召回率、F1 值) cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) scoring = ['accuracy', 'precision_weighted', 'recall_weighted', 'f1_weighted'] # 使用 cross_validate 计算交叉验证结果 cv_results = cross_validate(svm_model, X_train, y_train, cv=cv, scoring=scoring) # 打印交叉验证结果 print(f"准确度(均值): {cv_results['test_accuracy'].mean():.4f}") print(f"精度(均值): {cv_results['test_precision_weighted'].mean():.4f}") print(f"召回率(均值): {cv_results['test_recall_weighted'].mean():.4f}") print(f"F1 分数(均值): {cv_results['test_f1_weighted'].mean():.4f}") # 5. 使用测试集评估模型性能 y_pred = svm_model.predict(X_test) # 计算测试集上的性能指标 accuracy = accuracy_score(y_test, y_pred) precision = precision_score(y_test, y_pred, average='weighted') recall = recall_score(y_test, y_pred, average='weighted') f1 = f1_score(y_test, y_pred, average='weighted') # 打印测试集上的评估结果 print(f"\n测试集评估结果:") print(f"准确度: {accuracy:.4f}") print(f"精度: {precision:.4f}") print(f"召回率: {recall:.4f}") print(f"F1 分数: {f1:.4f}")