感知器是一种二元的线性分类器,其使用 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)