使用Numpy实现与运算感知器 发表于 2019-10-12 | 更新于 2021-02-19 | 分类于 ml , python 背景说明参考PHP实现感知器 实现代码12345678910111213141516171819202122232425262728293031import numpy as npdata = np.array([ [1, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 0],], dtype=np.int)bias = np.ones((4, 1))data = np.concatenate((bias, data), 1) # 偏置值作为输入def predict(x, w): """ 预测结果 """ x = x.reshape(x.shape[0], 1) # 输入 w = w.reshape(1, w.shape[0]) # 权重 return 1 if w.dot(x)[0][0] > 0 else 0rate = 0.009 # 学习率weight = np.zeros(data.shape[1] - 1) # 初始权重for _ in range(10): for _, item in enumerate(data): t = item[-1] # Label d = item[0: -1] # Data weight = weight + rate * (t - predict(d, weight)) * dprint(weight)