感知机(Perceptron algorithm) Python代码详解

其实像感知机这些基本的机器学习算法,原理自己也都懂,但是还是会在看代码的时候感到困惑,说不上哪里困惑,但就是困惑!所以,做一些笔记让自己更清楚一些。

1.

import numpy as np
import matplotlib.pyplot as plt#导入matplotlib库
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
np.random.seed(123)

% matplotlib inline

sklearn库对数据集进行划分需要使用model_selection函数,该函数的 train_test_split是交叉验证中常用的函数,功能是从样本中随机按比例选取train-data和test-data.

2. DataSet

X, y = make_blobs(n_samples=1000, centers=2)
fig = plt.figure(figsize=(8,6))
plt.scatter(X[:,0], X[:,1], c=y)#画散点图
plt.title("Dataset")#设置标题
plt.xlabel("First feature")#设置x轴标签
plt.ylabel("Second feature")#设置y轴标签
plt.show()#显示所画的图
(1)plt.figure:

在绘图过程中,调用figure创建一个绘图对象,并且使它成为当前的绘图对象。

(2)make_blobs:

scikit中的make_blobs方法常被用来生成聚类算法的测试数据,直观地说,make_blobs会根据用户指定的特征数量、中心点数量、范围等来生成几类数据,这些数据可用于测试聚类算法的效果。

  • n_samples是待生成的样本的总数。
  • n_features是每个样本的特征数。
  • centers表示类别数
(3) plt.figure(figsize=(8,6))
  • figsize:指定figure的宽和高,单位为英寸;

在这里插入图片描述

3.

y_true = y[:, np.newaxis]

X_train, X_test, y_train, y_test = train_test_split(X, y_true)
print(f'Shape X_train: {X_train.shape}')
print(f'Shape y_train: {y_train.shape})')
print(f'Shape X_test: {X_test.shape}')
print(f'Shape y_test: {y_test.shape}')

结果:
Shape X_train: (750, 2)
Shape y_train: (750, 1))
Shape X_test: (250, 2)
Shape y_test: (250, 1)

train_test_split是划分数据集的一个函数。

4. Perceptron model class

class Perceptron():

    def __init__(self):
        pass

    def train(self, X, y, learning_rate=0.05, n_iters=100):
        n_samples, n_features = X.shape

        # Step 0: Initialize the parameters
        self.weights = np.zeros((n_features,1))
        self.bias = 0

        for i in range(n_iters):
            # Step 1: Compute the activation
            a = np.dot(X, self.weights) + self.bias

            # Step 2: Compute the output
            y_predict = self.step_function(a)

            # Step 3: Compute weight updates
            delta_w = learning_rate * np.dot(X.T, (y - y_predict))
            delta_b = learning_rate * np.sum(y - y_predict)

            # Step 4: Update the parameters
            self.weights += delta_w
            self.bias += delta_b

        return self.weights, self.bias

    def step_function(self, x):
        return np.array([1 if elem >= 0 else 0 for elem in x])[:, np.newaxis]

    def predict(self, X):
        a = np.dot(X, self.weights) + self.bias
        return self.step_function(a)

5. Initialization and training the model

p = Perceptron()
w_trained, b_trained = p.train(X_train, y_train,learning_rate=0.05, n_iters=500)

6. Testing

y_p_train = p.predict(X_train)
y_p_test = p.predict(X_test)

print(f"training accuracy: {100 - np.mean(np.abs(y_p_train - y_train)) * 100}%")
print(f"test accuracy: {100 - np.mean(np.abs(y_p_test - y_test)) * 100}%")

7. Visualize decision boundary

def plot_hyperplane(X, y, weights, bias):
    """
    Plots the dataset and the estimated decision hyperplane
    """
    slope = - weights[0]/weights[1]
    intercept = - bias/weights[1]
    x_hyperplane = np.linspace(-10,10,10)
    y_hyperplane = slope * x_hyperplane + intercept
    fig = plt.figure(figsize=(8,6))
    plt.scatter(X[:,0], X[:,1], c=y)
    plt.plot(x_hyperplane, y_hyperplane, '-')
    plt.title("Dataset and fitted decision hyperplane")
    plt.xlabel("First feature")
    plt.ylabel("Second feature")
    plt.show()

8.

plot_hyperplane(X, y, w_trained, b_trained)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/YPP0229/article/details/92770857