脚本宝典收集整理的这篇文章主要介绍了【机器学习代码实现】 感知机,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
算法
代码
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))
测试结果
以上是脚本宝典为你收集整理的【机器学习代码实现】 感知机全部内容,希望文章能够帮你解决【机器学习代码实现】 感知机所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。