感知机是一种二类分类的线性分类器,属于判别模型(另一种是生成模型)。简单地说,就是通过输入特征,利用超平面,将目标分为两类。感知机是神经网络和支持向量机的基础。现实过程如下:
1.感知器模型结构
此例模型的输入量有三个x1、x2和偏置量bias=1.0,W1、W2、W3为输入量的权重系数,f(x)为激活函数,用于判断样本类型,实际上是一个三值化的输入函数,如图所示。我们希望所有样本为1的类别,输出值为1,样本为-1的类别,输出值为-1.
2.感知器的算法
(1)初始权重值,如w=[0.75,0.6,0.5],本例中为随机生成。
(2)根据权重公式f(x)=x1*W1+x2*W2+bias*W3计算出输出值,如果输出值不符合预期,则对权重系数进行调整。
(3)权重参数调整:△Wi=λ(d-f(x))*xi
即将预期结果d与激活函数输出结果做差,得到误差值,由于误差是由于输入引起的,因此将输入xi与误差值的乘积做为误差调整量。为了避免因一次调整量过大,加入λ做为调整量的因子,也被称为学习率,一般是一个0.1左右的小数值,不宜取值太大。则新的权重值为:
W=Wi+△Wi
(4)重复(2)-(3)步骤,将训练所有样本。
(5)检验训练结果
3.Python感知器的实现
1 '''利用高斯白噪声生成基于某个直线附近的若干个点 2 y=wb+b 3 weight 直线权重 4 bias 直线偏置 5 size 点的个数 6 ''' 7 import numpy as np 8 9 10 def random_point_nearby_line(weight, bias, size=10): 11 x_point = np.linspace(-1, 1, size)[:, np.newaxis] 12 noise = np.random.normal(0, 0.5, x_point.shape) 13 y_point = weight * x_point + bias + noise 14 15 input_arr = np.hstack((x_point, y_point)) 16 return input_arr 17 18 19 # 直线的真正参数 20 real_weight = 1 21 real_bias = 3 22 size = 100 23 24 # 输入数据标签 25 # 生成输入的数据 26 input_point = random_point_nearby_line(real_weight, real_bias, size) 27 label = np.sign(input_point[:, 1] - (input_point[:, 0] * real_weight + real_bias)).reshape((size, 1)) 28 29 # 分割数据,15个为测试样本,其余为训练样本 30 from sklearn.model_selection import train_test_split 31 testSize = 15 32 x_train, x_test, y_train, y_test = train_test_split(input_point, label, test_size=testSize) 33 34 trainSize = size - testSize 35 36 37 # 初始化W,b 38 Weight = np.random.rand(2, 1) # 随机生成一个-1到1的数据 39 Bias = 0.5 # 初始化为0 40 41 42 def trainByStochasticGradient(input, output, x_test, y_test, test_size, input_num, train_num=1000, learning_rate=1): 43 global Weight, Bias 44 x = input 45 y = output 46 for rounds in range(train_num): 47 for i in range(input_num): 48 x1, x2 = x[i] 49 prediction = np.sign(Weight[0] * x1 + Weight[1] * x2 + Bias) 50 if y[i] * prediction <= 0: 51 Weight[0] = Weight[0] + learning_rate * y[i] * x1 52 Weight[1] = Weight[1] + learning_rate * y[i] * x2 53 Bias = Bias + learning_rate * y[i] 54 55 if rounds % 10 == 0: 56 learning_rate *= 0.9 57 accuracy = compute_accuracy(x_test, y_test, test_size, Weight, Bias) 58 print("迭代次数{},测试精度{}".format(rounds, accuracy)) 59 60 # 测试样本 61 62 63 def compute_accuracy(x_test, y_test, test_size, weight, bias): 64 x1, x2 = np.reshape(x_test[:, 0], (test_size, 1)), np.reshape(x_test[:, 1], (test_size, 1)) 65 prediction = np.sign(y_test * (x1 * weight[0] + x2 * weight[1] + bias)) 66 count = 0 67 68 for i in range(prediction.size): 69 if prediction[i] > 0: 70 count = count + 1 71 return (count + 0.0) / test_size 72 73 74 trainByStochasticGradient(x_train, y_train, x_test, y_test, testSize, 85, train_num=400, learning_rate=1) 75 76 # 绘制样本及超平面 77 import matplotlib.pyplot as plt 78 fig = plt.figure("感知机(二分类器)") 79 80 # fig.title = "感知机(二分类器)" 81 ax = fig.add_subplot(1, 1, 1) 82 for i in range(y_train.size): 83 if y_train[i] == 1: 84 ax.scatter(x_train[i, 0], x_train[i, 1], color='r') 85 else: 86 ax.scatter(x_train[i, 0], x_train[i, 1], color='b') 87 # 绘制超平面 88 x = [-1, 1] 89 y = [(-Bias + Weight[0]) / Weight[1], (-Bias - Weight[0]) / Weight[1]] 90 ax.plot(x, y, color='g') 91 plt.show()
4.运行结果
标签:Weight,point,python,感知机,train,test,input,size From: https://www.cnblogs.com/lingdian92/p/16905395.html