11.24

from sklearn.datasets import load_iris
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}")
posted @   liuxuechao  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示