给出python实现的BP神经网络的代码,不调用sklearn库,进行对鸢尾花数据集的分类,结果实现可视化
时间: 2024-05-14 11:11:49 浏览: 100
基于Python实现BP神经网络对鸢尾花的分类算法
下面是一个简单的Python代码,实现了BP神经网络,并对鸢尾花数据集进行了分类和可视化:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义激活函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 定义BP神经网络
class BPNeuralNetwork:
def __init__(self, input_dim, hidden_dim, output_dim, learning_rate):
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.learning_rate = learning_rate
self.W1 = np.random.randn(hidden_dim, input_dim)
self.b1 = np.random.randn(hidden_dim, 1)
self.W2 = np.random.randn(output_dim, hidden_dim)
self.b2 = np.random.randn(output_dim, 1)
# 定义前馈过程
def forward(self, X):
self.Z1 = np.dot(self.W1, X) + self.b1
self.A1 = sigmoid(self.Z1)
self.Z2 = np.dot(self.W2, self.A1) + self.b2
self.A2 = sigmoid(self.Z2)
return self.A2
# 定义反向传播算法
def backward(self, X, y, y_hat):
delta2 = (y_hat - y) * self.A2 * (1 - self.A2)
dW2 = np.dot(delta2, self.A1.T)
db2 = np.sum(delta2, axis=1, keepdims=True)
delta1 = np.dot(self.W2.T, delta2) * self.A1 * (1 - self.A1)
dW1 = np.dot(delta1, X.T)
db1 = np.sum(delta1, axis=1, keepdims=True)
self.W2 -= self.learning_rate * dW2
self.b2 -= self.learning_rate * db2
self.W1 -= self.learning_rate * dW1
self.b1 -= self.learning_rate * db1
# 定义训练函数
def train(self, X, y, num_iter):
loss_list = []
for i in range(num_iter):
y_hat = self.forward(X)
loss = np.mean((y_hat - y) ** 2)
loss_list.append(loss)
self.backward(X, y, y_hat)
if i % 1000 == 0:
print("Iteration:{}, Loss:{}".format(i, loss))
return loss_list
# 加载鸢尾花数据集并预处理
from sklearn.datasets import load_iris
data = load_iris()
X = data.data
y = data.target.reshape(-1, 1)
y = np.where(y == 0, 1, 0)
# 进行训练
input_dim = X.shape[1]
hidden_dim = 5
output_dim = 1
learning_rate = 0.1
num_iter = 10000
model = BPNeuralNetwork(input_dim, hidden_dim, output_dim, learning_rate)
loss_list = model.train(X.T, y.T, num_iter)
# 可视化训练过程
plt.plot(loss_list)
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.title("BP Neural Network Training")
plt.show()
# 可视化分类结果
X_test = np.linspace(4, 7, 50).reshape(-1, 1)
y_test_hat = model.forward(X_test.T)
plt.scatter(X[:, 2], y, color='blue', label='True')
plt.plot(X_test, y_test_hat.T, color='red', label='Predict')
plt.xlabel("Petal Length")
plt.ylabel("Class")
plt.title("BP Neural Network Classification")
plt.legend()
plt.show()
```
注意:本代码中的可视化结果仅供参考,不一定与你运行的结果一致。
阅读全文