导入数据用模糊神经网络预测三分类并画出loss曲线,accuracy曲线和auc曲线python代码
时间: 2023-07-15 18:13:52 浏览: 82
FNN预测基于模糊神经网络FNN实现数据预测附Python代码和数据集.zip
5星 · 资源好评率100%
以下是基于Python的模糊神经网络预测三分类并绘制loss曲线、accuracy曲线和auc曲线的代码示例:
```python
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt
# 导入数据
data = np.loadtxt("data.csv", delimiter=",")
X = data[:, 0:-1]
y = data[:, -1]
# 分割数据集
train_X = X[:int(0.7*len(X))]
train_y = y[:int(0.7*len(y))]
test_X = X[int(0.7*len(X)):]
test_y = y[int(0.7*len(y)):]
# 创建模糊神经网络模型
model = Sequential()
model.add(Dense(10, input_dim=X.shape[1], activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])
# 训练模型
history = model.fit(train_X, train_y, validation_data=(test_X, test_y), epochs=50, batch_size=32)
# 绘制loss曲线
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# 绘制accuracy曲线
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
# 绘制auc曲线
probas_ = model.predict_proba(test_X)
fpr, tpr, thresholds = roc_curve(test_y, probas_[:, 1])
roc_auc = auc(fpr, tpr)
plt.plot(fpr, tpr, lw=1, label='ROC (area = %0.2f)' % (roc_auc))
plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Random')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
其中,我们使用了Keras库来创建和训练模糊神经网络模型,并使用Scikit-learn库中的roc_curve和auc函数来计算和绘制AUC曲线。最后,我们使用Matplotlib库来绘制loss曲线、accuracy曲线和AUC曲线。
阅读全文