Gradient Descient 小demo

数据:
x_data=[338,333,328,207,226,25,179,60,208,606]
y_data=[640,633,619,393,428,27,193,66,226,1591]

在图上的上述点在坐标系中的位置
Gradient Descient  小demo
找出直线拟合这些数据,从而达到预测的效果

import matplotlib.pyplot as plt
import numpy as np

x_data=[338,333,328,207,226,25,179,60,208,606]
y_data=[640,633,619,393,428,27,193,66,226,1591]

# y=b+w*x

b = -100
w = -4
lr = 0.0000001
iteration=100000

b_history = [b]
w_history = [w]

for i in range(iteration):
    b_grad = 0.0
    w_grad = 0.0
    for n in range(len(x_data)):
        b_grad = b_grad-2.0*(y_data[n] - b - w*x_data[n])*1.0
        w_grad = w_grad-2.0*(y_data[n] - b - w*x_data[n])*x_data[n]

    b=b - lr*b_grad
    w=w - lr*w_grad
    b_history.append(b)
    w_history.append(w)

plt.xlim(0,700)
plt.ylim(0, 1600)
plt.xlabel("x")
plt.ylabel("y")
for i in range(10):
    x=x_data[i]
    y=y_data[i]
    plt.plot([x], [y], 'ro')

# points = [(0, b), (700, 700*w+b)]
# (xpoints, ypoints) = zip(*points)
# plt.plot(xpoints, ypoints,color='red')

x = np.linspace(0, 700, 50)
for i in range(0,10):
    y = w_history[i]*x + b_history[i]
    plt.plot(x,y,color='black',label='w*x+b')
for i in range(len(w_history)-10,len(w_history)):
    y = w_history[i] * x + b_history[i]
    plt.plot(x, y, color='black', label='w*x+b')

plt.show()

效果图:
Gradient Descient  小demo
注:上述的学习率lr可以设置得小一点,不然很有可能出现数据大小超过计算机所能计算得范围。一开始我设置为0.001就出现这种情况

上一篇:使用海龟绘图,输出4个300*300的矩形


下一篇:垃圾mac