jupyter notebook 编写,windows建议创建虚拟环境,ubuntu直接安装
import numpy as np
import matplotlib.animation as animation
data = np.array([[80,200],[95,230],[104,245],[112,247],[125,259],[135,262]])
#m,b,mse的变化过程
mhistory = []
bhistory = []
msehistory = []
Weight = np.ones((2,1))
ones = np.ones((len(data),1))
Feature = np.hstack((data[:,0:1],ones))
print(Feature)
label = data[:,1:2]
print(label)
learningrate = 0.00002
v = np.zeros((2,1))
def gradentdecent():
global learningrate,v,Weight
mse = np.sum(np.square(np.dot(Feature,Weight)-label))
msehistory.append(mse)
#计算m和b的梯度
slop = np.dot(Feature.T,(np.dot(Feature,Weight)-label))
v = v*0.99-learningrate*slop
Weight = Weight+v
mhistory.append(Weight[0][0])
bhistory.append(Weight[1][0])
for i in range (50000):
gradentdecent()
%matplotlib notebook
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,6),dpi=60)
plt.xlim(0,5)
plt.ylim(0,130)
axis_name, = plt.plot(mhistory[0:100],bhistory[0:100],c='r')
plt.annotate("goal",xy=(1.0859,122.68), xytext=(+10, +15),
textcoords='offset points', fontsize=12,
arrowprops=dict(arrowstyle="->"))
def update(num):
axis_name.set_data(mhistory[0:num*100],bhistory[0:num*100])
animation.FuncAnimation(fig,update,np.arange(0,501),interval=20,repeat=False)
图形化展示: