使用python写一个使用Xavier初始化、正则化方法和MSGD的BP网络(8个输入,1个输出,5个隐藏节点),并写一个main实现将训练集投入网络训练,训练后将测试集投入得出预测输出。训练和测试数据均存在同一个xlsx文件的同一个页面表格中,而训练集和测试集存于名为"NEW.xlsx"的文件中,试以第2行至第3001行作为训练集,之后的作为测试集;且训练集和测试集的输入均在[9, 10, 11, 12, 13, 14, 16, 17]列中,输出为倒数第二列[-2]。 。在main模块中实现绘制R2图来说明模型的拟合度,并且输出MAE、MSE、预测值和真实值之间的相对误差平均百分比,绘制各输入输出的拟合折线图,且R2图和折线图中使用的数据均为未变换数据。
时间: 2023-12-10 14:38:47 浏览: 62
抱歉,由于这是一个较为复杂的任务,无法在一次回答中详细解决。我将提供一些思路和代码示例,希望可以帮助您完成此任务。
首先,需要安装一些必要的库,如pandas、numpy、matplotlib等:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
```
接下来,读取数据并将其分为训练集和测试集:
```python
data = pd.read_excel('NEW.xlsx', sheet_name='Sheet1')
train_data = data.iloc[1:3001, [9, 10, 11, 12, 13, 14, 16, 17, -2]]
test_data = data.iloc[3001:, [9, 10, 11, 12, 13, 14, 16, 17, -2]]
```
然后,需要对数据进行归一化处理。这里使用min-max归一化方法:
```python
def min_max_normalize(data):
return (data - np.min(data)) / (np.max(data) - np.min(data))
train_data_norm = min_max_normalize(train_data)
test_data_norm = min_max_normalize(test_data)
```
接下来,需要定义网络结构和相关参数,并进行Xavier初始化:
```python
input_size = 8
hidden_size = 5
output_size = 1
learning_rate = 0.1
epochs = 100
# Xavier initialization
weights_input_hidden = np.random.randn(input_size, hidden_size) * np.sqrt(1/input_size)
weights_hidden_output = np.random.randn(hidden_size, output_size) * np.sqrt(1/hidden_size)
bias_hidden = np.zeros((1, hidden_size))
bias_output = np.zeros((1, output_size))
```
定义激活函数和损失函数:
```python
# ReLU activation function
def relu(x):
return np.maximum(0, x)
# Mean Squared Error loss function
def mse_loss(y_pred, y_true):
return np.mean((y_pred - y_true)**2)
```
定义训练函数和预测函数:
```python
def train(X, y, weights_input_hidden, weights_hidden_output, bias_hidden, bias_output, learning_rate, epochs):
for i in range(epochs):
# Forward propagation
hidden_layer = np.dot(X, weights_input_hidden) + bias_hidden
hidden_layer_activation = relu(hidden_layer)
output_layer = np.dot(hidden_layer_activation, weights_hidden_output) + bias_output
# Backpropagation
error = output_layer - y
d_output = error / len(X)
d_weights_hidden_output = np.dot(hidden_layer_activation.T, d_output)
d_bias_output = np.sum(d_output, axis=0, keepdims=True)
d_hidden = np.dot(d_output, weights_hidden_output.T)
d_hidden[hidden_layer <= 0] = 0
d_weights_input_hidden = np.dot(X.T, d_hidden)
d_bias_hidden = np.sum(d_hidden, axis=0, keepdims=True)
# Update weights and biases
weights_input_hidden -= learning_rate * d_weights_input_hidden
weights_hidden_output -= learning_rate * d_weights_hidden_output
bias_hidden -= learning_rate * d_bias_hidden
bias_output -= learning_rate * d_bias_output
# Print MSE loss every 10 epochs
if i % 10 == 0:
loss = mse_loss(output_layer, y)
print(f"Epoch {i}, Loss: {loss:.6f}")
return weights_input_hidden, weights_hidden_output, bias_hidden, bias_output
def predict(X, weights_input_hidden, weights_hidden_output, bias_hidden, bias_output):
hidden_layer = np.dot(X, weights_input_hidden) + bias_hidden
hidden_layer_activation = relu(hidden_layer)
output_layer = np.dot(hidden_layer_activation, weights_hidden_output) + bias_output
return output_layer
```
使用训练集进行训练:
```python
X_train = train_data_norm.iloc[:, :-1].values
y_train = train_data_norm.iloc[:, -1:].values
weights_input_hidden, weights_hidden_output, bias_hidden, bias_output = train(X_train, y_train, weights_input_hidden, weights_hidden_output, bias_hidden, bias_output, learning_rate, epochs)
```
使用测试集进行预测,并计算各项指标:
```python
X_test = test_data_norm.iloc[:, :-1].values
y_test = test_data_norm.iloc[:, -1:].values
y_pred = predict(X_test, weights_input_hidden, weights_hidden_output, bias_hidden, bias_output)
# Transform normalized data back to original scale
y_test_orig = test_data.iloc[:, -2:].values
y_pred_orig = y_pred * (np.max(y_test_orig) - np.min(y_test_orig)) + np.min(y_test_orig)
# Calculate R2 score
r2_score = 1 - np.sum((y_test_orig - y_pred_orig)**2) / np.sum((y_test_orig - np.mean(y_test_orig))**2)
print(f"R2 score: {r2_score:.6f}")
# Calculate MAE and MSE
mae = np.mean(np.abs(y_test_orig - y_pred_orig))
mse = np.mean((y_test_orig - y_pred_orig)**2)
print(f"MAE: {mae:.6f}")
print(f"MSE: {mse:.6f}")
# Calculate relative error percentage
rel_error = np.mean(np.abs((y_test_orig - y_pred_orig) / y_test_orig)) * 100
print(f"Relative error percentage: {rel_error:.6f}%")
```
绘制各输入输出的拟合折线图:
```python
fig, axs = plt.subplots(2, 4, figsize=(20,10))
axs = axs.flatten()
for i in range(8):
axs[i].scatter(X_test[:, i], y_test_orig, s=5)
axs[i].plot(X_test[:, i], y_pred_orig, color='red')
axs[i].set_xlabel(f'Input {i+1}')
axs[i].set_ylabel('Output')
plt.show()
```
绘制R2图:
```python
plt.scatter(y_test_orig, y_pred_orig)
plt.plot([np.min(y_test_orig), np.max(y_test_orig)], [np.min(y_test_orig), np.max(y_test_orig)], color='red')
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.title(f'R2 score: {r2_score:.6f}')
plt.show()
```
完整代码如下:
阅读全文