李航统计学习方法-感知机python实现

  1、理论中原始算法的实现

# 利用Python实现感知机算法的原始形式
# -*- encoding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt


def createData():
    samples = np.array([[3, -3], [4, -3], [1, 1], [1, 2]])
    labels = [-1, -1, 1, 1]
    return samples, labels


class Perception:
    def __init__(self, x, y, a=1):
        self.x = x
        self.y = y
        self.l_rate = a
        self.w = np.zeros((x.shape[1], 1))
        self.b = 0
        self.numSimples = x.shape[0]
        self.numFeatures = x.shape[1]

    def sign(self, w, b, x):
        y = np.dot(x, w) + b  # x .w + b
        return int(y)

    def update(self, label_i, data_i):
        tmp = label_i * self.l_rate * data_i  # w = w + n yx
        tmp = tmp.reshape(self.w.shape)
        self.w = tmp + self.w
        self.b = self.b + label_i * self.l_rate  # b = b + n y

    def train(self):
        isFind = False
        while not isFind:
            count = 0
            for row in range(self.numSimples):
                simY = self.sign(self.w, self.b, self.x[row, :])
                if simY * self.y[row] <= 0:  # 如果是一个误分类实例点
                    print('误分类点为:', self.x[row, :], '此时的w和b为:', self.w, self.b)
                    count += 1
                    self.update(self.y[row], self.x[row])
            if count == 0:
                print('最终训练得到的w和b为:', self.w, self.b)
                isFind = True
        return self.w, self.b


class Picture:
    def __init__(self, data, w, b):
        self.w = w
        self.b = b
        plt.figure(1)
        plt.title("Perception Learning Algorithm", size=14)
        plt.xlabel("x0", size=14)
        plt.ylabel("x1", size=14)
        xData = np.linspace(0, 5, 100)
        yData = self.expression(xData)
        plt.plot(xData, yData, color='r', label='data')

        plt.scatter(data[0][0], data[0][1], s=50)
        plt.scatter(data[1][0], data[1][1], s=50)
        plt.scatter(data[2][0], data[2][1], s=50, marker='x')
        plt.scatter(data[3][0], data[3][1], s=50, marker='x')
        plt.savefig('2d_base.png', dpi=75)

    def expression(self, x):
        y = (-self.b - self.w[0] * x) / self.w[1] 
        # 注意在此,把x0,x1当做两个坐标轴,把x1当做自变量,x2为因变量
        return y

    def show_pic(self):
        plt.show()


if __name__ == '__main__':
    samples, labels = createData()
    myperceptron = Perception(x=samples, y=labels)
    weights, bias = myperceptron.train()
    Picture = Picture(samples, weights, bias)
    Picture.show_pic()

2、理论中对偶形式的实现:

 

# 利用Python实现感知机算法的对偶形式
# -*- encoding:utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt


# 1、 创建数据集
def createdata():
    samples = np.array([[3, -3], [4, -3], [1, 1], [1, 2]])
    labels = np.array([-1, -1, 1, 1])
    return samples, labels


class Perception:
    def __init__(self, x, y, a=1):
        self.x = x
        self.y = y
        self.w = np.zeros((1, x.shape[0]))
        self.b = 0
        self.a = 1  # 学习率
        self.numsamples = self.x.shape[0]
        self.numfeatures = self.x.shape[1]
        self.gMatrix = self.cal_gram(self.x)

    def cal_gram(self, x):
        gMatrix = np.zeros((self.numsamples, self.numsamples))
        for i in range(self.numsamples):
            for j in range(self.numsamples):
                gMatrix[i][j] = np.dot(self.x[i, :], self.x[j, :])
        return gMatrix

    def sign(self, w, b, key):
        y = np.dot(w * self.y, self.gMatrix[:, key]) + b    # αjYjXjXi + b
        return int(y)

    def update(self, i):
        self.w[:, i] = self.w[:, i] + self.a
        self.b = self.b + self.y[i] * self.a

    def cal_w(self):
        w = np.dot(self.w * self.y, self.x)
        return w

    def train(self):
        isFind = False
        while not isFind:
            count = 0
            for i in range(self.numsamples):
                tmpY = self.sign(self.w, self.b, i)
                if tmpY * self.y[i] <= 0:  # 如果是一个误分类实例点
                    print('误分类点为:', self.x[i, :], '此时的w和b为:', self.cal_w(), ',', self.b)
                    count += 1
                    self.update(i)
            if count == 0:
                print('最终训练得到的w和b为:', self.cal_w(), ',', self.b)
                isFind = True
        weights = self.cal_w()
        return weights, self.b


# 画图描绘
class Picture:
    def __init__(self, data, w, b):
        self.b = b
        self.w = w
        plt.figure(1)
        plt.title('Perception Learning Algorithm', size=14)
        plt.xlabel('x0', size=14)
        plt.ylabel('x1', size=14)

        xData = np.linspace(0, 5, 100)
        yData = self.expression(xData)
        plt.plot(xData, yData, color='r', label='data')

        plt.scatter(data[0][0], data[0][1], s=50)
        plt.scatter(data[1][0], data[1][1], s=50)
        plt.scatter(data[2][0], data[2][1], s=50, marker='x')
        plt.scatter(data[3][0], data[3][1], s=50, marker='x')
        plt.savefig('2d_duio.png', dpi=75)

    def expression(self, x):
        y = (-self.b - self.w[:, 0] * x) / self.w[:, 1]
        return y

    def show_pic(self):
        plt.show()

