您现在的位置是:首页 > 文章详情

python单层感知器

日期:2018-10-15点击:425

python的单向感知器

import numpy as np
import matplotlib.pyplot as plt #报表显示控件

start表示矩阵的长度和宽度

start, range_num = 5, 1000

输入数据

X = np.arange(start,start*(start+1)).reshape(start,start)

标签信息,Y的标签信息数目记得和Start数目一致

Y = np.array([1,1,-1,1,-1])

权值信息范围,介于-1到1之间

W = (np.random.random(start)-0.5)*2
print(W)

lr = 0.11 #学习率,一般介于0和1之间

表示迭代次数,O表示输出

n, O = 0, 0

更新权值函数

def update():

global X,Y,W,lr,n n += 1 #激活函数 O = np.sign(np.dot(X,W.T)) W_C = lr*((Y-O.T).dot(X))/int(X.shape[0]) W += W_C 

for _ in range(range_num):

update() O = np.sign(np.dot(X,W.T)) if (O==Y.T).all(): print("哈哈") break 

x1, y1, x2, y2 = [3,4], [3,3], [1], [1]
k, d = -W[1]/W[2], -W[0]/W[2]

xdata = np.linspace(0,10)
plt.figure()
plt.plot(xdata, xdata*k+d, 'r')
plt.plot(x1, y1, 'bo')
plt.plot(x2, y2, 'yo')
plt.show()

原文链接:https://yq.aliyun.com/articles/653405
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章