一、感知机
简介:感知机(perceptron)是二类分类的线性分类模型,其输入为实例的特征向量,输出为实例的类别。感知机对应于输入空间(特征空间)中将实例划分为正负两类的分离超平面。感知机是一种线性分类模型。
公式:x表示特征向量,y表示输出实例类别取值{-1,1},可知感知机函数为:
sign是符号函数:
二、学习策略
点到线的距离公式:
延伸到点到超平面距离:
忽略||w||分之一,就得到感知机的损失函数:
三、学习算法
目的:优化损失函数,采用随机梯度下降算法,求得
w梯度为:
b梯度为:
四、自实现感知机算法代码
...python
# -*- coding: UTF-8 -*-
"""
=================================================
@Project :ML
@File : perceptron
@Desc :感知机算法(自写与sklearn两种)
==================================================
"""
import numpyas np
class MyPerceptron():
def __init__(self):
self.w =None
self.b =0
self.learning_rate =0.1
self.epoch =5
def fit(self,trainX,trainY):
self.w = np.zeros(trainX.shape[1])
for _in range(self.epoch):
true_num =0
for x,yin zip(trainX,trainY):
status = y*(np.dot(self.w,x)+self.b)
if status <=0:
_w = np.dot(y,x)
_b = y
self.w +=self.learning_rate*_w
self.b +=self.learning_rate*_b
else:
true_num +=1
print("acc:",(true_num/trainX.shape[0]))
def funcB():
from sklearn.datasetsimport make_classification
from matplotlibimport pyplotas plt
x, y = make_classification(n_samples=1000, n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1)
for i,jin enumerate(y):
if j ==0:
y[i] = -1
x_data_train = x[:800, :]
x_data_test = x[800:, :]
y_data_train = y[:800]
y_data_test = y[800:]
positive_x1 = [x[i, 0]for iin range(1000)if y[i] ==1]
positive_x2 = [x[i, 1]for iin range(1000)if y[i] ==1]
negetive_x1 = [x[i, 0]for iin range(1000)if y[i] == -1]
negetive_x2 = [x[i, 1]for iin range(1000)if y[i] == -1]
perceptron = MyPerceptron()
perceptron.fit(x_data_train,y_data_train)
plt.scatter(positive_x1, positive_x2, c='red')
plt.scatter(negetive_x1, negetive_x2, c='blue')
# 画出超平面(在本例中即是一条直线)
line_x = np.arange(-4, 4)
line_y = line_x * (-perceptron.w[0]/ -perceptron.w[1]) - perceptron.b# y = -b-(w[0]x)/w[1]
plt.plot(line_x, line_y)
plt.show()
def funcA():
from sklearn.datasetsimport make_classification
from sklearn.linear_modelimport Perceptron
from matplotlibimport pyplotas plt
x,y = make_classification(n_samples=1000,n_features=2,n_redundant=0,n_informative=1,n_clusters_per_class=1)
# 训练数据和测试数据
x_data_train = x[:800, :]
x_data_test = x[800:, :]
y_data_train = y[:800]
y_data_test = y[800:]
# 正例和反例
positive_x1 = [x[i, 0]for iin range(1000)if y[i] ==1]
positive_x2 = [x[i, 1]for iin range(1000)if y[i] ==1]
negetive_x1 = [x[i, 0]for iin range(1000)if y[i] ==0]
negetive_x2 = [x[i, 1]for iin range(1000)if y[i] ==0]
# 构建感知机
clf = Perceptron(fit_intercept=False, n_iter=30, shuffle=False)
# 使用训练数据进行训练
clf.fit(x_data_train, y_data_train)
# 得到训练结果,权重矩阵
print(clf.coef_)
# 输出为:[[0.21720699 2.49185955]]
# 超平面的截距,此处输出为:[0.]
# print(clf.intercept_)
# 预测
acc = clf.score(x_data_test, y_data_test)
print(acc)
plt.scatter(positive_x1, positive_x2, c='red')
plt.scatter(negetive_x1, negetive_x2, c='blue')
# 画出超平面(在本例中即是一条直线)
line_x = np.arange(-4, 4)
line_y = line_x * (-clf.coef_[0][0] / clf.coef_[0][1]) - clf.intercept_# y = -b-(w[0]x)/w[1]
plt.plot(line_x, line_y)
plt.show()
if __name__ =='__main__':
funcB()
#funcA()
...
最后效果如图所示: