一元一次函数感知器: rosenblatt感知器

1. 数据文件

先写一个py文件,定义一个数据集:

import numpy as np

def get_beans(counts):
	xs = np.random.rand(counts)
	xs = np.sort(xs)
	ys = [1.2*x+np.random.rand()/10 for x in xs]
	return xs,ys

函数说明:

# np.random.rand(counts)创建counts个服从“0~1”均匀分
# 布的随机样本值。随机样本取值范围是[0,1),不包括1。
counts = 5
xs = np.random.rand(counts)
[0.70491788 0.42003216 0.65673763 0.76751605 0.47131048]
# np.sort(a,axis)函数的作用是对给定的数组的元素进行排序
# a:需要排序的数组
# axis:指定按什么排序,默认axis = 1 按行排序, axis = 0 按列排序
xs = np.sort(xs)
[0.42003216 0.47131048 0.65673763 0.70491788 0.76751605]

# 对于生成一个列表ys,其和xs的关系为1.2*xs+np.random.rand()/10
ys = [1.2*x+np.random.rand()/10 for x in xs]
[0.05303335450122437, 0.6920008171892412, 0.8394550531216658, 0.8587572065335468, 1.1020587170327967]
2. rosenblatt感知器

创建一个py文件,命名为rosenblatt.py

import dataset
from matplotlib import pyplot as plt
xs, ys = dataset.get_beans(100)

plt.title("size-toxicity function", fontsize = 12)
plt.xlabel("bean size")
plt.ylabel("toxicity")
plt.scatter(xs,ys)   # scatter用于绘制散点图


# 初始预测这个散点图的模型为y = 0.5*x
w = 0.5

for m in range(100):
	for i in range(100):
		x = xs[i]
		y = ys[i]
		y_pre = w * x
		e = y - y_pre
		alpha = 0.05
		w = w + alpha * e * x

# 这里 dataset里的随机数是numpy实现的
# 当一个数与一个数组进行乘法运算的时候,
# 自动把这个数广播给数组中的每一个元素,进行相乘
# 这就是numpy里的广播机制
y_pre = w * xs
print(y_pre)


plt.plot(xs, y_pre)
plt.show()
3. 预测结果
D:\lesson1>python rosenblatt.py
[0.03780218 0.04059785 0.0477705  0.0855943  0.12874181 0.13685395
 0.14489358 0.15819581 0.15873595 0.17213861 0.18243123 0.18766552
 0.19346648 0.19838957 0.20573188 0.20668577 0.24096686 0.24422348
 0.24707373 0.25030397 0.26286694 0.26648086 0.31443808 0.35684708
 0.3591003  0.39489619 0.39817362 0.40656083 0.41692886 0.41976208
 0.42143049 0.43370606 0.44166334 0.44266134 0.44797852 0.50634899
 0.54419973 0.54498475 0.56182277 0.5891967  0.59048749 0.61811849
 0.63202426 0.63321798 0.63505258 0.64797476 0.64997838 0.65315589
 0.67106602 0.67516753 0.67553516 0.7157855  0.73822292 0.75061685
 0.75788042 0.76475806 0.79805265 0.80329171 0.81531963 0.81951605
 0.83349578 0.84459298 0.84777837 0.84847466 0.84959395 0.85415659
 0.8768467  0.87854525 0.87910801 0.8931603  0.89518502 0.90568107
 0.92065886 0.93747641 0.94244445 0.96398146 0.97403517 1.01397134
 1.02901236 1.03930353 1.05150133 1.06272852 1.06790564 1.07580509
 1.07822583 1.08778958 1.11816681 1.12882552 1.13526779 1.14116164
 1.14473274 1.16009282 1.16715462 1.17221958 1.1767737  1.18421983
 1.19188772 1.19945019 1.21415735 1.24617466]

拟合效果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三十二画生H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值