感知机python实现

文章目录

  • 思想:错误驱动
  • 模型
  • 损失函数
  • 梯度下降
  • 代码实现
    • 训练集
    • 实验结果

思想:错误驱动

模型

f ( x ) = s i g n ( w T x ) s i g n ( a ) = { 1 , if  a > 0 0 , if  a < 0 f(x)=sign(w^{T}x)\\ sign(a)= \begin{cases}1,& \text {if $a>0$} \\0, & \text{if $a<0$}\end{cases} f(x)=sign(wTx)sign(a)={1,0,if a>0if a<0

损失函数

L ( w ) = ∑ i = 1 m I { y i w T x i < 0 } L ( w ) = ∑ x i ∈ D − y i w T x i L(w)=\sum_{i=1}^{m}I\{y_{i}w^{T}x_{i}<0\}\\ L(w)=\sum_{x_{i}\in D}-y_{i}w^{T}x_{i}\\ L(w)=i=1mI{yiwTxi<0}L(w)=xiDyiwTxi

梯度下降

w j = w j − α L ′ ( w ) 即 w j = w j + α ∑ x i ∈ D y i x i w_{j}=w_{j}-\alpha L^{'}(w)\\ 即w_{j}=w_{j}+\alpha \sum_{x_{i}\in D}y_{i}x_{i}\\ wj=wjαL(w)wj=wj+αxiDyixi

代码实现

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号


#导入数据
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target

#
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
df.label.value_counts()

data = np.array(df.iloc[:100, [0, 1, -1]])
X, y = data[:,:-1], data[:,-1]
#将标签记作-1和1
y = np.array([1 if i == 1 else -1 for i in y])


# 数据线性可分,二分类数据
# 此处为一元一次线性方程
class Model:
    '''
    w:权重
    b:偏置
    l_rate:学习率
    '''
    def __init__(self):
        self.w = np.ones(len(data[0]) - 1, dtype=np.float32)
        self.b = 0
        self.l_rate = 0.1

    # 定义感知机的模型
    def sign(self, x, w, b):
        y = np.dot(x, w) + b
        return y

    # 随机梯度下降法
    def fit(self, X_train, y_train):
        flag = False
        while not flag:
            flag = True
            for i in range(len(X_train)):
                if y_train[i] * self.sign(X_train[i], self.w, self.b) < 0:
                    self.w = self.w + self.l_rate * np.dot(X_train[i], y_train[i])
                    self.b = self.b + self.l_rate * y_train[i]
                    flag = False
                    break

perceptron = Model()
perceptron.fit(X, y)
# 画出分类线及样本点的散状图
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
fig = plt.figure()
x_points = np.linspace(4, 7, 10)#4-7取10个点
y_ = -(perceptron.b + perceptron.w[0] * x_points) / perceptron.w[1]
plt.plot(x_points, y_,'red')

right = plt.scatter(X[:, 0][y == 1], X[:, 1][y == 1])
false = plt.scatter(X[:, 0][y == -1], X[:, 1][y == -1], marker='^')

plt.xlabel('花萼长度')
plt.ylabel('花萼宽度')

plt.legend([right,false], ['0', '1'])
plt.show()

训练集

感知机python实现_第1张图片

实验结果

感知机python实现_第2张图片
# b站up主链接

你可能感兴趣的:(python,深度学习)