美文网首页
python实现感知机

python实现感知机

作者: 司马山哥 | 来源:发表于2020-08-08 18:50 被阅读0次

    简单对照统计学习上的感知机理论,基于numpy实现感知机学习模型

    1代码

    import pandas as pd
    import numpy as np
    from matplotlib import pyplot as plt
    
    
    def fit(x,y,max_iter=100, tol=1e-3, eta0=0.1):
        y = y.reshape(-1, 1)
        if x.shape[0]!=y.shape[0] or y.shape[1]!=1:
            raise ValueError
    
        iter_count = 0
        w = np.ones(shape=(x.shape[1],1))
        b = 0
        loss = (np.dot(x, w) + b) * y
        loss_function_sum = -1 * np.sum(loss[np.where(loss < 0)])
        error_lst = list(np.where(loss < 0)[0])
    
        while (iter_count<max_iter and loss_function_sum>tol and len(error_lst)>0):
            loss = (np.dot(x, w) + b) * y
            loss_function_sum = -1*np.sum(loss[np.where(loss<0)])
            iter_count += 1
            error_lst = list(np.where(loss < 0)[0])
    
            if len(error_lst)>0:
                error_x = x[error_lst[0]]
                error_y = y[error_lst[0]]
    
                w_step = (eta0 * error_x * error_y).reshape(-1, 1)
                b_step = (eta0*error_y)[0]
                w += w_step
                b += b_step
                b = round(b,6)
            else:
                continue
        return w,b
    
    if __name__ == '__main__':
        filePath = r'D:\statisticsLearn\perception\data'
        data = pd.read_csv(filePath + r'\sample.csv')
        data['y'] = data['y'].apply(lambda x:2*x-1)
    
        x = data[['x1','x2']].values
        y = data['y'].values
    
        # 训练数据和测试数据
        x_data_train = x[:80, :]
        x_data_test = x[80:, :]
        y_data_train = y[:80]
        y_data_test = y[80:]
    
        # 正例和反例
        positive_x1 = [x[i, 0] for i in range(100) if y[i] == 1]
        positive_x2 = [x[i, 1] for i in range(100) if y[i] == 1]
        negetive_x1 = [x[i, 0] for i in range(100) if y[i] == -1]
        negetive_x2 = [x[i, 1] for i in range(100) if y[i] == -1]
    
        coef = fit(x,y,max_iter=1000,eta0=1)
    
        plt.scatter(positive_x1, positive_x2, c='red')
        plt.scatter(negetive_x1, negetive_x2, c='blue')
        # 画出超平面(在本例中即是一条直线)
        line_x = np.arange(-4, 4)
        line_y = line_x * (coef[0][0] / coef[0][1]) - coef[1]
        plt.plot(line_x, line_y)
        plt.show()
    
    

    2与sklearn比较

    Figure_1.png

    相关文章

      网友评论

          本文标题:python实现感知机

          本文链接:https://www.haomeiwen.com/subject/gaghdktx.html