第二章 感知机的python实现

xiaoxiao2021-02-28  44

感知机模型

$$d = \frac{1}{{\left| w \right|}}|w \bullet {x_i} + b|$$

$$L = - \sum\limits_N {{y_i}} (w \bullet {x_i} + b)$$

$$\frac{{dL}}{{dw}} = - \sum\limits_N {{y_i}} {x_i}$$

$$w = w + \eta {y_i}{x_i}$$

预测

def predict(x, w, b): return sign(np.dot(w, x) + b)

小数据训练

dim = 2 #属性数量 dataSize = 10 #数据集大小 learning_rate = 0.1 #学习率 ITERATE = 1000 #迭代次数 x_train = np.array([[-1, 1], [-2, 0], [-1, 0], [-0.5, 0.5], [0, 0.5],[1, 3], [2, 3], [1, 1], [1, -0.5], [1, 0]]) x_train = x_train.reshape(10, dim, 1) y_train = np.array([1, 1, 1, 1, 1, -1, -1, -1, -1, -1]) # print(x_train.shape) # print(x_train[0].shape) w = np.zeros((1, dim)) b = 0 assert(x_train.shape == (dataSize, dim, 1)) assert(x_train[0].shape == (dim, 1)) assert(w.shape == (1, dim)) for x in range(ITERATE): params = svg(x_train, y_train, w, b, learning_rate) w = params['w'] b = params['b'] print(w) print(b)

数据生成器

def getData(rg, dim, size): # w = np.random.rand(1, dim) # b = np.random.randint(-rg/2, rg/2) w = np.array([1, 1]) b = 2.5 x = [] y = [] for i in range(size): x_i = np.random.rand(dim, 1) * rg - rg/2 y_i = -1 if np.dot(w, x_i) + b > 0: y_i = 1 x.append(x_i) y.append(y_i) x = np.array(x) y = np.array(y) # print("getData") # print(x) data = {"x":x, "y":y} return data

大数据测试

rangeOfNumber = 10 #随机数的范围 dim = 2 #属性数量 dataSize = 1000 #数据集大小 testSize = 2000 #测试集大小 learning_rate = 0.05 #学习率 ITERATE = 1000 #迭代次数 data_train = getData(rangeOfNumber, dim, dataSize) x_train = data_train["x"] y_train = data_train["y"] # print(x_train.shape) # print(x_train[0].shape) w = np.zeros((1, dim)) b = 0 assert(x_train.shape == (dataSize, dim, 1)) assert(x_train[0].shape == (dim, 1)) assert(w.shape == (1, dim)) for x in range(ITERATE): params = svg(x_train, y_train, w, b, learning_rate) w = params['w'] b = params['b'] print(w) print(b)

对测试集预测

data_test = getData(rangeOfNumber, dim, testSize) x_test = data_test["x"] y_test = data_test["y"] y_predict = [] for i in range(testSize): y_predict.append(predict(x_test[i], w, b)) cnt = 0 for i in range(testSize): if y_test[i] == y_predict[i]: cnt = cnt + 1 print("Accuracy:%d" % (cnt / testSize * 100))