from sklearn import datasets
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import numpy as np
# (1)加载iris数据集,并留出1/3作为测试集
iris = datasets.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)使用训练集训练支持向量机—SMO分类算法
svm_model = SVC()
svm_model.fit(X_train, y_train)
# (3)使用五折交叉验证对模型性能进行评估
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(svm_model, X_train, y_train, cv=skf, scoring='accuracy')
precision = cross_val_score(svm_model, X_train, y_train, cv=skf, scoring='precision_macro')
recall = cross_val_score(svm_model, X_train, y_train, cv=skf, scoring='recall_macro')
f1 = cross_val_score(svm_model, X_train, y_train, cv=skf, scoring='f1_macro')
# 打印交叉验证结果
print("训练集准确度: {:.3f}".format(np.mean(scores)))
print("训练集精确度: {:.3f}".format(np.mean(precision)))
print("训练集召回率: {:.3f}".format(np.mean(recall)))
print("训练集F1值: {:.3f}".format(np.mean(f1)))
# (4)使用测试集测试模型性能
y_pred = svm_model.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("测试集准确度: {:.3f}".format(accuracy))
print("测试集精确度: {:.3f}".format(precision))
print("测试集召回率: {:.3f}".format(recall))
print("测试集F1值: {:.3f}".format(f1))