赞
踩
训练数据为train.csv,预测数据为test.csv。
x1 | x2 | x3 | y |
0.05 | 0 | 186.25 | 9976.2 |
0.05 | 0 | 148.24 | 87299.5 |
0.05 | 0 | 129.23 | 359289.25 |
0.05 | 0 | 117.83 | 796450.33 |
0.05 | 4 | 137.7 | 10471.6 |
0.05 | 4 | 107.1 | 75978 |
0.05 | 4 | 87.98 | 424848.33 |
0.05 | 4 | 80.33 | 639609 |
0.05 | 6 | 124.7 | 9746.2 |
0.05 | 6 | 97.52 | 91818.25 |
0.05 | 6 | 82.17 | 297379.5 |
0.05 | 6 | 74.54 | 769776.5 |
0.05 | 8 | 115.93 | 9530.8 |
0.05 | 8 | 89.01 | 93123.75 |
0.05 | 8 | 73.82 | 303079.5 |
0.05 | 8 | 70.05 | 688099.33 |
10 | 0 | 205.82 | 10004.2 |
10 | 0 | 175.32 | 76471 |
10 | 0 | 160.08 | 410666 |
10 | 0 | 156.27 | 876261 |
10 | 4 | 149.89 | 13740.6 |
10 | 4 | 135.61 | 80659.67 |
10 | 4 | 124.9 | 395372 |
10 | 4 | 121.34 | 813718 |
10 | 6 | 138.61 | 14145.6 |
10 | 6 | 123.63 | 73092.5 |
10 | 6 | 112.39 | 380828 |
10 | 6 | 108.64 | 721834 |
10 | 8 | 128.66 | 9730.4 |
10 | 8 | 114.37 | 79006.25 |
10 | 8 | 103.65 | 362419 |
10 | 8 | 100.07 | 805459 |
x1 | x2 | x3 |
0.05 | 6 | 95 |
0.05 | 6 | 81 |
10 | 6 | 124 |
0.05 | 6 | 258 |
0.05 | 6 | 249 |
10 | 6 | 200 |
- # 导入库
- import numpy as np
- import pandas as pd
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- from sklearn.preprocessing import MinMaxScaler
- from tensorflow.keras.models import Sequential
- from tensorflow.keras.layers import Dense
- from tensorflow.keras.optimizers import Adam
- from tensorflow.keras import regularizers
-
- # 加载数据
- df = pd.read_csv('train.csv')
- x = df[['x1', 'x2', 'x3']].values
- y = df[['y']].values
-
- # 数据归一化
- x_scaler = MinMaxScaler(feature_range=(-1, 1))
- y_scaler = MinMaxScaler(feature_range=(-1, 1))
- x = x_scaler.fit_transform(x)
- y = y_scaler.fit_transform(y)
-
- # 定义神经网络模型
- model = Sequential()
- model.add(Dense(10, activation='relu', input_shape=(3,), kernel_regularizer=regularizers.l2(0.01)))
- model.add(Dense(16, activation='relu', kernel_regularizer=regularizers.l2(0.01)))
- model.add(Dense(1, activation='linear'))
-
- # 误差记录
- optimizer = Adam(lr=0.0001)
- model.compile(optimizer=optimizer, loss='mse')
-
- # 训练模型
- history = model.fit(x, y, epochs=50000, batch_size=67)
-
- # 评估模型
- mse = model.evaluate(x, y)
- print('Validation MSE:', mse)
-
- # 保存模型的权重和偏差
- model.save('my_model.h5')
-
- # 误差曲线
- # 设置中文显示和解决负号显示问题
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.rcParams['axes.unicode_minus'] = False
- plt.plot(history.history['loss'])
- plt.title("模型误差")
- plt.ylabel("误差")
- plt.xlabel("循环次数")
- plt.show()
-
- # 预测值输出
- y_pred = model.predict(x)
-
- # 预测值反归一化
- y = y_scaler.inverse_transform(y)
- y_pred = y_scaler.inverse_transform(y_pred)
- print("the prediction is:", y_pred)
-
- # 将预测值存储到Excel表中
- df_out = pd.DataFrame(y_pred, columns=['Prediction'])
- df_out.to_excel('prediction.xlsx', index=False)
-
- # 实际值与预测值的对比图
- # 设置中文显示和解决负号显示问题
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.rcParams['axes.unicode_minus'] = False
- plt.scatter(y, y_pred)
- plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
- plt.xlabel("实际值")
- plt.ylabel("预测值")
- plt.show()
训练神经网络模型一共四层,第一层为输入变量,后三层为Dense ,其中Dense中从参数可以调整:
model.add(Dense(10, activation='relu', input_shape=(3,), kernel_regularizer=regularizers.l2(0.01)))
此层神经元个数为10,激活函数为relu,并且进行了正则化处理。
- # 导入库
- import pandas as pd
- from sklearn.preprocessing import MinMaxScaler
- from tensorflow.keras.models import load_model
-
- # 数据预处理(归一化)
- df = pd.read_csv('train.csv')
- x = df[['x1', 'x2', 'x3']].values
- y = df[['y']].values
- x_scaler = MinMaxScaler(feature_range=(-1, 1))
- y_scaler = MinMaxScaler(feature_range=(-1, 1))
- x = x_scaler.fit_transform(x)
- y = y_scaler.fit_transform(y)
-
- # 加载预测数据
- df_test = pd.read_csv('test.csv')
- x_test = df_test[['x1', 'x2', 'x3']].values
-
- # 预测数据归一化
- x_test = x_scaler.transform(x_test)
-
- # 加载训练好的神经网络模型
- model = load_model('my_model.h5')
-
- # 对预测数据进行预测
- y_pred = model.predict(x_test)
- y_pred = y_scaler.inverse_transform(y_pred)
- print(y_pred)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。