使用python创建一个使用归一化、Xavier初始化、正则化方法和MSGD的BP网络,该网络有8个输入1个输出1层隐藏层,要求分为两个模块(py文件,一个BPNN.py,一个main.py),且该BP网络可以实现在训练后,将同样8个输入1个输出的测试集进行预测得出预测输出值。 而训练集和测试集存于名为"NEW.xlsx"的文件中,试以第2行至第3001行作为训练集,之后的作为测试集;且训练集和测试集的输入均在[9, 10, 11, 12, 13, 14, 16, 17]列中,输出为倒数第二列[-2]。 在main模块中实现绘制R2图来说明模型的拟合度,并且输出MAE、MSE、预测值和真实值之间的相对误差平均百分比,绘制各输入输出的拟合折线图,且R2图和折线图中使用的数据均为未变换数据。
时间: 2023-11-27 15:51:09 浏览: 67
很高兴为您解答,以下是您所需的代码:
BPNN.py文件:
```
import numpy as np
import pandas as pd
class BPNN:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.W1 = np.random.randn(self.input_size, self.hidden_size) * np.sqrt(1. / self.input_size)
self.b1 = np.zeros(self.hidden_size)
self.W2 = np.random.randn(self.hidden_size, self.output_size) * np.sqrt(1. / self.hidden_size)
self.b2 = np.zeros(self.output_size)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.output = self.sigmoid(self.z2)
return self.output
def backward(self, X, y, output, learning_rate):
error = output - y
delta_output = error * self.sigmoid_derivative(output)
error_hidden = delta_output.dot(self.W2.T)
delta_hidden = error_hidden * self.sigmoid_derivative(self.a1)
self.W2 -= self.a1.T.dot(delta_output) * learning_rate
self.b2 -= np.sum(delta_output, axis=0, keepdims=True) * learning_rate
self.W1 -= X.T.dot(delta_hidden) * learning_rate
self.b1 -= np.sum(delta_hidden, axis=0) * learning_rate
def train(self, X, y, learning_rate=0.1, epochs=1000):
for i in range(epochs):
output = self.forward(X)
self.backward(X, y, output, learning_rate)
def predict(self, X):
return self.forward(X)
```
main.py文件:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from BPNN import BPNN
# 读取数据
data = pd.read_excel('NEW.xlsx')
train_data = data.iloc[1:3001, [9, 10, 11, 12, 13, 14, 16, 17, -2]].values
test_data = data.iloc[3001:, [9, 10, 11, 12, 13, 14, 16, 17, -2]].values
# 数据归一化
scaler = MinMaxScaler()
scaler.fit(train_data[:, :-1])
train_data[:, :-1] = scaler.transform(train_data[:, :-1])
test_data[:, :-1] = scaler.transform(test_data[:, :-1])
# 划分输入和输出
X_train, y_train = train_data[:, :-1], train_data[:, -1]
X_test, y_test = test_data[:, :-1], test_data[:, -1]
# 初始化BP神经网络
input_size = X_train.shape[1]
hidden_size = 10
output_size = 1
bpnn = BPNN(input_size, hidden_size, output_size)
# 训练BP神经网络
bpnn.train(X_train, y_train, learning_rate=0.1, epochs=1000)
# 预测测试集
y_pred = bpnn.predict(X_test)
# 反归一化
y_pred = scaler.inverse_transform(np.concatenate([X_test, y_pred.reshape(-1, 1)], axis=1))[:, -1]
y_test = scaler.inverse_transform(np.concatenate([X_test, y_test.reshape(-1, 1)], axis=1))[:, -1]
# 计算MAE和MSE
mae = np.mean(np.abs(y_pred - y_test))
mse = np.mean((y_pred - y_test) ** 2)
# 计算相对误差平均百分比
relative_error = np.abs((y_pred - y_test) / y_test).mean() * 100
# 计算R2值
ss_res = np.sum((y_test - y_pred) ** 2)
ss_tot = np.sum((y_test - np.mean(y_test)) ** 2)
r2 = 1 - ss_res / ss_tot
# 输出结果
print('MAE:', mae)
print('MSE:', mse)
print('Relative Error:', relative_error)
print('R2:', r2)
# 绘制R2图
plt.scatter(y_test, y_pred)
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], 'k--', lw=2)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.show()
# 绘制拟合折线图
for i in range(X_test.shape[1]):
plt.scatter(X_test[:, i], y_test, label='True Values')
plt.scatter(X_test[:, i], y_pred, label='Predictions')
plt.xlabel('Input {}'.format(i+1))
plt.ylabel('Output')
plt.legend()
plt.show()
```
在运行main.py前,请确保已经安装了以下库:
- numpy
- pandas
- matplotlib
- scikit-learn
另外,需要将NEW.xlsx文件放在与main.py和BPNN.py同一目录下。
阅读全文