算法
代码
import numpy as np def perceptron_training(X, y, learning_rate, max_iter_nums): iter_nums = 0 [N, n] = X.shape w, b= np.zeros(n), 0 while (iter_nums < max_iter_nums): no_mistake = True for i in range(N): if y[i] * (np.dot(w, X[i]) + b) <= 0: w += learning_rate * y[i] * X[i] b += learning_rate * y[i] no_mistake = False if no_mistake == True: break ++iter_nums return w, b def test(X, y, w, b): N = len(X) no_mistake = True for i in range(N): if y[i] * (np.dot(w, X[i]) + b) <= 0: return False return True # test: positive point: x1 = (3, 3), x2 = (4, 3); negative point: x3 = (1, 1) X_test = np.array([[3, 3], [4, 3], [1, 1]]) y_test = np.array([1, 1, -1]) w, b = perceptron_training(X_test, y_test, 1, 30) print("w: ", w) print("b: ", b) print("after traning: ", test(X_test, y_test, w, b))
测试结果