导入数据用模糊神经网络预测三分类并画出loss曲线,accuracy曲线和auc曲线python代码
时间: 2023-07-15 18:13:49 浏览: 97
下面是一个基本的模糊神经网络预测三分类的示例代码,包括导入数据、模型构建、训练和绘制曲线:
```
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc
import skfuzzy as fuzz
import matplotlib.pyplot as plt
# 导入数据
data = pd.read_csv('data.csv')
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 数据预处理
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 模糊化输入数据
X_train_t = X_train.T
X_train_fuzzy = []
for i in range(X_train_t.shape[0]):
fuzzy = fuzz.trimf(X_train_t[i], [np.min(X_train_t[i]), np.mean(X_train_t[i]), np.max(X_train_t[i])])
X_train_fuzzy.append(fuzzy)
X_train_fuzzy = np.array(X_train_fuzzy).T
# 构建模型
input_vars = []
for i in range(X_train.shape[1]):
ivar = 'x' + str(i+1)
input_vars.append(fuzz.Antecedent(np.arange(np.min(X_train[:, i]), np.max(X_train[:, i]), 0.01), ivar))
output_vars = fuzz.Classifier(fuzz.argmax, np.arange(3))
rules = []
for i in range(X_train.shape[1]):
ivar = input_vars[i]
ivar['low'] = np.min(X_train[:, i])
ivar['high'] = np.max(X_train[:, i])
ivar.automf(3)
for j in range(3):
rvar = output_vars[j]
rvar.automf(3)
rule = fuzz.Rule(ivar[0] & ivar[1] & ivar[2] & ivar[3] & ivar[4] & ivar[5] & ivar[6] & ivar[7] & ivar[8] & ivar[9], rvar[j])
rules.append(rule)
model = fuzz.ControlSystem(rules)
prediction = fuzz.ControlSystemSimulation(model)
# 训练模型
epoch = 200
loss = []
accuracy = []
for i in range(epoch):
for j in range(X_train_fuzzy.shape[0]):
prediction.input['x1'] = X_train_fuzzy[j][0]
prediction.input['x2'] = X_train_fuzzy[j][1]
prediction.input['x3'] = X_train_fuzzy[j][2]
prediction.input['x4'] = X_train_fuzzy[j][3]
prediction.input['x5'] = X_train_fuzzy[j][4]
prediction.input['x6'] = X_train_fuzzy[j][5]
prediction.input['x7'] = X_train_fuzzy[j][6]
prediction.input['x8'] = X_train_fuzzy[j][7]
prediction.input['x9'] = X_train_fuzzy[j][8]
prediction.input['x10'] = X_train_fuzzy[j][9]
prediction.compute()
if prediction.output['class'] != y_train[j]:
model.control_rules[prediction.rule].weight = 0.95
model.control_rules[prediction.rule].consequent[prediction.output['class']] = 0.05
model.control_rules[prediction.rule].consequent[y_train[j]] = 0.95
y_pred = []
y_prob = []
for j in range(X_test.shape[0]):
prediction.input['x1'] = fuzz.interp_membership(input_vars[0].universe, input_vars[0].mf, X_test[j][0])
prediction.input['x2'] = fuzz.interp_membership(input_vars[1].universe, input_vars[1].mf, X_test[j][1])
prediction.input['x3'] = fuzz.interp_membership(input_vars[2].universe, input_vars[2].mf, X_test[j][2])
prediction.input['x4'] = fuzz.interp_membership(input_vars[3].universe, input_vars[3].mf, X_test[j][3])
prediction.input['x5'] = fuzz.interp_membership(input_vars[4].universe, input_vars[4].mf, X_test[j][4])
prediction.input['x6'] = fuzz.interp_membership(input_vars[5].universe, input_vars[5].mf, X_test[j][5])
prediction.input['x7'] = fuzz.interp_membership(input_vars[6].universe, input_vars[6].mf, X_test[j][6])
prediction.input['x8'] = fuzz.interp_membership(input_vars[7].universe, input_vars[7].mf, X_test[j][7])
prediction.input['x9'] = fuzz.interp_membership(input_vars[8].universe, input_vars[8].mf, X_test[j][8])
prediction.input['x10'] = fuzz.interp_membership(input_vars[9].universe, input_vars[9].mf, X_test[j][9])
prediction.compute()
y_pred.append(prediction.output['class'])
y_prob.append([prediction.output['class' + str(k)] for k in range(3)])
y_pred = np.array(y_pred)
y_prob = np.array(y_prob)
loss.append(np.sum(np.abs(y_pred - y_test)) / y_test.shape[0])
accuracy.append(np.sum(y_pred == y_test) / y_test.shape[0])
# 绘制曲线
plt.plot(loss, label='loss')
plt.plot(accuracy, label='accuracy')
fpr, tpr, thresholds = roc_curve(y_test, y_prob[:, 1])
plt.plot(fpr, tpr, label='auc')
plt.legend()
plt.show()
```
其中,数据文件`data.csv`应该包含训练和测试数据,每行是一个样本(包括输入和输出),最后一列是输出类别。模糊化输入数据是为了增加模型的鲁棒性,使其能够更好地处理噪声和不确定性。模型构建部分包括构造输入和输出变量、定义规则和控制系统,并使用模糊控制模型进行预测。模型训练部分是一个简单的迭代过程,其中每个样本都用于更新模型的权重。最后,绘制三种曲线:损失、准确度和AUC。
阅读全文