上机实验五:BP 神经网络算法实现与测试

from sklearn import datasets
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.neural_network import MLPClassifier
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)使用训练集训练BP神经网络分类算法
mlp = MLPClassifier(hidden_layer_sizes=(100,), max_iter=1000, alpha=1e-4,
solver='sgd', verbose=10, random_state=42, tol=1e-4)
mlp.fit(X_train, y_train)

# (3)使用五折交叉验证对模型性能进行评估
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(mlp, X_train, y_train, cv=skf, scoring='accuracy')
precision = cross_val_score(mlp, X_train, y_train, cv=skf, scoring='precision_macro')
recall = cross_val_score(mlp, X_train, y_train, cv=skf, scoring='recall_macro')
f1 = cross_val_score(mlp, 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 = mlp.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))
posted @ 2024-12-06 10:34  不会JAVA的小袁  阅读(13)  评论(0编辑  收藏  举报