感知机算法
*
import random
import numpy as np
import matplotlib.pyplot as plt
class percePtron:
#初始化w向量和截距b,注意初始化为浮点类型
def __init__(self):
self.weight = [0.0, 0.0]
self.bias = 0.0
def draw(self, x1, x2, y1, y2):
plt.scatter(x1, y1, label="y = 1")
plt.scatter(x2, y2, label="y = -1")
plt.legend()
plt.xlabel('X1')
plt.ylabel('X2')
label_x = np.linspace(0, 4, 20)
# print(-(self.bias + self.weight[0] * label_x) / self.weight[1])
plt.plot(label_x, -(self.bias + self.weight[0] * label_x) / self.weight[1])
plt.show()
# learning_rate=0.1表示为步长为0.1,step表示为训练次数,data为样本集
def train(self, data, learning_rate=0.1, step=1000):
self.weight = np.array(self.weight)
for i in range(step):
randomindex = random.randint(0, len(data) - 1)
randomData = data[randomindex]
#如果y(wx + b) <= 0,则表明这个点为误分类点
if randomData[2] * (self.weight[0] * randomData[0] + self.weight[1] * randomData[1] + self.bias) <= 0:
#此处为梯度下降算法更新w和b
self.weight[0] = self.weight[0] + randomData[2] * randomData[0] * learning_rate
self.weight[1] = self.weight[1] + randomData[2] * randomData[1] * learning_rate
self.bias = self.bias + randomData[2] * learning_rate
print("weight : ", self.weight)
print("bias : ", self.bias)
if __name__ == "__main__":
data = [[1, 4, 1], [0.5, 2, 1], [2, 2.3, 1], [1, 0.5, -1], [2, 1, -1],
[4, 1, -1], [3.5, 4, 1], [3, 2.2, -1]]
x1 = []
y1 = []
x2 = []
y2 = []
for p in data:
if p[2] > 0:
x1.append(p[0])
y1.append(p[1])
else:
x2.append(p[0])
y2.append(p[1])
x1 = np.array(x1)
x2 = np.array(x2)
y1 = np.array(y1)
y2 = np.array(y2)
model = percePtron()
model.train(data)
model.draw(x1, x2, y1, y2)
*
运行前:

运行后:

哈哈,完美!
结束语:本人为机器学习初学者,若代码有误请大佬留言,本人会及时更正,谢谢!!!