利用Python实现一个感知机学习算法
本文主要参考英文教材Python Machine Learning第二章。pdf文档下载链接: https://pan.baidu.com/s/1nuS07Qp 密码: gcb9。
本文主要内容包括利用Python实现一个感知机模型并利用这个感知机模型完成一个分类任务。
Warren和McCullock于1943年首次提出MCP neuron神经元模型[1],之后,Frank Rosenblatt在MCP neuron model的基础之上提出了感知机Perceptron模型[2]。具体细节请阅读教材第二章。
采用面向对象的方法编写一个感知机接口,这样就可以初始化一个新的感知机对象,这个对象可以通过fit()方法从数据中学习参数,通过predict()方法做预测。下面通过代码来讲解实现过程:
import numpy as np 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_: 1d-array,Weights after fitting.
errors_: list,Numebr 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 samples and n_featuers is the number of features.
y: array-like, shape=[n_smaples]
Target values.
Returns
----------
self: object
"""
self.w_ = np.zeros(1 + X.shape[1]) # add w_0
#初始化权重。数据集特征维数+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))#调用了predict()函数
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)
接下来使用鸢尾花Iris数据集来训练感知机模型。加载两类花:Setosa和Versicolor。属性选定为:sepal length和petal length。当然,不局限于两个属性。我们可通过One-vs-All(OvA)或One-vs-Rest(OvR)技术讲二分类扩展到多分类的情形。
import pandas as pd#用pandas读取数据
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap
from Perceptron_1 import Perceptron df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',header=None)#读取数据还可以用request这个包
print(df.tail())#输出最后五行数据,看一下Iris数据集格式 """抽取出前100条样本,这正好是Setosa和Versicolor对应的样本,我们将Versicolor
对应的数据作为类别1,Setosa对应的作为-1。对于特征,我们抽取出sepal length和petal
length两维度特征,然后用散点图对数据进行可视化""" y = df.iloc[0:100,4].values
y = np.where(y == 'Iris-setosa',-1,1)
X = df.iloc[0:100,[0,2]].values
plt.scatter(X[:50,0],X[:50,1],color = 'red',marker='o',label='setosa')
plt.scatter(X[50:100,0],X[50:100,1],color='blue',marker='x',label='versicolor')
plt.xlabel('petal length')
plt.ylabel('sepal lenght')
plt.legend(loc='upper left')
plt.show() #train our perceptron model now
#为了更好地了解感知机训练过程,我们将每一轮的误分类
#数目可视化出来,检查算法是否收敛和找到分界线
ppn=Perceptron(eta=0.1,n_iter=10)
ppn.fit(X,y)
plt.plot(range(1,len(ppn.errors_)+1),ppn.errors_,marker='o')
plt.xlabel('Epoches')
plt.ylabel('Number of misclassifications')
plt.show() #画分界线超平面
def plot_decision_region(X,y,classifier,resolution=0.02):
#setup marker generator and color map
markers=('s','x','o','^','v')
colors=('red','blue','lightgreen','gray','cyan')
cmap=ListedColormap(colors[:len(np.unique(y))]) #plot the desicion surface
x1_min,x1_max=X[:,0].min()-1,X[:,0].max()+1
x2_min,x2_max=X[:,1].min()-1,X[:,1].max()+1 xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution),
np.arange(x2_min,x2_max,resolution))
Z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)
Z=Z.reshape(xx1.shape) plt.contour(xx1,xx2,Z,alpha=0.4,cmap=cmap)
plt.xlim(xx1.min(),xx1.max())
plt.ylim(xx2.min(),xx2.max()) #plot class samples
for idx,cl in enumerate(np.unique(y)):
plt.scatter(x=X[y==cl,0],y=X[y==cl,1],alpha=0.8,c=cmap(idx), marker=markers[idx],label=cl) plot_decision_region(X,y,classifier=ppn)
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc='upperleft')
plt.show()
结果如下图:

若两类模式是线性可分的,即存在一个线性超平面能将它们分开,则感知机的学习过程一定会收敛converge而求得适当的权向量;否则感知机学习过程就会发生振荡fluctuation,权重向量难以稳定下来,不能求得合适解,具体的证明过程见文章[3]
代码中用到NumPy、Pandas和Matplotlib库,不熟悉的可以通过如下链接学习。
NumPy: http://wiki.scipy.org/Tentative_NumPy_Tutorial
Pandas: http://pandas.pydata.org/pandas-docs/stable/tutorials.html
Matplotlib: http://matplotlib.org/users/beginner.html
References:
[1] W. S. McCulloch and W. Pitts. A Logical Calculus of the Ideas Immanent in Nervous Activity. The bulletin of mathematical biophysics, 5(4):115–133, 1943
[2] F. Rosenblatt, The Perceptron, a Perceiving and Recognizing Automaton. Cornell Aeronautical Laboratory, 1957
[3] Minsky, M. and S. Papert. (1969). Perceptrons. MIT Press, Cambridge, MA.
利用Python实现一个感知机学习算法的更多相关文章
- 感知机学习算法 python实现
参考李航<统计学习方法> 一开始的感知机章节,看着不太复杂就实现一下... """ 感知机学习算法的原始形式 例2.1 """ ...
- 【机器学习】感知机学习算法(PLA)
感知机问题学习算法引入:信用卡问题 根据已知数据(不同标准的人的信用评级)训练后得出一个能不能给新客户发放信用卡的评定结果 解决该问题的核心思想扔为之前所讲到的梯度下降算法,对于更多条件的类似问题,首 ...
- 感知机学习算法(PLA)
Perception Learning Algorithm, PLA 1.感知机 感知机是一种线性分类模型,属于判别模型. 感知机模型给出了由输入空间到输出空间的映射: f(X) = sign(WTX ...
- $《利用Python进行数据分析》学习笔记系列——IPython
本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...
- 感知机学习算法Java实现
感知机学习算法Java实现. Perceptron类用于实现感知机, 其中的perceptronOriginal()方法用于实现感知机学习算法的原始形式: perceptronAnother()方法用 ...
- 利用Python完成一个小游戏:随机挑选一个单词,并对其进行乱序,玩家要猜出原始单词
一 Python的概述以及游戏的内容 Python是一种功能强大且易于使用的编程语言,更接近人类语言,以至于人们都说它是“以思考的速度编程”:Python具备现代编程语言所应具备的一切功能:Pytho ...
- 利用Python制作一个只属于和她的聊天器,再也不用担心隐私泄露啦!
------------恢复内容开始------------ 是否担心微信的数据流会被监视?是否担心你和ta聊天的小秘密会被保存到某个数据库里?没关系,现在我们可以用Python做一个只属于你和ta的 ...
- 吴裕雄 python 机器学习——人工神经网络感知机学习算法的应用
import numpy as np from matplotlib import pyplot as plt from sklearn import neighbors, datasets from ...
- 利用python写一个简单的小爬虫 爬虫日记(1)(好好学习)
打开py的IDLE >>>import urllib.request >>>a=urllib.request.urlopen("http://www.ba ...
随机推荐
- Linux常用网络测试命令
Linux常用网络测试命令 1. ifconfig 可以使用ifconfig命令来配置并查看网络接口的配置情况. 例如: (1) 配置eth0的IP地址, 同时激活该设备. #ifcon ...
- find命令之xargs,exec
一,find命令之xargs: 在 使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命 令长度有限制,这样 ...
- 全局精确流量调度新思路-HttpDNS服务详解
但凡使用域名来给用户提供服务的互联网企业,都或多或少地无法避免在有中国特色的互联网环境中遭遇到各种域名被缓存.用户跨网访问缓慢等问题.那么对于腾讯这样的域名数量在10万级别的互联网公司来讲,域名解析异 ...
- iOS CAReplicatorLayer 实现脉冲动画效果
iOS CAReplicatorLayer 实现脉冲动画效果 效果图 脉冲数量.速度.半径.透明度.渐变颜色.方向等都可以设置.可以用于地图标注(Annotation).按钮长按动画效果(例如录音按钮 ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- 影响国内WinCE7发展的最大障碍是没有D版下载
WinCE红火的时代已经过去,做嵌入式系统时考虑WinCE的越来越少,网络上相关文章也是越来越少. 但真正用过WinCE的应该有体会,它集成了文件系统,tcp/ip,GUI系统,usb驱动,就这些,你 ...
- ipython及Python初体验
阅读目录: Python环境体验 Python编辑器 ipython安装 Python提示符 Python初体验 print和变量 变量操作 内建函数:方法 数学运算:简单算术.随机数 关于模块 一. ...
- Spring学习(24)--- AOP之 Aspect instantiation models(aspect实例模式)特别说明
重要: schema-defined aspects只支持singleton model,即 基于配置文件的aspects只支持单例模式
- Spring学习(23)--- AOP之Introductions应用
简介允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象 由<aop:aspect>中的<aop:declare-parents>元素声明该元素用 ...
- ionic2 tabs使用自定义图标
在ionic2中图标是通过类添加的 比如说 tabIcon="homeImg" 在页面中研究可以看到在ios上有一个类是 .ion-ios-homeImg:before 所以我 ...