梯度下降
用matplotlib画出梯度下降中的点
- plt,scatter()画出点,c代表颜色,s代表大小
- plt.plot(),label可以得到图像的标签,但是最后要用plt.legend()才可以显示出label
import numpy as np
import matplotlib.pyplot as plt
import math
def grad(x):
return 2*x*math.sin(x)+x**2*math.cos(x)
def draw():
x = 28
for i in range(5):
x = x - 0.001*grad(x)
y = x**2*math.sin(x)
plt.scatter(x,y,c="r",s=20)
x = np.linspace(0,np.pi*10,1000)
y=x**2*np.sin(x)
plt.plot(x, y, color="blue", label='x^2sinx')
draw()
plt.legend()
plt.show()