划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1055) # 建立神经网络模型 model = Sequential() model.add(Dense(units=6, activation='relu', input_dim=X_train.shape[1])) model.add(Dense(units=6, activation='relu')) model.add(Dense(units=1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(X_train, y_train, epochs=100, batch_size=32) # 在测试集上评估模型 loss, accuracy = model.evaluate(X_test, y_test) print('Test accuracy:', accuracy)在此基础上Acc值、Rrecision值、Recall值、PR曲线、F1值、TPR值、FPR值、ROC曲线、AUC值,给出代码
时间: 2024-03-03 10:51:57 浏览: 60
以下是计算指标和绘制曲线的代码:
```
# 预测结果
y_pred = model.predict(X_test)
y_pred = (y_pred > 0.5)
# 计算指标
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_curve, auc, confusion_matrix
acc = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(fpr, tpr)
# 绘制 ROC 曲线
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
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()
# 计算 TPR 和 FPR
tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
tpr = tp / (tp + fn)
fpr = fp / (fp + tn)
print('Accuracy:', acc)
print('Precision:', precision)
print('Recall:', recall)
print('F1 score:', f1)
print('TPR:', tpr)
print('FPR:', fpr)
# 计算 AUC 值和绘制 PR 曲线
precision, recall, thresholds = precision_recall_curve(y_test, y_pred)
auc_score = auc(recall, precision)
print('AUC:', auc_score)
plt.figure(figsize=(8, 6))
plt.plot(recall, precision, color='navy', lw=2, label='PR curve (area = %0.2f)' % auc_score)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall curve')
plt.legend(loc="lower right")
plt.show()
```
这里计算的 TPR 是 Recall,FPR 是 1 - Specificity。注意,ROC 曲线的横轴是 FPR,纵轴是 TPR。
阅读全文