感知机是神经网络是神经网络和支持向量机的基础,本章介绍了感知机的模型、学习策略、损失函数、学习算法、以及对偶形式等。

3.代码
python实现
import numpy as np import matplotlib.pyplot as plt class Myperceptron: def __init__(self): self.w = None self.b = 0 self.l_rate = 1 def fit(self,x_train,y_train): #梯度下降算法 self.w = np.zeros(x_train.shape[1]) i = 0 while i < x_train.shape[0]: x = x_train[i] y = y_train[i] if y*(np.dot(self.w,x)+self.b) <= 0: self.w = self.w + self.l_rate * np.dot(y,x) self.b = self.b + self.l_rate * y i = 0 else: i += 1 def draw(X, w, b): # 生成超平面上的两点 X_new = np.array([[0], [6]]) y_predict = (-b - (w[0] * X_new)) / w[1] # 绘制训练数据集的散点图 plt.plot(X[:2, 0], X[:2, 1], "g*", label="1") plt.plot(X[2:, 0], X[2:, 1], "rx", label="-1") # 绘制分离超平面,超平面即为wx+b=0的点 plt.plot(X_new, y_predict, "b-") plt.axis([0, 6, 0, 6]) plt.xlabel(‘x1‘) plt.ylabel(‘x2‘) # 显示图例 plt.legend() plt.show() def main(): #数据集 x_train = np.array([[3,3],[4,3],[1,1]]) y_train = np.array([1,1,-1]) #构建感知机对象并训练 perceptron = Myperceptron() perceptron.fit(x_train,y_train) #绘制结果 draw(x_train,perceptron.w,perceptron.b) if __name__ == "__main__": main()
sklearn实现
from sklearn.linear_model import Perceptron import numpy as np x_train = np.array([[3, 3], [4, 3], [1, 1]]) y = np.array([1, 1, -1]) perceptron = Perceptron() perceptron.fit(x_train, y) print("w:", perceptron.coef_, "\n", "b:", perceptron.intercept_, "\n", "n_iter:", perceptron.n_iter_) res = perceptron.score(x_train, y) print("correct rate:{:.0%}".format(res))
ps:本文实对书《统计学习方法》的学习总结
原文:https://www.cnblogs.com/helloandhey/p/10741230.html