首页 > 其他 > 详细

统计学习_感知机

时间:2019-04-20 16:07:44      阅读:93      评论:0      收藏:0      [点我收藏+]

1.概述:

感知机是神经网络是神经网络和支持向量机的基础,本章介绍了感知机的模型、学习策略、损失函数、学习算法、以及对偶形式等。

2.思维导图:

技术分享图片

3.代码

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):   #梯度下降算法
        self.w = np.zeros(x_train.shape[1])
        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 += 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")
    # 绘制分离超平面,超平面即为wx+b=0的点
    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)
    #绘制结果
    draw(x_train,perceptron.w,perceptron.b)

if __name__ == "__main__":
    main()

sklearn实现

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

 

ps:本文实对书《统计学习方法》的学习总结

 

统计学习_感知机

原文:https://www.cnblogs.com/helloandhey/p/10741230.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!