【Python-ML】感知器学习算法(perceptron)
2017-12-21 14:16
363 查看
1、数学模型
2、权值训练
3、Python代码
感知器收敛的前提是两个类别必须是线性可分的,且学习速率足够小。如果两个类别无法通过一个线性决策边界进行划分,要为模型在训练集上的学习迭代次数设置一个最大值,或者设置一个允许错误分类样本数量的阈值,否则感知器训练算法将永远不停地更新权值。
# -*- coding: utf-8 -*-
'''
Created on 2017年12月15日
@author: Jason.F
@summary: 感知器学习算法
'''
import numpy as np
import time
import matplotlib.pyplot as plt
import pandas as pd
class perceptron(object):
'''
Perceptron classifier.
Parameters
eta:float=Learning rate (between 0.0 and 1.0)
n_iter:int=Passes over the training dataset.
Attributes
w_:ld-array=weights after fitting.
errors_:list=Number of misclassifications in every epoch.
'''
def __init__(self,eta=0.01,n_iter=10):
self.eta=eta
self.n_iter=n_iter
def fit(self,X,y):
'''
Fit training data.
Parameters
X:{array-like},shape=[n_samples,n_features]
Training vectors,where n_samples is the number of the samples and n_features is the number of features.
y:array-like,shape=[n_samples]
Target values.
Returns
self:object
'''
self.w_=np.zeros(1+X.shape[1])
self.errors_=[]
for _ in range(self.n_iter):
errors=0
for xi , target in zip(X,y):
update=self.eta * (target - self.predict(xi))
self.w_[1:]+=update *xi
self.w_[0]+=update
errors += int (update !=0.0)
self.errors_.append(errors)
return self
def net_input(self,X):
'''
Calculate net input
'''
return np.dot(X, self.w_[1:])+self.w_[0]
def predict(self,X):
'''
Return class label after unit step
'''
return np.where(self.net_input(X) >=0.0,1,-1)
if __name__ == "__main__":
start = time.clock()
train =pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',header=None)
ppn = perceptron(eta=0.01,n_iter=10)
X_train = train.drop([4], axis=1)
X_train=X_train.values #dataframe convert to array
y_train = train[4].values
y_train=np.where(y_train == 'Iris-setosa',-1,1)
ppn.fit(X_train,y_train)
#预测
print (ppn.predict([6.9,3.0,5.1,1.8]))
#绘制错误分类样本数量
plt.plot(range(1,len(ppn.errors_)+1),ppn.errors_,marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of misclassifications')
plt.show()
end = time.clock()
print('finish all in %s' % str(end - start))
代码中用UCI机器学习库中的数据集做试验。可设置不同的学习速率eta和迭代次数n_iter观察收敛情况。
2、权值训练
3、Python代码
感知器收敛的前提是两个类别必须是线性可分的,且学习速率足够小。如果两个类别无法通过一个线性决策边界进行划分,要为模型在训练集上的学习迭代次数设置一个最大值,或者设置一个允许错误分类样本数量的阈值,否则感知器训练算法将永远不停地更新权值。
# -*- coding: utf-8 -*-
'''
Created on 2017年12月15日
@author: Jason.F
@summary: 感知器学习算法
'''
import numpy as np
import time
import matplotlib.pyplot as plt
import pandas as pd
class perceptron(object):
'''
Perceptron classifier.
Parameters
eta:float=Learning rate (between 0.0 and 1.0)
n_iter:int=Passes over the training dataset.
Attributes
w_:ld-array=weights after fitting.
errors_:list=Number of misclassifications in every epoch.
'''
def __init__(self,eta=0.01,n_iter=10):
self.eta=eta
self.n_iter=n_iter
def fit(self,X,y):
'''
Fit training data.
Parameters
X:{array-like},shape=[n_samples,n_features]
Training vectors,where n_samples is the number of the samples and n_features is the number of features.
y:array-like,shape=[n_samples]
Target values.
Returns
self:object
'''
self.w_=np.zeros(1+X.shape[1])
self.errors_=[]
for _ in range(self.n_iter):
errors=0
for xi , target in zip(X,y):
update=self.eta * (target - self.predict(xi))
self.w_[1:]+=update *xi
self.w_[0]+=update
errors += int (update !=0.0)
self.errors_.append(errors)
return self
def net_input(self,X):
'''
Calculate net input
'''
return np.dot(X, self.w_[1:])+self.w_[0]
def predict(self,X):
'''
Return class label after unit step
'''
return np.where(self.net_input(X) >=0.0,1,-1)
if __name__ == "__main__":
start = time.clock()
train =pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',header=None)
ppn = perceptron(eta=0.01,n_iter=10)
X_train = train.drop([4], axis=1)
X_train=X_train.values #dataframe convert to array
y_train = train[4].values
y_train=np.where(y_train == 'Iris-setosa',-1,1)
ppn.fit(X_train,y_train)
#预测
print (ppn.predict([6.9,3.0,5.1,1.8]))
#绘制错误分类样本数量
plt.plot(range(1,len(ppn.errors_)+1),ppn.errors_,marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of misclassifications')
plt.show()
end = time.clock()
print('finish all in %s' % str(end - start))
代码中用UCI机器学习库中的数据集做试验。可设置不同的学习速率eta和迭代次数n_iter观察收敛情况。
相关文章推荐
- 闲谈:感知器学习算法(The perceptron learning algorithm)
- 什么是感知器学习算法(Perceptron Learning Algorithm/PLA)?
- 感知机学习算法的简单实现(Python)
- 聚类算法——python实现学习向量量化(LVQ)算法
- 分类算法之感知器学习算法PLA 和口袋算法Pocket Algorithm
- 利用Python实现一个感知机学习算法
- 感知器学习算法----神经网络
- 分类系列之感知器学习算法PLA 和 口袋算法Pocket Algorithm
- 初学ML笔记N0.2——生成学习算法
- 学习python之路---python小算法总结(一)
- 算法学习(1):排序算法-插入排序及python实现
- 发现shedskin的example是学习算法的好材料(Python实现)
- 机器学习实战_初识朴素贝叶斯算法_理解其python代码(二)
- python OpenCV学习笔记(二十九):图像流域(分水岭)分割算法
- python学习12-算法
- 初学ML笔记N0.2——生成学习算法
- 2.感知器学习算法(待修)
- 学习python之路---python小算法总结(二)
- python实现神经网络感知器算法
- matlab实现感知器学习算法