第二课时

Week1任务简介:学习第2章感知机,理解感知机模型解决的问题,模型形式、学习策略和求解算法。学习时长:Day4-Day5详细说明:第2章讲了在数据线性可分的情况下的感知机模型。通过阅读第1节,理解感知机模型的基本思想和模型形式;通过阅读第2节了解感知机模型采用的损失函数的形式及含义;第3节描述了感知机模型对应的优化问题的原始形式和对偶形式,请大家学习原始形式对应的随机梯度算法及算法的收敛性,对偶形式不做要求。学习目标:
1、导读视频
2、掌握感知机的模型形式、损失函数及对应的优化问题
3、掌握随机梯度下降算法原理
4、理解感知机模型中随机梯度算法的收敛性

作业2:
1.思考感知机模型假设空间是什么?模型复杂度体现在哪里?打卡进行文字说明。
2.已知训练数据集D,其正实例点是x1=(3,3)T,x2=(4,3)T,负实例点是x3=(1,1)T:
(1) 用python 自编程实现感知机模型,对训练数据集进行分类,并对比误分类点选择次序不同对最终结果的影响。可采用函数式编程或面向对象的编程。
(2)试调用sklearn.linear_model 的Perceptron模块,对训练数据集进行分类,并对比不同学习率η对模型学习速度及结果的影响。(3)附加题:    对比传统感知机算法及其对偶形式的运行速度。作业答案及代码讲解在本周六公布,助教会进行视频讲解。打卡要求:打卡代码运行结果的截图(文字20字,图片1张)
1.

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:,0], "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.

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


from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
import numpy as np

X_train = np.array([[3, 3], [4, 3], [1, 1]])
y = np.array([1, 1, -1])
#perceptron=Perceptron(penalty="l2",alpha=0.01,eta0=1,max_iter=50,tol=1e-3)
#perceptron=Perceptron()
perceptron=SGDClassifier(loss="perceptron",eta0=1, learning_rate="constant", penalty=None)
perceptron.fit(X_train,y)
print(perceptron.coef_)
print(perceptron.intercept_)
print(perceptron.n_iter_)
X=np.array([[2,2]])
y=perceptron.predict(X)

猜你喜欢

转载自blog.csdn.net/qq_36227329/article/details/89229533