导入库
import numpy as np
import random
import matplotlib.pyplot as plt
数据集
x, y = [], []
for i in range(500):
r = random.randint(0, 100) * 0.1
delta = random.randint(-500, 500) * 0.1
if delta != 0:
x.append([r, r+delta])
if delta >= 0:
y.append(1)
else:
y.append(-1)
print(len(x))
print(len(y))
print(x[:5])
print(y[:5])
499
499
[[4.6000000000000005, 26.6], [9.700000000000001, -18.4], [4.4, 13.9], [0.6000000000000001, -33.9], [3.6, 35.4]]
[1, -1, 1, -1, 1]
x_pos, x_neg = [], []
y_pos, y_neg = [], []
for a, b in x:
if a < b:
x_pos.append(a)
y_pos.append(b)
else:
x_neg.append(a)
y_neg.append(b)
plt.scatter(x_pos, y_pos)
plt.scatter(x_neg, y_neg)
plt.show()

模型
class Perceptron(object):
def __init__(self, dim, lr):
self.w = np.zeros(dim)
self.b = 0
self.lr = lr
def train(self, x, y):
if isinstance(x, list):
x = np.array(x)
if isinstance(y, list):
y = np.array(y)
for _ in range(10):
for i in range(x.shape[0]):
if y[i] * (np.sum(self.w * x[i]) + b) <= 0:
self.w += self.lr * y[i] * x[i]
self.b += self.lr * y[i]
return self.w, self.b
训练
x = np.array(x)
y = np.array(y)
perceptron = Perceptron(2, 0.5)
w, b = perceptron.train(x, y)
print(w, b)
[-15.1 14.05] -6.0
结果展示
xl = np.linspace(0, 10, 200)
yl = (-b - w[0] * xl) / w[1]
plt.scatter(x_pos, y_pos)
plt.scatter(x_neg, y_neg)
plt.plot(xl, yl)
plt.show()
