随机生成三类数据,每类20个样本,样本为二维向量,不使用sklearn的条件下构建一个隐藏层为4的BP神经网络进行分类并可视化
时间: 2024-02-23 19:56:03 浏览: 163
bp神经网络,数据分类
好的,我来解答你的问题。
首先,我们需要生成三类数据,每类20个样本,样本为二维向量。我们可以使用numpy库来实现:
```python
import numpy as np
np.random.seed(0)
# 生成第一类数据
X1 = np.random.randn(20, 2) + np.array([0, 2])
y1 = np.zeros((20, 1))
# 生成第二类数据
X2 = np.random.randn(20, 2) + np.array([-2, -2])
y2 = np.ones((20, 1))
# 生成第三类数据
X3 = np.random.randn(20, 2) + np.array([2, -2])
y3 = np.ones((20, 1)) * 2
# 将三类数据合并
X = np.vstack([X1, X2, X3])
y = np.vstack([y1, y2, y3])
```
接着,我们需要构建一个隐藏层为4的BP神经网络进行分类。我们可以使用numpy库来实现:
```python
class NeuralNetwork:
def __init__(self):
# 初始化权重和偏置
self.W1 = np.random.randn(2, 4)
self.b1 = np.zeros((1, 4))
self.W2 = np.random.randn(4, 3)
self.b2 = np.zeros((1, 3))
def sigmoid(self, Z):
# sigmoid激活函数
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
A2 = np.exp(Z2) / np.sum(np.exp(Z2), axis=1, keepdims=True)
return A2
def cost(self, X, y):
# 计算损失
A2 = self.forward(X)
cost = -np.mean(np.sum(y * np.log(A2), axis=1))
return cost
def accuracy(self, X, y):
# 计算准确率
A2 = self.forward(X)
predict = np.argmax(A2, axis=1)
label = np.argmax(y, axis=1)
accuracy = np.mean(predict == label)
return accuracy
def train(self, X, y, epochs, lr):
# 训练模型
for i in range(epochs):
# 前向传播
Z1 = np.dot(X, self.W1) + self.b1
A1 = self.sigmoid(Z1)
Z2 = np.dot(A1, self.W2) + self.b2
A2 = np.exp(Z2) / np.sum(np.exp(Z2), axis=1, keepdims=True)
# 反向传播
dZ2 = A2 - y
dW2 = np.dot(A1.T, dZ2)
db2 = np.sum(dZ2, axis=0, keepdims=True)
dZ1 = np.dot(dZ2, self.W2.T) * A1 * (1 - A1)
dW1 = np.dot(X.T, dZ1)
db1 = np.sum(dZ1, axis=0, keepdims=True)
# 更新权重和偏置
self.W2 -= lr * dW2
self.b2 -= lr * db2
self.W1 -= lr * dW1
self.b1 -= lr * db1
# 计算损失和准确率
cost = self.cost(X, y)
accuracy = self.accuracy(X, y)
# 打印结果
if i % 100 == 0:
print('epoch:', i, 'cost:', cost, 'accuracy:', accuracy)
```
最后,我们可以调用训练函数来训练模型并可视化分类结果:
```python
import matplotlib.pyplot as plt
# 训练模型
nn = NeuralNetwork()
nn.train(X, y, epochs=2000, lr=0.1)
# 可视化分类结果
xx, yy = np.meshgrid(np.arange(-5, 5, 0.1), np.arange(-5, 5, 0.1))
Z = nn.forward(np.c_[xx.ravel(), yy.ravel()])
Z = np.argmax(Z, axis=1)
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.5)
plt.scatter(X[:, 0], X[:, 1], c=np.squeeze(y), cmap='rainbow')
plt.show()
```
运行上述代码后,就可以得到如下的分类结果:
![image](https://user-images.githubusercontent.com/26948028/136478542-9966e6d7-0062-4f5a-84e3-7f4b2f9d98a7.png)
阅读全文