给定两个类别的一共8个样本,通过感知机算法求解判别函数
def bi_perception(): X1 = np.array([ [0, 0, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0] ]) X2 = np.array([ [0, 0, 1], [0, 1, 1], [0, 1, 0], [1, 1, 1] ]) # 增广 X1 = np.concatenate((X1, np.ones((len(X1), 1))), axis=1) X2 = np.concatenate((X2, np.ones((len(X2), 1))), axis=1) X1 = np.concatenate((X1, -X2), axis=0) C = 1 w = np.zeros(X1.shape[1]) # w = np.array([-1., -1., 1., 1.]) cnt = 0 ind = 0 np.random.shuffle(X1) while cnt < len(X1): cnt = 0 for dat in X1: print(dat, '·', w, '=', np.matmul(dat, w)) if np.matmul(dat, w) <= 0: w += C*dat else: cnt += 1 ind += 1 print("迭代次数:", ind) print("return w=", w)
算得:
迭代次数: 3
return w= [ 2. -2. -2. 1.]
即$d(x)=2x_1-2x_2-2x_3+1$
给定三个类别的各1个样本,通过感知机算法求解判别函数
def tri_perception(): X = np.array([ [-1, -1], [0, 0], [1, 1] ]) X = np.concatenate((X, np.ones((3, 1))), axis=1) w = np.zeros((3, X.shape[1])) c = 1 ind = 0 cnt = 0 while cnt < len(X)-1: print(w) tmp = np.zeros(len(X)) lind = ind % 3 for i in range(len(tmp)): tmp[i] = c*np.matmul(w[i], X[lind]) for i in range(len(tmp)): tmp[i] = tmp[lind]-tmp[i] if tmp[i] > 0 and i != lind: cnt += 1 if cnt == len(X)-1: continue for i in range(len(X)): if i == lind: w[i] += X[lind] else: w[i] -= X[lind] cnt = 0 ind += 1 print(w)
算得:
W=[[-2. -2. -1.]
[ 0. 0. -1.]
[ 2. 2. -1.]]
即
$d_1(x)=-2x_1-2x_2-1$
$d_2(x)=-1$
$d_3(x)=2x_1+2x_2-1$
标签:tmp,cnt,lind,模式识别,判别函数,len,感知机,np,X1 From: https://www.cnblogs.com/zhaoke271828/p/17004009.html