在学习人工智能时,使用matplotlib绘制散点图以及预测函数时希望能够动态的显示每一次预测的线性函数而不是只展现结果
import numpy as np
from matplotlib import pyplot as plt
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
xs,ys = get_beans(100)
plt.title("test")
plt.xlabel("Bean size")
plt.ylabel("Toxicity")
plt.scatter(xs,ys)
w=0.5
alpha = 0.05
for i in range(100):
for i in range(100):
x = xs[i]
y = ys[i]
y_pre = x * w
w = w + alpha*(y-y_pre)*x
y_pre = xs * w
plt.plot(xs, y_pre)
plt.show()