from keras.models import Sequential from keras.layers import Dense from sklearn.preprocessing import MinMaxScaler import numpy as np from sklearn.model_selection import train_test_split # 加载数据集,18列数据 dataset = np.loadtxt(r'D:\python-learn\asd.csv', delimiter=",",skiprows=1) # 划分数据, 使用17列数据来预测最后一列 X = dataset[:,0:17] y = dataset[:,17] # 归一化 scaler = MinMaxScaler(feature_range=(0, 1)) X = scaler.fit_transform(X) y = scaler.fit_transform(y.reshape(-1, 1)) # 将数据集分为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 创建模型 model = Sequential() model.add(Dense(64, input_dim=17, activation='relu')) model.add(Dense(32, activation='relu')) model.add(Dense(16, activation='relu')) model.add(Dense(8, activation='relu')) model.add(Dense(1, activation='linear')) # 编译模型, 选择MSE作为损失函数 model.compile(loss='mse', optimizer='adam') # 训练模型, 迭代1000次 model.fit(X_train, y_train, epochs=300, batch_size=32) score= model.evaluate(X_train, y_train) print('Test loss:', score) # 评估神经网络模型 score= model.evaluate(X_test,y_test) print('Test loss:', score) # 预测结果 dataset = np.loadtxt(r'D:\python-learn\testdata.csv', delimiter=",",skiprows=1) X = dataset[:,0:17] scaler = MinMaxScaler(feature_range=(0, 1)) X = scaler.fit_transform(X) y = scaler.fit_transform(y.reshape(-1, 1)) # pred_Y = model.predict(X) print("Predicted value:", pred_Y) from sklearn.metrics import mean_squared_error, r2_score # y_true是真实值,y_pred是预测值 # 计算均方误差 y_true = dataset[:,-1] mse = mean_squared_error(y_true, pred_Y) # 计算决定系数 r2 = r2_score(y_true, pred_Y) # 输出均方误差和决定系数 print("均方误差: %.2f" % mse) print("决定系数: %.2f" % r2) import matplotlib.pyplot as plt plt.scatter(y_true, pred_Y) # 添加x轴标签 plt.xlabel('真实值') # 添加y轴标签 plt.ylabel('预测值') # 添加图标题 plt.title('真实值与预测值的散点图') # 显示图像 plt.show()请你优化一下这段代码,尤其是归一化和反归一化过程
时间: 2024-03-10 21:47:27 浏览: 117
tf.keras_分类模块_CNN-深度可分离.ipynb_nose8eu_keras_CNN_tf.keras_分离卷积_
好的,我可以为您提供一些优化建议。首先,在归一化和反归一化过程中,应该使用同一个 `MinMaxScaler` 对象,而不是分别创建两个对象进行处理。其次,在训练模型之前,可以将数据集划分为训练集、验证集和测试集,以更好地评估模型的性能。最后,在预测时应该使用 `model.predict()` 方法获得预测结果,而不是注释掉该行代码。
下面是优化后的代码:
```python
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt
# 加载数据集,18列数据
dataset = np.loadtxt(r'D:\python-learn\asd.csv', delimiter=",", skiprows=1)
# 划分数据, 使用17列数据来预测最后一列
X = dataset[:, 0:17]
y = dataset[:, 17]
# 归一化
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
y = scaler.fit_transform(y.reshape(-1, 1))
# 将数据集分为训练集、验证集和测试集
X_train, X_valtest, y_train, y_valtest = train_test_split(X, y, test_size=0.2, random_state=0)
X_val, X_test, y_val, y_test = train_test_split(X_valtest, y_valtest, test_size=0.5, random_state=0)
# 创建模型
model = Sequential()
model.add(Dense(64, input_dim=17, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='linear'))
# 编译模型, 选择MSE作为损失函数
model.compile(loss='mse', optimizer='adam')
# 训练模型, 迭代1000次
model.fit(X_train, y_train, epochs=300, batch_size=32, validation_data=(X_val, y_val))
# 评估神经网络模型
train_loss = model.evaluate(X_train, y_train)
val_loss = model.evaluate(X_val, y_val)
test_loss = model.evaluate(X_test, y_test)
print('Train loss:', train_loss)
print('Validation loss:', val_loss)
print('Test loss:', test_loss)
# 预测结果
dataset = np.loadtxt(r'D:\python-learn\testdata.csv', delimiter=",", skiprows=1)
X = dataset[:, 0:17]
X = scaler.transform(X)
y_true = dataset[:, -1]
pred_Y = model.predict(X)
# 反归一化
pred_Y = scaler.inverse_transform(pred_Y)
# 计算均方误差和决定系数
mse = mean_squared_error(y_true, pred_Y)
r2 = r2_score(y_true, pred_Y)
# 输出均方误差和决定系数
print("均方误差: %.2f" % mse)
print("决定系数: %.2f" % r2)
# 绘制散点图
plt.scatter(y_true, pred_Y)
# 添加x轴标签
plt.xlabel('真实值')
# 添加y轴标签
plt.ylabel('预测值')
# 添加图标题
plt.title('真实值与预测值的散点图')
# 显示图像
plt.show()
```
优化后的代码中,将数据集按 6:2:2 的比例划分为训练集、验证集和测试集,并且在训练模型时设置了验证集参数,以便在训练过程中评估模型的性能。在预测时,使用 `model.predict()` 方法获得预测结果,然后再进行反归一化处理。最后,计算均方误差和决定系数,并绘制真实值与预测值的散点图。
阅读全文