您的位置:首页 > 编程语言

《统计学习方法》笔记之感知机模型及代码实现

2020-03-05 17:25 85 查看

思考题

1.感知机模型假设空间是什么?模型复杂度体现在哪里?
感知机模型的假设空间是分离超平面w⋅x+b=0w\cdot{x}+b=0w⋅x+b=0
模型的复杂度主要体现在x(x(1),x(2),x(3),…,x(d))x(x^{(1)},x^{(2)},x^{(3)},…,x^{(d)})x(x(1),x(2),x(3),…,x(d))的特征数量,也就是维度d上。
2.已知训练数据集D,其正实例点是x1=(3,3)T,x2=(4,3)T,x_{1}=(3,3)^{T},x_{2}=(4,3)^{T},x1​=(3,3)T,x2​=(4,3)T,负实例点是x3=(1,1)Tx_{3}=(1,1)^{T}x3​=(1,1)T:
(1) 用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):
#用样本点的特征数更新初始w,如x1=(3,3)T,有两个特征,则self.w=[0,0]
self.w=np.zeros(X_train.shape[1])
i=0
while i<X_train.shape[0]:
X=X_train[i]
y=y_train[i]
# 如果y*(wx+b)≤0 说明是误判点,更新w,b
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")
#绘制分离超平面
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)
print(perceptron.w)
print(perceptron.b)
# 结果图像绘制
draw(X_train,perceptron.w,perceptron.b)

if __name__=="__main__":
main()

误分类点选择的顺序会影响最后超平面的确定。
(2)试调用sklearn.linear_model 的Perceptron模块,对训练数据集进行分类,并对比不同学习率η\etaη对模型学习速度及结果的影响。


代码

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()
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))

运行结果

w: [[ 1.  0.]]
b: [-2.]
n_iter: 5
corret rate:100%

将学习率η\etaη设置为0.5

perceptron=Perceptron(eta0=0.5)

运行结果

w: [[ 0.5  0. ]]
b: [-1.]
n_iter: 5
correct rate:100%

当w,bw,bw,b的初始值为0时,学习率η\etaη的取值对最终结果是没有影响的。
(3)对比传统感知机算法及其对偶形式的运行速度。
维度d较大用对偶形式,样本N较大用原始形式。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
小陈今天学习了吗 发布了2 篇原创文章 · 获赞 0 · 访问量 34 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: