感知机简单python实现

参考链接

from functools import reduce from itertools import tee class Perceptron: def __init__(self,input_num,activator): self.activator = activator self.weights = [0.0 for _ in range(input_num)] self.bias = 0.0 def __str__(self): return 'weights\t:%s\nbias\t:%f\n' % (self.weights, self.bias) def predict(self, input_vec): return self.activator(\ reduce(lambda a,b:a+b,\ map(lambda x_w:x_w[0]*x_w[1], \ zip(input_vec,self.weights)) \ ,0.0)+self.bias) def _update_weight(self,input_vec,output,label,rate): delta = label - output self.weights = list(map( lambda x_w:x_w[1]+rate*delta*x_w[0], zip(input_vec,self.weights))) self.bias += rate * delta def _one_iteration(self,input_vecs,labels,rate): samples = zip(input_vecs,labels) #print(list(samples)) for(input_vec,label) in samples: output = self.predict(input_vec) self._update_weight(input_vec,output,label,rate) def train(self,input_vecs,labels,iteration,rate): for i in range(iteration): self._one_iteration(input_vecs,labels,rate) def and_f(x): return 1 if x>0 else 0 if __name__ == '__main__': x = [[0,0],[0,1],[1,0],[1,1]] y = [0,0,0,1] and_perception = Perceptron(2,and_f) and_perception.train(x,y,10,0.1) print(and_perception) print('1 and 1 = %d'%and_perception.predict([1,1])) print('0 and 0 = %d'%and_perception.predict([0,0])) print('1 and 0 = %d'%and_perception.predict([1,0])) print('0 and 1 = %d'%and_perception.predict([0,1]))

对应可视化

可视化

from matplotlib import pyplot from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D import numpy as np if __name__ == '__main__': # 训练数据 x = np.array([[0,0,0],[1,1,1],[1,1,0],[1,0,1],[0,1,1]]) y = [1,0,0,0,0] and_perception = Perceptron(3,and_f) and_perception.train(x,y,10,0.1) print(and_perception) fig = pyplot.figure() pyplot.xlim(0, 1) pyplot.ylim(0, 1) ax = Axes3D(fig) ax.set_zlim3d(0,1) length = len(y) for i in range(length): if y[i]==1: ax.scatter(x[0:,0:1][i],x[0:,1:2][i],x[0:,2:3][i], c = 'r', marker='*') else: ax.scatter(x[0:,0:1][i],x[0:,1:2][i],x[0:,2:3][i], c = 'b', marker='*') x2 = np.linspace(0,1,10) y2 = np.linspace(0,1,10) X,Y = np.meshgrid(x2,y2) Z = (and_perception.weights[0]*X + and_perception.weights[1]*Y + + and_perception.bias)/(-and_perception.weights[2]) for idx1, zz in enumerate(Z): for idx2, zzz in enumerate(zz): if Z[idx1][idx2]<0 : Z[idx1][idx2] = and_perception.bias/(-and_perception.weights[2])#Z[idx1][idx2] if Z[idx1][idx2]>0 else np.nan X[idx1][idx2] = 0 Y[idx1][idx2] = 0 surf = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,cmap = 'viridis', edgecolor = 'none',vmin=np.nanmin(Z), vmax=np.nanmax(Z)) #使用模型对100个随机三维点进行分类 test = np.random.randint(0,1000,(100,3))/1000 for t in test: if and_perception.predict(t)==1: ax.scatter(t[0],t[1],t[2], c = 'r', marker='*') else: ax.scatter(t[0],t[1],t[2], c = 'b', marker='*') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') pyplot.show()