bp神经网络预测python代码
时间: 2024-12-31 19:46:55 浏览: 20
### BP神经网络 Python 实现预测示例代码
为了展示如何使用Python实现BP神经网络并进行预测,下面提供了一个完整的代码实例。此代码实现了具有单个隐藏层的前馈神经网络,并展示了数据预处理、模型构建、训练以及最终的预测过程。
#### 数据准备与初始化
首先导入必要的库,并设置超参数:
```python
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 定义一些神经网络的参数[^4]
num_iterations = 10000 # 迭代次数
lr = 0.01 # 学习率
train_batch_size = 32 # 训练批次大小
early_stopping_patience = 5 # 提前停止耐心度
```
#### 构建BP神经网络类
接着定义一个简单的BP神经网络类,该类包含了正向传播和反向传播的方法:
```python
class SimpleNN:
def __init__(self, input_dim, hidden_units=8, output_dim=1):
self.W1 = np.random.randn(input_dim, hidden_units)
self.b1 = np.zeros((1, hidden_units))
self.W2 = np.random.randn(hidden_units, output_dim)
self.b2 = np.zeros((1, output_dim))
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def forward(self, X):
Z1 = np.dot(X, self.W1) + self.b1
A1 = self.sigmoid(Z1)
Z2 = np.dot(A1, self.W2) + self.b2
pred = self.sigmoid(Z2)
return {'Z1': Z1, 'A1': A1, 'Z2': Z2, 'pred': pred}
def backward(self, cache, X, y):
m = X.shape[0]
dZ2 = cache['pred'] - y
dW2 = np.dot(cache['A1'].T, dZ2) / m
db2 = np.sum(dZ2, axis=0, keepdims=True) / m
dA1 = np.dot(dZ2, self.W2.T)
dZ1 = dA1 * cache['A1'] * (1-cache['A1'])
dW1 = np.dot(X.T, dZ1) / m
db1 = np.sum(dZ1, axis=0, keepdims=True) / m
grads = {"dW1": dW1,
"db1": db1,
"dW2": dW2,
"db2": db2}
return grads
def update_parameters(self, grads, learning_rate=0.01):
self.W1 -= learning_rate * grads["dW1"]
self.b1 -= learning_rate * grads["db1"]
self.W2 -= learning_rate * grads["dW2"]
self.b2 -= learning_rate * grads["db2"]
def compute_loss(self, y_true, y_pred):
loss = -(y_true*np.log(y_pred)+(1-y_true)*np.log(1-y_pred)).mean()
return loss
```
#### 加载与预处理数据集
假设有一个二分类问题的数据集`X_data`, `y_labels`:
```python
scaler = StandardScaler()
def preprocess_data(X_raw, y_raw):
X_scaled = scaler.fit_transform(X_raw)
# 将标签转换成one-hot编码形式(如果需要)
y_onehot = (y_raw[:, None] == np.unique(y_raw).reshape([1, -1])).astype(int)
return X_scaled, y_onehot.flatten() if len(np.unique(y_raw)) == 2 else y_onehot
X_train, X_val, y_train, y_val = train_test_split(
X_data, y_labels, test_size=0.2, random_state=42)
X_train_processed, y_train_processed = preprocess_data(X_train, y_train)
X_val_processed, _ = preprocess_data(X_val, y_val)
```
#### 模型训练循环
最后,在主程序中调用上述组件完成整个流程:
```python
nn_model = SimpleNN(input_dim=X_train_processed.shape[1], hidden_units=8, output_dim=y_train_processed.shape[-1])
for i in range(num_iterations):
indices = np.random.choice(len(X_train_processed), size=train_batch_size, replace=False)
batch_X = X_train_processed[indices]
batch_y = y_train_processed[indices][:,None]
caches = nn_model.forward(batch_X)
preds = caches['pred']
cost = nn_model.compute_loss(batch_y, preds)
gradients = nn_model.backward(caches, batch_X, batch_y)
nn_model.update_parameters(gradients, lr)
if i % 1000 == 0 or i == num_iterations - 1:
val_preds = nn_model.forward(X_val_processed)['pred']
validation_cost = nn_model.compute_loss(y_val, val_preds)
print(f"Iteration {i}: Training Loss={cost:.4f}, Validation Loss={validation_cost:.4f}")
print('预测结果:')
predictions = nn_model.forward(X_val_processed)['pred']
print(predictions)
```
阅读全文