感知机
\(f=\cfrac{1}{||w||}|w*x_0+b|\\L(w.b)=-y_i(w*x_i+b)\)
import numpy as np
from sklearn.datasets import make_blobs
x,y = make_blobs(100,2,centers=2,random_state=42)
y = np.where(y == 0, -1, y)
import matplotlib.pyplot as plt
plt.scatter(x[:,0],x[:,1],c=y)
plt.xlabel("x1")
plt.ylabel("x2")
plt.show()

w = [0,0]
b = 0
n = 0.1
for i in range(5000):
for x_item,y_item in zip(x,y):
# print(np.array(x_item)*2)
r = -y_item*(np.dot(w, x_item)+b)
if (r >= 0).any():
w = w + np.array(x_item)*y_item*n
b = b + n*y_item
print(w)
print(b)
[ 0.87113019 -0.58016554]
0.0
x1 = np.linspace(-10, 10, 10)
y1 = (-w[0] * x1 - b) / w[1]
plt.plot(x1, y1, color='red')
plt.scatter(x[:, 0], x[:, 1], c=y)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Classification')
plt.show()


浙公网安备 33010602011771号