用深度神经网络训练mnist,输出Accuracy (ACC), Precision, Recall, F1 Score (F1),AUC,并画 ROC曲线python代码
时间: 2024-02-12 15:02:20 浏览: 199
Python利用全连接神经网络求解MNIST问题详解
好的,我会尽力回答您的问题!以下是用深度神经网络训练mnist,输出Accuracy (ACC), Precision, Recall, F1 Score (F1),AUC,并画 ROC曲线的Python代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc, confusion_matrix, classification_report
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import Adam
from keras.utils import np_utils
# 加载数据
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# 数据预处理
X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)
X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
# 创建模型
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation('softmax'))
# 编译模型
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history = model.fit(X_train, y_train, epochs=20, batch_size=128, verbose=1, validation_data=(X_test, y_test))
# 预测测试集
y_pred = model.predict(X_test)
# 获取混淆矩阵和分类报告
y_pred = np.argmax(y_pred, axis=1)
y_true = np.argmax(y_test, axis=1)
cm = confusion_matrix(y_true, y_pred)
cr = classification_report(y_true, y_pred, target_names=[str(i) for i in range(10)])
# 输出精度、召回率、F1值
tn, fp, fn, tp = cm.ravel()
precision = tp / (tp + fp)
recall = tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print('Precision:', precision)
print('Recall:', recall)
print('F1 Score:', f1)
# 计算AUC
fpr, tpr, thresholds = roc_curve(y_true, y_pred, pos_label=None)
roc_auc = auc(fpr, tpr)
print('AUC:', roc_auc)
# 画ROC曲线
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
```
这段代码首先加载MNIST数据集,并进行数据预处理,然后创建一个包含两个隐藏层的神经网络模型。我们使用Adam优化器进行模型训练,并输出模型精度、召回率、F1值和AUC。最后,我们使用sklearn库中的roc_curve和auc函数来计算和绘制ROC曲线。
希望能对您有所帮助!
阅读全文