感知机
取书本上的正例点(3,3),(4,3),负例点(1,1)
以下是感知机的python代码实现:
原始形式代码:
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[3, 3, 1], [4, 3, 1], [1, 1, -1]])
X = data[:, :2]
y = data[:, -1]
class Perceptron:
def __init__(self):
self.w = np.zeros(len(data[0])-1, dtype=np.float32)
self.b = 0
self.l_rate = 1 # 学习率
def sign(self, x, w, b):
y = np.dot(w, x)+b
if y >= 0:
return 1
else:
return -1
def sgd(self, X_train, y_train):
flag = False
while not flag:
wrong_count = 0
for i in range(len(X_train)):
X = X_train[i]
y = y