统计学习方法训练营第一次作业:感知机

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

使用自编程实现:

import numpy as np
import matplotlib.pyplot as plt

class MyPerceptron:
    def __init(self):
        self.w = None #x维度未知
        self.b = 0
        self.l_rate = 1
        
    def fit(self,X_train,y_train):
        self.w = np.zeros(X_train.shape[1]) #用样本点特征数更新初始值w
        self.b = 0
        self.l_rate = 1#不加这两句报错了AttributeError: 'MyPerceptron' object has no attribute 'b'
        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 = 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()

使用sklearn中的perceptron模块:

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

借鉴了训练营公布的答案,希望有机会可以继续完善。

你可能感兴趣的:(训练营作业)