a(误差乘以一个系数alpha,调整每次修正的时候幅度,这个alpha也就是所谓的“学习率”,让误差乘以学习率是为了防止调整幅度过大,但太小的学习率会让这个调整的过程磨磨唧唧)
import dataset
from matplotlib import pyplot as plt
# 画豆豆散点图
xs, ys = dataset.get_beans(100)
print(xs)
print(ys)
plt.title("size-toxicity Fuction", fontsize=12)
plt.xlabel('bean size')
plt.ylabel("toxicity")
plt.scatter(xs, ys)
# 确定曲线
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
y_pre = w * xs
print(y_pre)
plt.plot(xs, y_pre)
plt.show()
dataset代码
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