python大战机器学习——人工神经网络
人工神经网络是有一系列简单的单元相互紧密联系构成的,每个单元有一定数量的实数输入和唯一的实数输出。神经网络的一个重要的用途就是接受和处理传感器产生的复杂的输入并进行自适应性的学习,是一种模式匹配算法,通常用于解决分类和回归问题。
常用的人工神经网络算法包括:感知机神经网络(Perceptron Neural Nerwork)、反向传播网络(Back Propagation,BP)、HopField网络、自组织映射网络(Self-Organizing Map,SOM)、学习矢量量化网络(Learning Vector Quantization,LVQ)
1、感知机模型
感知机是一种线性分类器,它用于二类分类问题。它将一个实例分类为正类(取值+1)和负类(-1)。其物理意义:它是将输入空间(特征空间)划分为正负两类的分离超平面。
输入:线性可分训练数据集T,学习率η
输出:感知机参数w,b
算法步骤:
1)选取初始值w0和b0
2)在训练数据集中选取数据(xi,yi)
3)若y1(w.xi+b)<=0(即该实例为误分类点)则更新参数:w=w+η.yi.xi b=b+η.yi
4)在训练数据集中重复选取数据来更新w,b直到训练数据集中没有误分类点为止
实验代码:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifier def creat_data(n):
np.random.seed(1)
x_11=np.random.randint(0,100,(n,1))
x_12=np.random.randint(0,100,(n,1,))
x_13 = 20+np.random.randint(0, 10, (n, 1,))
x_21 = np.random.randint(0, 100, (n, 1))
x_22 = np.random.randint(0, 100, (n, 1))
x_23 = 10-np.random.randint(0, 10, (n, 1,)) # print(x_11)
# print(x_12)
# print(x_13)
# print(x_21)
# print(x_22)
# print(x_23) # rotate 45 degrees along the X axis
new_x_12=x_12*np.sqrt(2)/2-x_13*np.sqrt(2)/2
new_x_13 = x_12 * np.sqrt(2) / 2 + x_13 * np.sqrt(2) / 2
new_x_22=x_22*np.sqrt(2)/2-x_23*np.sqrt(2)/2
new_x_23 = x_22 * np.sqrt(2) / 2 + x_23 * np.sqrt(2) / 2 # print(new_x_12)
# print(new_x_13)
# print(new_x_22)
# print(new_x_23) plus_samples=np.hstack([x_11,new_x_12,new_x_13,np.ones((n,1))])
minus_samples=np.hstack([x_11,new_x_22,new_x_23,-np.ones((n,1))])
samples=np.vstack([plus_samples,minus_samples])
# print(samples)
np.random.shuffle(samples) # print(plus_samples)
# print(minus_samples)
# print(samples) return samples def plot_samples(ax,samples):
Y=samples[:,-1]
Y=samples[:,-1]
# print(Y)
position_p=Y==1 ##the position of positve class
position_m=Y==-1 ##the position of minus class
# print(position_p)
# print(position_m)
ax.scatter(samples[position_p,0],samples[position_p,1],samples[position_p,2],marker='+',label="+",color='b')
ax.scatter(samples[position_m,0],samples[position_m,1],samples[position_m,2],marker='^',label='-',color='y') def perceptron(train_data,eta,w_0,b_0):
x=train_data[:,:-1] #x data
y=train_data[:,-1] #corresponding classification
length=train_data.shape[0] #the size of sample==the row number of the train_data
w=w_0
b=b_0
step_num=0
while True:
i=0
while(i<length): #traverse all sample points in a sample set
step_num+=1
x_i=x[i].reshape((x.shape[1],1))
y_i=y[i]
if y_i*(np.dot(np.transpose(w),x_i)+b)<=0: #the point is misclassified
w=w+eta*y_i*x_i #gradient descent
b=b+eta*y_i
break;#perform the next round of screening
else: #the point is not a misclassification point select the next sample point
i=i+1
if(i==length):
break
return (w,b,step_num) def creat_hyperplane(x,y,w,b):
return (-w[0][0]*x-w[1][0]*y-b)/w[2][0] #w0*x+w1*y+w2*z+b=0 data=creat_data(100)
eta,w_0,b_0=0.1,np.ones((3,1),dtype=float),1
w,b,num=perceptron(data,eta,w_0,b_0) fig=plt.figure()
plt.suptitle("perceptron")
ax=Axes3D(fig)
#draw samplt point
plot_samples(ax,data)
#draw hyperplane
x=np.linspace(-30,100,100)
y=np.linspace(-30,100,100)
x,y=np.meshgrid(x,y)
z=creat_hyperplane(x,y,w,b)
ax.plot_surface(x,y,z,rstride=1,cstride=1,color='g',alpha=0.2) ax.legend(loc='best')
plt.show()
实验结果:
注:算法中,最外层循环只有在全部分类正确的这种情况下退出;内层循环从前到后遍历所有的样本点。一旦发现某个样本点是误分类点,就更新w和b,然后重新从头开始遍历所有的样本点。感知机算法的对偶形式(参考原著)其处理效果与原始算法相差不大,但是从其输出的α数组值可以看出:大多数的样本点对于最终解并没有贡献。分离超平面的位置是由少部分重要的样本点决定的。而感知机学习算法的对偶形式能够找出这些重要的样本点。这就是支持向量机的原理。
一开始我在create data时,公式写错了,导致所得到的数据线性不可分,此时发现算法根本无法完成迭代。可见感知机算法只能够用于线性可分数据集。
2、神经网络
(1)从感知机到神经网络:M-P神经元模型
1)每个神经元接收到来自相邻神经元传递过来的输入信号
2)这些输入信号通过带权重的连接进行传递
3)神经元接收到的总输入值将与神经元的阈值进行比较,然后通过“激活函数”处理以产生神经元输出
4)理论上的激活函数为阶跃函数
1,x>=0
f(x)=
0,x<0
(2)多层前馈神经网络
通常神经网络的结构为:1)每层神经元与下一层神经元全部互连 2)同层神经元之间不存在连接 3)跨层神经元之间也不存在连接
多层前馈神经网络具有以下特点:1)隐含层和输出层神经元都是拥有激活函数的功能神经元 2)输入层接收外界输入信号,不进行激活函数处理 3)最终结果由输出层神经元给出
神经网络的学习就是根据训练数据集来调整神经元之间的连接权重,以及每个功能神经元的阈值。
多层前馈神经网络的学习通常采用误差逆传播算法(error BackPropgation,BP):该算法是训练多层神经网络的经典算法,其从原理上就是普通的梯度下降法求最小值问题。它关键的地方在于两个:1)导数的链式法则 2)sigmoid激活函数的性质:sigmoid函数求导的结果等于自变量的乘积形式
多层前馈网络若包含足够多神经元的隐含层,则它就能够以任意精度逼近任意复杂度的连续函数
实验代码:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifier def creat_data_no_linear(n):
np.random.seed(1)
x_11=np.random.randint(0,100,(n,1))
x_12=10+np.random.randint(-5,5,(n,1,)) x_21 = np.random.randint(0, 100, (n, 1))
x_22 = 20+np.random.randint(0, 10, (n, 1)) x_31 = np.random.randint(0, 100, (int(n/10),1))
x_32 = 20+np.random.randint(0, 10, (int(n/10), 1)) # print(x_11)
# print(x_12)
# print(x_13)
# print(x_21)
# print(x_22)
# print(x_23) # rotate 45 degrees along the X axis
new_x_11 = x_11 * np.sqrt(2) / 2 - x_12 * np.sqrt(2) / 2
new_x_12=x_11*np.sqrt(2)/2+x_12*np.sqrt(2)/2
new_x_21 = x_21 * np.sqrt(2) / 2 - x_22 * np.sqrt(2) / 2
new_x_22=x_21*np.sqrt(2)/2+x_22*np.sqrt(2)/2
new_x_31 = x_31 * np.sqrt(2) / 2 - x_32 * np.sqrt(2) / 2
new_x_32 = x_31 * np.sqrt(2) / 2 + x_32 * np.sqrt(2) / 2 # print(new_x_12)
# print(new_x_13)
# print(new_x_22)
# print(new_x_23) plus_samples=np.hstack([new_x_11,new_x_12,np.ones((n,1))])
minus_samples=np.hstack([new_x_21,new_x_22,-np.ones((n,1))])
err_samples=np.hstack([new_x_31,new_x_32,np.ones((int(n/10),1))])
samples=np.vstack([plus_samples,minus_samples,err_samples])
# print(samples)
np.random.shuffle(samples) # print(plus_samples)
# print(minus_samples)
# print(samples) return samples def plot_samples_2d(ax,samples):
Y=samples[:,-1]
# print(Y)
position_p=Y==1 ##the position of positve class
position_m=Y==-1 ##the position of minus class
# print(position_p)
# print(position_m)
ax.scatter(samples[position_p,0],samples[position_p,1],marker='+',label="+",color='b')
ax.scatter(samples[position_m,0],samples[position_m,1],marker='^',label='-',color='y') def predict_with_MLPClassifier(ax,train_data):
train_x=train_data[:,:-1]
train_y=train_data[:,-1]
clf=MLPClassifier(activation='logistic',max_iter=1000)
clf.fit(train_x,train_y)
print(clf.score(train_x,train_y)) x_min,x_max=train_x[:,0].min()-1,train_x[:,0].max()+2
y_min,y_max=train_x[:,1].min()-1,train_x[:,1].max()+2
plot_step=1 xx,yy=np.meshgrid(np.arange(x_min,x_max,plot_step),np.arange(y_min,y_max,plot_step))
Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
ax.contourf(xx,yy,Z,cmap=plt.cm.Paired) fig=plt.figure()
ax=fig.add_subplot(1,1,1)
data=creat_data_no_linear(500)
predict_with_MLPClassifier(ax,data)
plot_samples_2d(ax,data)
ax.legend(loc='best')
plt.show()
实验结果:
生成的实验数据
样例(对鸢尾花进行分类)代码:
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from sklearn.datasets import load_iris
from sklearn.neural_network import MLPClassifier def load_data():
iris=load_iris()
X=iris.data[:,0:2] #choose the first two features
Y=iris.target
data=np.hstack((X,Y.reshape(Y.size,1)))
np.random.seed(0)
np.random.shuffle(data)
X=data[:,:-1]
Y=data[:,-1]
x_train=X[:-30]
x_test=X[-30:]
y_train=Y[:-30]
y_test=Y[-30:] return x_train,x_test,y_train,y_test def neural_network_sample(*data):
x_train,x_test,y_train,y_test=data
cls=MLPClassifier(activation='logistic',max_iter=10000,hidden_layer_sizes=(30,))
cls.fit(x_train,y_train)
print("the train score:%.f"%cls.score(x_train,y_train))
print("the test score:%.f"%cls.score(x_test,y_test)) x_train,x_test,y_train,y_test=load_data() neural_network_sample(x_train,x_test,y_train,y_test)
实验结果:
这里的score居然等于1。。。书上给的数据是0.8,不知道是因为算法改进了还是怎么回事。
注:在进行编码实现时遇到了一些小插曲,就是Anaconda的sklearn并不是完整的,当导入MLPClassifier时会出错,因为其中0.17的版本的scikit-learn中并不包括这个库。所以接下来要做的就是更新Anaconda的相关库。这时候问题就来了,更新时,网速极慢,只有6k/s,并且十几秒后就会报错,错误原因是无法连接某些网址。起初我以为是网速的问题,尝试了各种方式,调整了网络通各个端口去试都没有用,甚至想到了换网线和换网络通账号,结果还是不行。后来想到会不会是要翻墙,于是参照着这个网址:http://blog.sina.com.cn/s/blog_920b83770102xjxp.html,翻了一下,发现并不能访问youtube,这让我感到很难受。抱着试试的态度我又重新敲起了更新的指令,这次居然奇迹般的可以了!!!后来一想youtube之所以打不开,可能是浏览器的设置问题,vpn本身是可以用的。有种天道酬勤的感觉!
不过这也让我很好的反思了一下。遇到问题时一定要认真分析可能的原因,而不是盲目的去猜,甚至愚蠢的去多次尝试之前并没有生效的方法,这样会浪费太多的时间。解决问题一定是从可能性最大的错误着手,并且要加以分析,否则不仅时间浪费了,自信心也受到了打击,人也会弄的疲劳不堪。
python大战机器学习——人工神经网络的更多相关文章
- 吴裕雄 python 机器学习——人工神经网络感知机学习算法的应用
import numpy as np from matplotlib import pyplot as plt from sklearn import neighbors, datasets from ...
- 吴裕雄 python 机器学习——人工神经网络与原始感知机模型
import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ...
- python大战机器学习——集成学习
集成学习是通过构建并结合多个学习器来完成学习任务.其工作流程为: 1)先产生一组“个体学习器”.在分类问题中,个体学习器也称为基类分类器 2)再使用某种策略将它们结合起来. 通常使用一种或者多种已有的 ...
- python大战机器学习——模型评估、选择与验证
1.损失函数和风险函数 (1)损失函数:常见的有 0-1损失函数 绝对损失函数 平方损失函数 对数损失函数 (2)风险函数:损失函数的期望 经验风险:模型在数据集T上的平均损失 根据大 ...
- python大战机器学习——数据预处理
数据预处理的常用流程: 1)去除唯一属性 2)处理缺失值 3)属性编码 4)数据标准化.正则化 5)特征选择 6)主成分分析 1.去除唯一属性 如id属性,是唯一属性,直接去除就好 2.处理缺失值 ( ...
- python大战机器学习——半监督学习
半监督学习:综合利用有类标的数据和没有类标的数据,来生成合适的分类函数.它是一类可以自动地利用未标记的数据来提升学习性能的算法 1.生成式半监督学习 优点:方法简单,容易实现.通常在有标记数据极少时, ...
- python大战机器学习——支持向量机
支持向量机(Support Vector Machine,SVM)的基本模型是定义在特征空间上间隔最大的线性分类器.它是一种二类分类模型,当采用了核技巧之后,支持向量机可以用于非线性分类. 1)线性可 ...
- python大战机器学习——聚类和EM算法
注:本文中涉及到的公式一律省略(公式不好敲出来),若想了解公式的具体实现,请参考原著. 1.基本概念 (1)聚类的思想: 将数据集划分为若干个不想交的子集(称为一个簇cluster),每个簇潜在地对应 ...
- python大战机器学习——数据降维
注:因为公式敲起来太麻烦,因此本文中的公式没有呈现出来,想要知道具体的计算公式,请参考原书中内容 降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中 1.主成分分析(PCA) 将n ...
随机推荐
- tensorflow kmeans 聚类
iris: # -*- coding: utf-8 -*- # K-means with TensorFlow #---------------------------------- # # This ...
- (转)HLS协议,html5视频直播一站式扫盲
本文来自于腾讯bugly开发者社区,原文地址:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1277 视频直播这么火,再不学就 ou ...
- vs中解决方案、项目、类及ATL的理解
解决方案,是对所有要完成工作的统称,一般叫Solution. 项目,也叫工程,是将解决方案分成若干个模块进行处理,一般叫做Project.添加项目就是添加工程.解决方案是所有项目的总和. 一个项目里面 ...
- Linux下的Tomcat JVM 调优
1. 适用场景 Tomcat 运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或者java.lang.OutOfMemoryErro ...
- 1091 Acute Stroke (30)(30 分)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the re ...
- 【Lintcode】017.Subsets
题目: 题解: Solution 1 () class Solution { public: vector<vector<int> > subsets(vector<in ...
- C++之迭代器失效总结
1. 对于序列式容器(如vector,deque),序列式容器就是数组式容器,删除当前的iterator会使后面所有元素的iterator都失效.这是因为vetor,deque使用了连续分配的内存,删 ...
- select查询语句执行顺序
查询中用到的关键词主要包含六个,并且他们的顺序依次为select--from--where--group by--having--order by其中select和from是必须的,其他关键词是可选的 ...
- VijosP1595:学校网络(有向图变强连通图)
描述 一些学校的校园网连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校a支援学校b,并不表示学校b一定支援学校a).当某校获得一个新软件时,无论是直接得到的还是从 ...
- 使用BIND安装智能DNS服务器(三)---添加view和acl配置
智能DNS的配置主要修改named.conf文件,利用view和acl来实现. acl文件内容,这里只列出一部分,具体详细的可以参考这个网址 纯真IP库,给出了十分详细的IP地址,下载安装后,打开软件 ...