python纯手工实现BP网络对鸢尾花数据集进行分类
时间: 2023-08-17 15:22:58 浏览: 104
好的,下面是一个简单的纯手工实现BP网络对鸢尾花数据集进行分类的示例代码:
```python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载鸢尾花数据集
iris = load_iris()
X, y = iris.data, iris.target
# 将标签转为独热编码
n_classes = len(np.unique(y))
y_one_hot = np.eye(n_classes)[y]
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y_one_hot, test_size=0.2, random_state=42)
# 定义BP网络的类
class NeuralNetwork:
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)
self.b1 = np.zeros(self.hidden_size)
self.W2 = np.random.randn(self.hidden_size, self.output_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.a2 = self.sigmoid(self.z2)
return self.a2
def backward(self, X, y, output):
# 反向传播
self.error = y - output
self.delta2 = self.error * self.sigmoid_derivative(output)
self.a1.T.dot(self.delta2)
self.delta1 = self.delta2.dot(self.W2.T) * self.sigmoid_derivative(self.a1)
self.W1 += X.T.dot(self.delta1)
self.b1 += np.sum(self.delta1, axis=0)
self.W2 += self.a1.T.dot(self.delta2)
self.b2 += np.sum(self.delta2, axis=0)
def train(self, X, y):
output = self.forward(X)
self.backward(X, y, output)
def predict(self, X):
# 预测
output = self.forward(X)
return np.argmax(output, axis=1)
# 训练BP网络
input_size = X_train.shape[1]
hidden_size = 5
output_size = n_classes
nn = NeuralNetwork(input_size, hidden_size, output_size)
for i in range(1000):
nn.train(X_train, y_train)
# 在测试集上进行预测
y_pred = nn.predict(X_test)
accuracy = np.mean(y_pred == np.argmax(y_test, axis=1))
print("Accuracy:", accuracy)
```
这个示例代码中,我们首先使用 `load_iris` 函数加载鸢尾花数据集,并将标签转为独热编码。然后使用 `train_test_split` 函数将数据集分割为训练集和测试集。
接下来定义了一个名为 `NeuralNetwork` 的类,该类包含初始化权重和偏置、前向传播、反向传播、训练和预测等方法。在训练过程中,我们使用了随机梯度下降算法来更新权重和偏置。
最后,在测试集上进行预测,并计算准确率。
阅读全文