神经网络 感知机 Perceptron python实现
import numpy as np
import matplotlib.pyplot as plt
import math
def create_data(w1=3,w2=-7,b=4,seed=1,size=30):
np.random.seed(seed)
w = np.array([w1,w2])
x1 = np.arange(0,size)
v = np.random.normal(loc=0,scale=5,size=size)
x2 = v - (b+w[0]*x1)/(w[1]*1.0)
y_train=[]
x_train = np.array(zip(x1,x2))
for item in v:
if item >=0:
y_train.append(1)
else:
y_train.append(-1)
y_train = np.array(y_train)
return x_train,y_train
def SGD(x_train,y_train):
alpha=0.01
w,b=np.array([0,0]),0
c,i=0,0
while i<len(x_train):
if (x_train[i].dot(w)+b)*y_train[i] <=0:
c +=1
w=w+alpha*y_train[i]*x_train[i]
b=b+alpha*y_train[i]
print("count:%s index:%s w:%s:b:%s" %(c,i,w,b))
i=0
else:
i=i+1
return w,b
def test_and_show(w1,w2,b,size,w_estimate,b_estimate,x_train,y_train):
fig = plt.figure()
ax1 = fig.add_subplot(111)
plt.xlabel('x1')
plt.ylabel('x2')
x1 = np.arange(0,size+1,size)
x2 = -(b+w1*x1)/(w2*1.0)
ax1.plot(x1,x2,c="black")
x2 = -(b_estimate+w_estimate[0]*x1)/w_estimate[1]*1.0
ax1.plot(x1,x2,c="red")
for i in range(0,len(x_train)):
if y_train[i]>0:
ax1.scatter(x_train[i,0],x_train[i,1],c="r",marker='o')
else:
ax1.scatter(x_train[i,0],x_train[i,1],c="b",marker="^")
plt.show()
if __name__ == '__main__':
w1,w2,b=3,-7,4
size=50
x_train,y_train=create_data(w1,w2,b,1,size)
w_estimate,b_estimate=SGD(x_train,y_train)
test_and_show(w1,w2,b,size,w_estimate,b_estimate,x_train,y_train)
count:1 index:0 w:[0. 0.08693155]:b:0.01
count:2 index:9 w:[-0.09 0.05511436]:b:0.0
count:3 index:8 w:[-0.01 0.11106631]:b:0.01
count:4 index:9 w:[-0.1 0.07924912]:b:0.0
count:5 index:8 w:[-0.02 0.13520107]:b:0.01
count:6 index:9 w:[-0.11 0.10338388]:b:0.0
count:7 index:8 w:[-0.03 0.15933583]:b:0.01
count:8 index:9 w:[-0.12 0.12751864]:b:0.0
count:9 index:8 w:[-0.04 0.18347059]:b:0.01
count:10 index:9 w:[-0.13 0.1516534]:b:0.0
count:11 index:8 w:[-0.05 0.20760535]:b:0.01
count:12 index:9 w:[-0.14 0.17578815]:b:0.0
count:13 index:8 w:[-0.06 0.23174011]:b:0.01
count:14 index:9 w:[-0.15 0.19992291]:b:0.0
count:15 index:8 w:[-0.07 0.25587487]:b:0.01
count:16 index:9 w:[-0.16 0.22405767]:b:0.0
count:17 index:8 w:[-0.08 0.28000963]:b:0.01
count:18 index:9 w:[-0.17 0.24819243]:b:0.0
count:19 index:18 w:[0.01 0.33316026]:b:0.01
count:20 index:7 w:[-0.06 0.33550632]:b:0.0
count:21 index:9 w:[-0.15 0.30368913]:b:-0.01
count:22 index:18 w:[0.03 0.38865696]:b:0.0
count:23 index:7 w:[-0.04 0.39100302]:b:-0.01
count:24 index:9 w:[-0.13 0.35918582]:b:-0.02
count:25 index:16 w:[-0.29 0.29352152]:b:-0.03
count:26 index:8 w:[-0.21 0.34947347]:b:-0.02
count:27 index:18 w:[-0.03 0.4344413]:b:-0.01
count:28 index:9 w:[-0.12 0.40262411]:b:-0.02
count:29 index:9 w:[-0.21 0.37080691]:b:-0.03
count:30 index:18 w:[-0.03 0.45577474]:b:-0.02
count:31 index:9 w:[-0.12 0.42395755]:b:-0.03
count:32 index:9 w:[-0.21 0.39214035]:b:-0.04
count:33 index:18 w:[-0.03 0.47710818]:b:-0.03
count:34 index:9 w:[-0.12 0.44529098]:b:-0.04
count:35 index:9 w:[-0.21 0.41347379]:b:-0.05
count:36 index:18 w:[-0.03 0.49844162]:b:-0.04
count:37 index:9 w:[-0.12 0.46662442]:b:-0.05
count:38 index:9 w:[-0.21 0.43480723]:b:-0.06
count:39 index:18 w:[-0.03 0.51977506]:b:-0.05
count:40 index:9 w:[-0.12 0.48795786]:b:-0.06
count:41 index:9 w:[-0.21 0.45614067]:b:-0.07
count:42 index:44 w:[0.23 0.65296677]:b:-0.06
count:43 index:7 w:[0.16 0.65531283]:b:-0.07
count:44 index:7 w:[0.09 0.65765889]:b:-0.08
count:45 index:7 w:[0.02 0.66000495]:b:-0.09
count:46 index:9 w:[-0.07 0.62818775]:b:-0.1
count:47 index:9 w:[-0.16 0.59637056]:b:-0.11
count:48 index:9 w:[-0.25 0.56455336]:b:-0.12
count:49 index:44 w:[0.19 0.76137946]:b:-0.11
count:50 index:7 w:[0.12 0.76372552]:b:-0.12
count:51 index:7 w:[0.05 0.76607158]:b:-0.13
count:52 index:7 w:[-0.02 0.76841764]:b:-0.14
count:53 index:9 w:[-0.11 0.73660045]:b:-0.15
count:54 index:9 w:[-0.2 0.70478325]:b:-0.16
count:55 index:9 w:[-0.29 0.67296605]:b:-0.17
count:56 index:35 w:[-0.64 0.517885]:b:-0.18
count:57 index:8 w:[-0.56 0.57383695]:b:-0.17
count:58 index:8 w:[-0.48 0.62978891]:b:-0.16
count:59 index:8 w:[-0.4 0.68574086]:b:-0.15
count:60 index:18 w:[-0.22 0.77070869]:b:-0.14
count:61 index:9 w:[-0.31 0.7388915]:b:-0.15
count:62 index:35 w:[-0.66 0.58381044]:b:-0.16
count:63 index:8 w:[-0.58 0.6397624]:b:-0.15
count:64 index:8 w:[-0.5 0.69571435]:b:-0.14
count:65 index:8 w:[-0.42 0.75166631]:b:-0.13
count:66 index:18 w:[-0.24 0.83663414]:b:-0.12
count:67 index:9 w:[-0.33 0.80481694]:b:-0.13
count:68 index:26 w:[-0.59 0.6938186]:b:-0.14
count:69 index:8 w:[-0.51 0.74977055]:b:-0.13
count:70 index:8 w:[-0.43 0.8057225]:b:-0.12
count:71 index:18 w:[-0.25 0.89069034]:b:-0.11
count:72 index:9 w:[-0.34 0.85887314]:b:-0.12
count:73 index:16 w:[-0.5 0.79320884]:b:-0.13
count:74 index:18 w:[-0.32 0.87817667]:b:-0.12
count:75 index:16 w:[-0.48 0.81251236]:b:-0.13
count:76 index:18 w:[-0.3 0.89748019]:b:-0.12
count:77 index:9 w:[-0.39 0.865663]:b:-0.13
count:78 index:44 w:[0.05 1.0624891]:b:-0.12
count:79 index:9 w:[-0.04 1.0306719]:b:-0.13
count:80 index:9 w:[-0.13 0.99885471]:b:-0.14
count:81 index:9 w:[-0.22 0.96703751]:b:-0.15
count:82 index:9 w:[-0.31 0.93522032]:b:-0.16
count:83 index:9 w:[-0.4 0.90340312]:b:-0.17
神经网络 感知机 Perceptron python实现的更多相关文章
- 2. 感知机(Perceptron)基本形式和对偶形式实现
1. 感知机原理(Perceptron) 2. 感知机(Perceptron)基本形式和对偶形式实现 3. 支持向量机(SVM)拉格朗日对偶性(KKT) 4. 支持向量机(SVM)原理 5. 支持向量 ...
- 吴裕雄 python 机器学习——人工神经网络感知机学习算法的应用
import numpy as np from matplotlib import pyplot as plt from sklearn import neighbors, datasets from ...
- 感知机(python实现)
感知机(perceptron)是二分类的线性分类模型,输入为实例的特征向量,输出为实例的类别(取+1和-1).感知机对应于输入空间中将实例划分为两类的分离超平面.感知机旨在求出该超平面,为求得超平面导 ...
- BP神经网络原理及python实现
[废话外传]:终于要讲神经网络了,这个让我踏进机器学习大门,让我读研,改变我人生命运的四个字!话说那么一天,我在乱点百度,看到了这样的内容: 看到这么高大上,这么牛逼的定义,怎么能不让我这个技术宅男心 ...
- 20151227感知机(perceptron)
1 感知机 1.1 感知机定义 感知机是一个二分类的线性分类模型,其生成一个分离超平面将实例的特征向量,输出为+1,-1.导入基于误分类的损失函数,利用梯度下降法对损失函数极小化,从而求得此超平面,该 ...
- 感知机(perceptron)概念与实现
感知机(perceptron) 模型: 简答的说由输入空间(特征空间)到输出空间的如下函数: \[f(x)=sign(w\cdot x+b)\] 称为感知机,其中,\(w\)和\(b\)表示的是感知机 ...
- 神经网络(BP)算法Python实现及简单应用
首先用Python实现简单地神经网络算法: import numpy as np # 定义tanh函数 def tanh(x): return np.tanh(x) # tanh函数的导数 def t ...
- 深层神经网络框架的python实现
概述 本文demo非常适合入门AI与深度学习的同学,从最基础的知识讲起,只要有一点点的高等数学.统计学.矩阵的相关知识,相信大家完全可以看明白.程序的编写不借助任何第三方的深度学习库,从最底层写起. ...
- 机器学习(4):BP神经网络原理及其python实现
BP神经网络是深度学习的重要基础,它是深度学习的重要前行算法之一,因此理解BP神经网络原理以及实现技巧非常有必要.接下来,我们对原理和实现展开讨论. 1.原理 有空再慢慢补上,请先参考老外一篇不错的 ...
随机推荐
- 回忆之placeholder
直接看效果点这里 HTML <!DOCTYPE html> <html> <head lang="zh-CN"> <meta charse ...
- CentOS8部署nextcloud网盘
Nextcloud是一款开源的存储软件,功能丰富,支持多人协同工作,目前完全免费. 官网:https://www.nextcloud.com 架构:LAMP或LNMP 本文以LAMP为基础 注意:ph ...
- C# lambda 实现 Ascii 排序
var dir = new Dictionary<string, string>(); dir.Add("channelId", "1& ...
- Jmeter扩展组件开发(2) - 扩展开发第一个demo的实现
maven工程src目录介绍 main:写代码 main/java:写Java代码 main/resources:写配置文件 test:写测试代码 test/java demo实现 创建Package ...
- python 深度学习 库文件安装出错汇总
Cython_bbox FairMOT | win10下cython-bbox安装的心酸之路_是阳阳呀的博客-CSDN博客 swig 安装polyiou.py https://blog.csdn.ne ...
- web自动化:IE11运行Python+selenium程序
from selenium import webdriver # 运行此脚本前必须按要求修改注册表'''[HKEY_CURRENT_USER\Software\Microsoft\Internet E ...
- AT4353-[ARC101D]Robots and Exits【LIS】
正题 题目链接:https://www.luogu.com.cn/problem/AT4353 题目大意 数轴上有\(n\)个球\(m\)个洞,每次可以将所有球左移或者右移,球到洞的位置会掉下去. 求 ...
- CF622F-The Sum of the k-th Powers【拉格朗日插值】
正题 题目链接:https://www.luogu.com.cn/problem/CF622F 题目大意 给出\(n,k\),求 \[\sum_{i=1}^ni^k \] 解题思路 很经典的拉格朗日差 ...
- 用Fiddler抓不到https的包?因为你姿势不对!往这看!
前言 刚入行测试的小伙伴可能不知道,Fiddler默认抓http的包,如果要抓https的包,是需要装证书的!什么鬼证书?不明白的话继续往下看. Fiddler 抓取 https 数据 第一步:下载 ...
- virtualbox 桥接模式网络配置虚拟机之间通讯以及虚拟机联网
一般来说桥接模式可以解决所有的网络问题 网卡选择 [root@HELLO network-scripts]# cat ifcfg-eth0 TYPE="Ethernet" PROX ...