课程地址:https://www.imooc.com/learn/813html
分类行为python
向量的点积git
矩阵的转置github
增长w0,x0变量。当z大于0,输出1,当z值小于0,输出-1算法
x(j)输入的相关电信号数组
只有感知器获得错误分类,才须要调整▽w(j)的权重网络
学习率根据不一样状况调整app
更新第一个量机器学习
同理,更新第二个量函数
更新第三个量
在输入新的分类输入,更新权重
阈值初始化为0,电信号份量固定值为1,被省略
告知其算法必须知足第一种状况(图一),即线性分割。后两种状况不适合感知器算法
目标是:找到中间分开虚线
步骤:
(1)初始化向量w
(2)把x样本输入到感知器中
(3)点积,份量相乘结果求和
(4)结果输入到步调函数中,激活函数,获得结果1或者-1。
(5)若是正确则输出电信号,获得最终结果;若是错误,就把错误结果根据前面所描述得步骤返回来,对权重向量进行跟新,再把其余原有的训练样本或者是新的徐连样本再从新输入到感知器中。
y中1对应X的【1,2,3】样本,y中的-1对应X的【4,5,6】样本
训练过程完成
电信号输入的点积,判断预测值是否大于0,返回判断分类,1,-1
神经元算法实现完毕
要加载的数据,经过逗号隔开
读取数据,显示前十行
把第四列赋值给y
把字符串改为数字
把第0列和第2列抽取出来
数据可视化
数据能够线性分割
根据不一样颜色填充准备好的数据
利用meshgrid,构造2个维矩阵
把第一个元素拿出来,重复185次,成为第一行。能够重复255行矩阵
ravel:二维数组还原成扩展前的单维向量
把求和结果直接当成最终结果,比较
切线斜率正,减小神经元w份量的值
切线斜率负,增长神经元w份量的值
取得w最小值
适应性线性网络神经元代码完成
主要是fit函数算法不一样
上一节代码拷贝到以前工程文件下
##是修改过的地方
用图表查看训练过程
neuralNetwork.py
#-*- coding:utf-8 -*- import numpy as np ''' eta:学习率 n_iter:权重向量的训练次数 W:神经分叉权重向量 errors:用于记录神经元判断出错次数 ''' class Perceptron(object): # 初始化 def __init__(self, eta=0.01, n_iter=10): self.eta = eta self.n_iter = n_iter pass ''' 输入训练数据,培训神经元,x输入样本向量,y对应样本分类 X:shape[n_samples, n_features] X:[[1,2,3],[4,5,6]] n_samples: 2 n_features: 3 y:[1, -1] ''' def fit(self, X, y): #print('X',print(X)) #print ('X.shape[1]',X.shape[1]) #特征值有2个 self.W = np.zeros(1 + X.shape[1]) #+1是由于前面算法提到的W0,也就是步调函数阈值, #print ('W',self.W) #[0,0,0] self.errors = [] ''' X:[[1,2,3],[4,5,6]] y:[1,-1] zip(X,y) = [[1,2,3, 1],[4,5,6, -1]] ''' for _ in range(self.n_iter): error = 0 #初始化权重向量为0 dw = np.zeros(1 + X.shape[1]) # print('dw',dw)#出现20个[0,0,0] ''' zip:将对象中对应的元素打包成一个个元组,而后返回由这些元组组成的列表 >>> a = [1,2,3] >>> b = [4,5,6] >>> zipped = zip(a,b) # 打包为元组的列表 [(1, 4), (2, 5), (3, 6)] ''' for xi, target in zip(X, y): ''' update = η * (y - y`) xi 是一个向量 update * xi 等价: [∇w(1) = X[1] * update, ∇w(2) = X[2] *update, ∇w(3) = X[3] *update] ''' update = self.eta * (target - self.predict(xi)) dw[1:] += update * xi dw[0] += update error += int(update != 0) pass self.W += dw self.errors.append(error) pass pass ''' z = W0*1 + W1*X1 + ... Wn*Xn ''' def net_input(self, xi): return np.dot(self.W[1:], xi) + self.W[0] def predict(self, xi): return np.where(self.net_input(xi) > 0, 1, -1) pass # 文件读取 file = './iris.data.csv' import pandas as pd df = pd.read_csv(file, header=None) df.head(10) # 显示原始数据 import matplotlib.pyplot as plt import numpy as np y = df.loc[0:99, 4].values y = np.where(y == 'Iris-setosa', -1, 1) # X为第1和3列 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(u'花瓣长度') plt.ylabel(u'花径长度') plt.legend(loc='upper left') # plt.show() # 训练并打印错误曲线 ppn = Perceptron(0.1, 20) ppn.fit(X, y) print (ppn.W) plt.scatter(range(1, len(ppn.errors) + 1), ppn.errors, color='red', marker='o') # plt.show() # 定义打印分类器边界函数 from matplotlib.colors import ListedColormap def plot_decision_regions(X, y, classifier, resolution=0.02): markers = ('s', 'x', 'o', 'v') colors = ('red', 'blue', 'lightgreen', 'grey', 'cyan') cmap = ListedColormap(colors[:len(np.unique(y))]) x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() print(x1_min, x1_max) print(x2_min, x2_max) xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution)) # print(xx2.shape) # print(xx2) z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()])) # print(xx1.ravel()) # print(xx2.ravel()) # print(z) z = z.reshape(xx1.shape) plt.contourf(xx1, xx2, z, alpha=0.8, cmap=cmap) plt.xlim(xx1.min(), xx1.max()) plt.ylim(xx2.min(), xx2.max()) 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_regions(X, y, ppn) plt.xlabel(u'花瓣长度') plt.ylabel(u'花径长度') plt.legend(loc='upper left') # plt.show()
iris.data.csv
5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1.5,0.2,Iris-setosa 5.0,3.6,1.4,0.2,Iris-setosa 5.4,3.9,1.7,0.4,Iris-setosa 4.6,3.4,1.4,0.3,Iris-setosa 5.0,3.4,1.5,0.2,Iris-setosa 4.4,2.9,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.4,3.7,1.5,0.2,Iris-setosa 4.8,3.4,1.6,0.2,Iris-setosa 4.8,3.0,1.4,0.1,Iris-setosa 4.3,3.0,1.1,0.1,Iris-setosa 5.8,4.0,1.2,0.2,Iris-setosa 5.7,4.4,1.5,0.4,Iris-setosa 5.4,3.9,1.3,0.4,Iris-setosa 5.1,3.5,1.4,0.3,Iris-setosa 5.7,3.8,1.7,0.3,Iris-setosa 5.1,3.8,1.5,0.3,Iris-setosa 5.4,3.4,1.7,0.2,Iris-setosa 5.1,3.7,1.5,0.4,Iris-setosa 4.6,3.6,1.0,0.2,Iris-setosa 5.1,3.3,1.7,0.5,Iris-setosa 4.8,3.4,1.9,0.2,Iris-setosa 5.0,3.0,1.6,0.2,Iris-setosa 5.0,3.4,1.6,0.4,Iris-setosa 5.2,3.5,1.5,0.2,Iris-setosa 5.2,3.4,1.4,0.2,Iris-setosa 4.7,3.2,1.6,0.2,Iris-setosa 4.8,3.1,1.6,0.2,Iris-setosa 5.4,3.4,1.5,0.4,Iris-setosa 5.2,4.1,1.5,0.1,Iris-setosa 5.5,4.2,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 5.0,3.2,1.2,0.2,Iris-setosa 5.5,3.5,1.3,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa 4.4,3.0,1.3,0.2,Iris-setosa 5.1,3.4,1.5,0.2,Iris-setosa 5.0,3.5,1.3,0.3,Iris-setosa 4.5,2.3,1.3,0.3,Iris-setosa 4.4,3.2,1.3,0.2,Iris-setosa 5.0,3.5,1.6,0.6,Iris-setosa 5.1,3.8,1.9,0.4,Iris-setosa 4.8,3.0,1.4,0.3,Iris-setosa 5.1,3.8,1.6,0.2,Iris-setosa 4.6,3.2,1.4,0.2,Iris-setosa 5.3,3.7,1.5,0.2,Iris-setosa 5.0,3.3,1.4,0.2,Iris-setosa 7.0,3.2,4.7,1.4,Iris-versicolor 6.4,3.2,4.5,1.5,Iris-versicolor 6.9,3.1,4.9,1.5,Iris-versicolor 5.5,2.3,4.0,1.3,Iris-versicolor 6.5,2.8,4.6,1.5,Iris-versicolor 5.7,2.8,4.5,1.3,Iris-versicolor 6.3,3.3,4.7,1.6,Iris-versicolor 4.9,2.4,3.3,1.0,Iris-versicolor 6.6,2.9,4.6,1.3,Iris-versicolor 5.2,2.7,3.9,1.4,Iris-versicolor 5.0,2.0,3.5,1.0,Iris-versicolor 5.9,3.0,4.2,1.5,Iris-versicolor 6.0,2.2,4.0,1.0,Iris-versicolor 6.1,2.9,4.7,1.4,Iris-versicolor 5.6,2.9,3.6,1.3,Iris-versicolor 6.7,3.1,4.4,1.4,Iris-versicolor 5.6,3.0,4.5,1.5,Iris-versicolor 5.8,2.7,4.1,1.0,Iris-versicolor 6.2,2.2,4.5,1.5,Iris-versicolor 5.6,2.5,3.9,1.1,Iris-versicolor 5.9,3.2,4.8,1.8,Iris-versicolor 6.1,2.8,4.0,1.3,Iris-versicolor 6.3,2.5,4.9,1.5,Iris-versicolor 6.1,2.8,4.7,1.2,Iris-versicolor 6.4,2.9,4.3,1.3,Iris-versicolor 6.6,3.0,4.4,1.4,Iris-versicolor 6.8,2.8,4.8,1.4,Iris-versicolor 6.7,3.0,5.0,1.7,Iris-versicolor 6.0,2.9,4.5,1.5,Iris-versicolor 5.7,2.6,3.5,1.0,Iris-versicolor 5.5,2.4,3.8,1.1,Iris-versicolor 5.5,2.4,3.7,1.0,Iris-versicolor 5.8,2.7,3.9,1.2,Iris-versicolor 6.0,2.7,5.1,1.6,Iris-versicolor 5.4,3.0,4.5,1.5,Iris-versicolor 6.0,3.4,4.5,1.6,Iris-versicolor 6.7,3.1,4.7,1.5,Iris-versicolor 6.3,2.3,4.4,1.3,Iris-versicolor 5.6,3.0,4.1,1.3,Iris-versicolor 5.5,2.5,4.0,1.3,Iris-versicolor 5.5,2.6,4.4,1.2,Iris-versicolor 6.1,3.0,4.6,1.4,Iris-versicolor 5.8,2.6,4.0,1.2,Iris-versicolor 5.0,2.3,3.3,1.0,Iris-versicolor 5.6,2.7,4.2,1.3,Iris-versicolor 5.7,3.0,4.2,1.2,Iris-versicolor 5.7,2.9,4.2,1.3,Iris-versicolor 6.2,2.9,4.3,1.3,Iris-versicolor 5.1,2.5,3.0,1.1,Iris-versicolor 5.7,2.8,4.1,1.3,Iris-versicolor 6.3,3.3,6.0,2.5,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 7.1,3.0,5.9,2.1,Iris-virginica 6.3,2.9,5.6,1.8,Iris-virginica 6.5,3.0,5.8,2.2,Iris-virginica 7.6,3.0,6.6,2.1,Iris-virginica 4.9,2.5,4.5,1.7,Iris-virginica 7.3,2.9,6.3,1.8,Iris-virginica 6.7,2.5,5.8,1.8,Iris-virginica 7.2,3.6,6.1,2.5,Iris-virginica 6.5,3.2,5.1,2.0,Iris-virginica 6.4,2.7,5.3,1.9,Iris-virginica 6.8,3.0,5.5,2.1,Iris-virginica 5.7,2.5,5.0,2.0,Iris-virginica 5.8,2.8,5.1,2.4,Iris-virginica 6.4,3.2,5.3,2.3,Iris-virginica 6.5,3.0,5.5,1.8,Iris-virginica 7.7,3.8,6.7,2.2,Iris-virginica 7.7,2.6,6.9,2.3,Iris-virginica 6.0,2.2,5.0,1.5,Iris-virginica 6.9,3.2,5.7,2.3,Iris-virginica 5.6,2.8,4.9,2.0,Iris-virginica 7.7,2.8,6.7,2.0,Iris-virginica 6.3,2.7,4.9,1.8,Iris-virginica 6.7,3.3,5.7,2.1,Iris-virginica 7.2,3.2,6.0,1.8,Iris-virginica 6.2,2.8,4.8,1.8,Iris-virginica 6.1,3.0,4.9,1.8,Iris-virginica 6.4,2.8,5.6,2.1,Iris-virginica 7.2,3.0,5.8,1.6,Iris-virginica 7.4,2.8,6.1,1.9,Iris-virginica 7.9,3.8,6.4,2.0,Iris-virginica 6.4,2.8,5.6,2.2,Iris-virginica 6.3,2.8,5.1,1.5,Iris-virginica 6.1,2.6,5.6,1.4,Iris-virginica 7.7,3.0,6.1,2.3,Iris-virginica 6.3,3.4,5.6,2.4,Iris-virginica 6.4,3.1,5.5,1.8,Iris-virginica 6.0,3.0,4.8,1.8,Iris-virginica 6.9,3.1,5.4,2.1,Iris-virginica 6.7,3.1,5.6,2.4,Iris-virginica 6.9,3.1,5.1,2.3,Iris-virginica 5.8,2.7,5.1,1.9,Iris-virginica 6.8,3.2,5.9,2.3,Iris-virginica 6.7,3.3,5.7,2.5,Iris-virginica 6.7,3.0,5.2,2.3,Iris-virginica 6.3,2.5,5.0,1.9,Iris-virginica 6.5,3.0,5.2,2.0,Iris-virginica 6.2,3.4,5.4,2.3,Iris-virginica 5.9,3.0,5.1,1.8,Iris-virginica
1.神经网络入门:http://www.ruanyifeng.com/blog/2017/07/neural-network.html
2.github项目:https://github.com/a414351664/Perceptron-sort-algorithm