统计学习方法读书笔记4-感知机课后习题

it2023-05-09  72

文章目录

1.课本课后习题2.视频课后习题(1)自编程实现(2)sklearn形式(3)感知机

1.课本课后习题

2.视频课后习题

(1)自编程实现

# 感知机自编程实现-面向对象 import numpy as np import matplotlib.pyplot as plt # 构建感知机类 class Myperceptron(): # 属性初始化 def __init__(self): # 不知道x_train的shape self.w = None self.b = 0 self.η = 1 # 核心方法实现-随机梯度下降 def fit(self,x_train,y): m,n = np.shape(x_train) # w的dim1与x的特征值相对应 # x.T:[2,3] w : [1,2] self.w = np.zeros((1,n)) i = 0 # for 与 while的区别 while i < m: # 获取训练集的x,y xi = x_train[i] yi = y[i] # 误判点的特征为:-yi*(w*xi+b) >= 0,如果为误判点,则w、b更新 if -1 * yi * (np.dot(self.w,xi.T) + self.b) >= 0: self.w = self.w + self.η * np.dot(yi,xi) self.b = self.b + self.η * yi # 如果为误判点,重头检测 i = 0 else: i += 1 # 当i=m时,while循环结束 def draw(x,w,b): # 生产分离超平面的两个点 x_new = np.array([[0], [6]]) # 公式变形 # y_predict = -(w[0]*x_new + b) / w[1] y_predict = -(b + w[0][0] * x_new) / w[0][1] plt.plot(x[:2,0],x[:2,1],'b*',label='1') plt.plot(x[-1,0],x[-1,1],'rx',label='-1') # 绘制分离超平面 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 = np.array([1,1,-1]) # 构建感知机对象,对数据进行训练 perceptron = Myperceptron() perceptron.fit(x_train,y) print(perceptron.w) # 结果图像绘制 draw(x_train,perceptron.w,perceptron.b) if __name__ == '__main__': main()

(2)sklearn形式

L1正则化:特征值更稀疏 L2正则化:权值更均匀 w,b初始值为0时,w、b均为eta0的整数倍, 误分类点的判定条件为yi(w*xi + b) <= 0,此时eta0的影响为0 所以不同的学习速率对模型学习速度和结果无影响 当w,b不为0时,有影响 # sklean实现感知机 import numpy as np from sklearn.linear_model import Perceptron # 训练数据集 x_train = np.array([[3,3],[4,3],[1,1]]) y = np.array([1,1,-1]) perceptron = Perceptron() perceptron.fit(x_train,y) # attribute print('w:',perceptron.coef_) print('b:',perceptron.intercept_) print('n_iter:',perceptron.n_iter_) # 测试模型预测的准确率 res = perceptron.score(x_train,y) print('correct rate : %d'%res) w: [[1. 0.]] b: [-2.] n_iter: 9 correct rate : 1

(3)感知机

感知机模型的假设空间是分离超平面wx+b=0 模型的复杂度主要体现在x的特征数量上面,也就是维度d上
最新回复(0)