from sklearn.datasets import load_iris标签:score,macro,11.24,print,2f,train,test From: https://www.cnblogs.com/luoqingci/p/18638888
from sklearn.model_selection import train_test_split, cross_validate
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# 加载数据集并进行标准化
iris = load_iris()
X, y = iris.data, iris.target
scaler = StandardScaler().fit(X)
X_scaled = scaler.transform(X)
# 分割数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=1/3, stratify=y, random_state=42)
# 初始化BP神经网络模型,调整参数
clf = MLPClassifier(hidden_layer_sizes=(10,), max_iter=500, alpha=1e-4,
solver='sgd', random_state=1, learning_rate_init=.1)
# 使用五折交叉验证评估模型性能
scoring = ['accuracy', 'precision_macro', 'recall_macro', 'f1_macro']
scores = cross_validate(clf, X_train, y_train, cv=5, scoring=scoring)
# 输出交叉验证的平均得分
print("Cross-validation scores:")
for metric in scoring:
print(f"Mean {metric}: {scores['test_' + metric].mean():.2f}")
# 训练最终模型
clf.fit(X_train, y_train)
# 使用测试集评估最终模型性能
y_pred = clf.predict(X_test)
print("\nTest set performance:")
print(f"Accuracy: {accuracy_score(y_test, y_pred):.2f}")
print(f"Precision: {precision_score(y_test, y_pred, average='macro'):.2f}")
print(f"Recall: {recall_score(y_test, y_pred, average='macro'):.2f}")
print(f"F1 Score: {f1_score(y_test, y_pred, average='macro'):.2f}")