常見的機器學習算法(三)感知器

感知器是一種二元的線性分類器,其使用 d- 維超平面來將一組訓練樣本( d- 維輸入向量)映射成二進制輸出值(0、1)。

使用感知器的學習規則來計算權重向量和偏置量的更新值:

更新權重向量和偏置量:

import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs

np.random.seed(123)
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')
plt.ylabel('Second feature')
plt.show()

y_true = y[:, np.newaxis]
x_train, x_test, y_train, y_test = train_test_split(x, y_true)
print('Shape of x_train: ', x_train.shape)
print('Shape of y_train: ', y_train.shape)
print('Shape of x_test: ', x_test.shape)
print('Shape of y_test: ', y_test.shape)

class Perceptron:
    def __init__(self):
        pass

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def train(self, x, y, l_r=0.05, n_iters=100):
        n_samples, n_features = x.shape
        self.weight = np.zeros((n_features, 1))
        self.bias = 0

        for i in range(n_iters):
            y_predict = np.dot(x, self.weight) + self.bias
            y_predict = self.step_f(y_predict)
            dw = l_r * np.dot(x.T, (y - y_predict))
            db = l_r * np.sum(y - y_predict)

            self.weight += dw
            self.bias += db

        return self.weight, self.bias

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

    def predict(self, x):
        y_predict = np.dot(x, self.weight) + self.bias
        return self.step_f(y_predict)

p = Perceptron()
w_trained, b_trained = p.train(x_train, y_train, l_r=0.05, n_iters=500)

'測試'
y_p_train = p.predict(x_train)
y_p_test = p.predict(x_test)
print('Train accuracy: ',
      (100 - np.mean(np.abs(y_p_train - y_train))), '%')
print('Test accuracy: ',
      (100 - np.mean(np.abs(y_p_test - y_test))), '%')

'可視化決策邊界'
def plot_hyperplane(x, y, weight, bias):
    slope = - weight[0] / weight[1]#斜率
    intercept = - bias / weight[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()

plot_hyperplane(x, y, w_trained, b_trained)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章