python离散建模之感知器学习算法
作者:努力奋斗的K崽 时间:2022-06-10 04:49:47
我们将研究一种判别式分类方法,其中直接学习评估 g(x)所需的 w 参数。我们将使用感知器学习算法。
感知器学习算法很容易实现,但为了节省时间,我在下面为您提供了一个实现。该函数有几个输入:训练数据、训练标签、对权重的初始猜测和学习率。注意,对于这两个类,类标签的值必须为+1和-1。
它将返回一个元组,其中包含:
1.学习w参数
2.执行的迭代次数
3.错误分类的样本数
花些时间检查代码。如果不清楚每一行是如何工作的,不要担心,只要让你自己知道每一行的目的是什么就可以了。代码中有一些注释可以帮助大家。
def perce(X, y, w_init, rho, max_iter=1000):
(N, nfeatures) = X.shape
# Augment the feature vectors by adding a 1 to each one. (see lecture notes)
X = np.hstack((X, np.ones((N, 1))))
nfeatures += 1
w = w_init # initialise weights
iter = 0
mis_class = N # start by assuming all samples are misclassified
while mis_class > 0 and iter < max_iter:
iter += 1
mis_class = 0
gradient = np.zeros(nfeatures) # initaliase the gradients to 0
# loop over every training sample.
for i in range(N):
# each misclassified point will cause the gradient to change
if np.inner(X[i, :], w) * y[i] <= 0:
mis_class += 1
gradient += -y[i] * X[i, :]
# update the weight vector ready for the next iteration
# Note, also that the learning rate decays over time (rho/iter)
w -= rho / iter * gradient
return w, iter, mis_class
解释:
X-数据矩阵。每行代表一个单独的样本
y-与X-标签行对应的整数类标签的一维数组必须为+1或-1
w_init-初始权重向量
rho-标量学习率
最大迭代次数-最大迭代次数(默认为1000)
def perce_fast(X, y, w_init, rho, max_iter=10000):
(N, nfeatures) = X.shape
X = np.hstack((X, np.ones((N, 1))))
nfeatures += 1
w = w_init
iter = 0
mis_class = N
yy = np.tile(y, (nfeatures, 1)).T
while mis_class > 0 and iter < max_iter:
iter += 1
# Compute set of misclassified points
mc = (np.dot(X, w.transpose()) * y) <= 0
mis_class = np.sum(mc)
# Update weights. Note, the learning rate decays over time (rho/iter)
w -= rho / iter * (np.sum(-yy[mc, :] * X[mc, :], axis=0))
return w, iter, np.sum(mc)
感知器算法的高效实现
对于笔记本电脑数据,此版本的工作速度将提高x100!
来源:https://blog.csdn.net/kirsten111111/article/details/121429528
标签:python,离散,建模,感知器,学习算法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python检测服务器端口代码实例
2023-07-07 06:34:14
使用Math.max,Math.min获取数组中的最值实例
2023-09-03 23:23:59
如何绝对获知浏览器类型?
2009-12-16 18:58:00
vbScript on error resume next容错使用心得
2010-06-26 19:28:00
django框架实现模板中获取request 的各种信息示例
2023-08-31 05:16:41
![](https://img.aspxhome.com/file/2023/8/61738_0s.jpg)
python使用turtle绘制分形树
2022-10-21 12:10:23
![](https://img.aspxhome.com/file/2023/2/77662_0s.jpg)
python fabric实现远程操作和部署示例
2021-07-18 14:22:14
关于python常见异常以及处理方法
2021-03-17 06:13:34
Python爬取腾讯视频评论的思路详解
2021-05-30 23:04:43
![](https://img.aspxhome.com/file/2023/2/70732_0s.jpg)
详解django中自定义标签和过滤器
2021-02-16 19:43:38
![](https://img.aspxhome.com/file/2023/3/75883_0s.png)
自动完成autoComplete
2011-01-17 18:01:00
解析:正确的理解SQL Server和XML支持
2009-01-23 13:52:00
golang 实现一个restful微服务的操作
2023-07-06 23:42:13
Python标准库与第三方库详解
2021-12-16 04:23:03
pytorch 一行代码查看网络参数总量的实现
2023-04-23 17:42:36
![](https://img.aspxhome.com/file/2023/8/89138_0s.jpg)
Python实现线性插值和三次样条插值的示例代码
2023-12-04 19:19:42
![](https://img.aspxhome.com/file/2023/2/88862_0s.png)
sqlserver 三种分页方式性能比较[图文]
2011-09-30 11:16:20
Python安装与卸载流程详细步骤(图解)
2023-10-05 19:36:21
![](https://img.aspxhome.com/file/2023/8/75148_0s.jpg)
Go项目实现优雅关机与平滑重启功能
2023-07-16 07:36:13
asp随机生成文件名的函数
2009-02-11 13:41:00