if __name__ == '__main__':
    samples, labels = createdata()
    myperceptron = Perception(x=samples, y=labels)
    weights, bias = myperceptron.train()
    Picture = Picture(samples, weights, bias)
    Picture.show_pic()

3、通过sklearn 中的现象回归的实现

# -*- encoding:utf-8 -*-

"""
利用skLearn中的感知机学习算法进行实验
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron


def creatdata():
    """
    #创建数据,直接定义数据列表
    :return:
    """
    samples = np.array([[3, -3], [4, -3], [1, 1], [1, 2]])
    labels = np.array([-1, -1, 1, 1])
    return samples, labels


def MyPerceptron(samples, labels):
    # 定义感知机
    clf = Perceptron(fit_intercept=True, n_iter=30, shuffle=False)
    # 训练感知机
    clf.fit(samples, labels)
    # 得到权重矩阵
    weigths = clf.coef_

    # 得到截距bisa
    bias = clf.intercept_

    return weigths, bias


# 画图描绘
class Picture:
    def __init__(self, data, w, b):
        self.b = b
        self.w = w
        plt.figure(1)
        plt.title('Perceptron Learning Algorithm', size=14)
        plt.xlabel('x0-axis', size=14)
        plt.ylabel('x1-axis', size=14)

        xData = np.linspace(0, 5, 100)
        yData = self.expression(xData)
        plt.plot(xData, yData, color='r', label='sample data')

        plt.scatter(data[0][0], data[0][1], s=50)
        plt.scatter(data[1][0], data[1][1], s=50)
        plt.scatter(data[2][0], data[2][1], s=50, marker='x')
        plt.scatter(data[3][0], data[3][1], s=50, marker='x')
        plt.savefig('3sk_1.png', dpi=75)

    def expression(self, x):
        y = (-self.b - self.w[:, 0] * x) / self.w[:, 1]
        return y

    def show_pic(self):
        plt.show()


if __name__ == '__main__':
    samples, labels = creatdata()
    weights, bias = MyPerceptron(samples, labels)
    print('最终训练得到的w和b为:', weights, ',', bias)
    Picture = Picture(samples, weights, bias)
    Picture.show_pic()

4、基于iris 的分类实现

# -*-  encoding:utf-8 -*-
import numpy as np

'''
以scikit-learn 中的perceptron为例介绍分类算法

应用及其学习分类算法的五个步骤
(1)选择特征
(2)选择一个性能指标
(3)选择一个分类器和一个优化算法
(4)评价模型的性能
(5)优化算法

以scikit-learn 中的perceptron为例介绍分类算法
1 读取数据-iris
2 分配训练集和测试集
3 标准化特征值
4 训练感知器模型
5 用训练好的模型进行预测
6 计算性能指标
7 描绘分类界面

'''

from sklearn import datasets
import numpy as np
import matplotlib.pyplot as plt

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

# 训练数据和测试数据分为6:4
from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)

# 标准化数据
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()
sc.fit(x_train)
x_train_std = sc.transform(x_train)
x_test_std = sc.transform(x_test)

# 引入skleran 的Perceptron并进行训练
from sklearn.linear_model import Perceptron

ppn = Perceptron(n_iter=40, eta0=0.01, random_state=0)
ppn.fit(x_train_std, y_train)

y_pred = ppn.predict(x_test_std)
print('错误分类数:%d' % (y_test != y_pred).sum())

from sklearn.metrics import accuracy_score

print('准确率为:%.2f' % accuracy_score(y_test, y_pred))

# 绘制决策边界
from matplotlib.colors import ListedColormap
import warnings


def versiontuple(v):
    return tuple(map(int, (v.split('.'))))


def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
    # 设置标记点和颜色
    markers = ('s', 'x', 'o', 'b', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])

    # 绘制决策面
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())

    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1],
                    alpha=0.8, c=cmap(idx),
                    marker=markers[idx], label=cl)

    if test_idx:
        # 绘制所有数据点
        if not versiontuple(np.__version__) >= versiontuple('1.9.0'):
            X_test, y_test = X[list(test_idx), :], y[list(test_idx)]
            warnings.warn('Please update to NumPy 1.9.0 or newer')
        else:
            X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:, 0], X_test[:, 1], c='',
                    alpha=1.0, linewidth=1, marker='o',
                    s=55, label='test set')


def plot_result():
    X_combined_std = np.vstack((x_train_std, x_test_std))
    y_combined = np.hstack((y_train, y_test))

    plot_decision_regions(X=X_combined_std, y=y_combined,
                          classifier=ppn, test_idx=range(105, 150))
    plt.xlabel('petal length [standardized]')
    plt.ylabel('petal width [standardized]')
    plt.legend(loc='upper left')

    plt.tight_layout()
    plt.show()


plot_result()

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值