![梯度下降法——一元线性回归(实战)_第1张图片](http://img.e-com-net.com/image/info8/0fb4b5c823d5449b9e2ee6035dc50862.png)
import numpy as np
import matplotlib.pyplot as plt
data = np.genfromtxt("data.csv", delimiter=",")
x_data = data[:,0]
y_data = data[:,1]
plt.scatter(x_data,y_data)
plt.show()
![梯度下降法——一元线性回归(实战)_第2张图片](http://img.e-com-net.com/image/info8/7fefcf9f2bcf43d59bf05ad4f7364ed3.png)
lr = 0.0001
b = 0
k = 0
epochs = 50
def compute_error(b, k, x_data, y_data):
totalError = 0
for i in range(0, len(x_data)):
totalError += (y_data[i] - (k * x_data[i] + b)) ** 2
return totalError / float(len(x_data)) / 2.0
def gradient_descent_runner(x_data, y_data, b, k, lr, epochs):
m = float(len(x_data))
for i in range(epochs):
b_grad = 0
k_grad = 0
for j in range(0, len(x_data)):
b_grad += (1/m) * (((k * x_data[j]) + b) - y_data[j])
k_grad += (1/m) * x_data[j] * (((k * x_data[j]) + b) - y_data[j])
b = b - (lr * b_grad)
k = k - (lr * k_grad)
return b, k
print("Starting b = {0}, k = {1}, error = {2}".format(b, k, compute_error(b, k, x_data, y_data)))
print("Running...")
b, k = gradient_descent_runner(x_data, y_data, b, k, lr, epochs)
print("After {0} iterations b = {1}, k = {2}, error = {3}".format(epochs, b, k, compute_error(b, k, x_data, y_data)))
plt.plot(x_data, y_data, 'b.')
plt.plot(x_data, k*x_data + b, 'r')
plt.show()
![梯度下降法——一元线性回归(实战)_第3张图片](http://img.e-com-net.com/image/info8/89ac79df855545c3afed2072a2c3a4ab.png)
或者你想要更好地观察过程,可以让它每五次画图一次
lr = 0.0001
b = 0
k = 0
epochs = 50
def compute_error(b, k, x_data, y_data):
totalError = 0
for i in range(0, len(x_data)):
totalError += (y_data[i] - (k * x_data[i] + b)) ** 2
return totalError / float(len(x_data)) / 2.0
def gradient_descent_runner(x_data, y_data, b, k, lr, epochs):
m = float(len(x_data))
for i in range(epochs):
b_grad = 0
k_grad = 0
for j in range(0, len(x_data)):
b_grad += (1/m) * (((k * x_data[j]) + b) - y_data[j])
k_grad += (1/m) * x_data[j] * (((k * x_data[j]) + b) - y_data[j])
b = b - (lr * b_grad)
k = k - (lr * k_grad)
if i % 5==0:
print("epochs:",i)
plt.plot(x_data, y_data, 'b.')
plt.plot(x_data, k*x_data + b, 'r')
plt.show()
return b, k
print("Starting b = {0}, k = {1}, error = {2}".format(b, k, compute_error(b, k, x_data, y_data)))
print("Running...")
b, k = gradient_descent_runner(x_data, y_data, b, k, lr, epochs)
print("After {0} iterations b = {1}, k = {2}, error = {3}".format(epochs, b, k, compute_error(b, k, x_data, y_data)))
![梯度下降法——一元线性回归(实战)_第4张图片](http://img.e-com-net.com/image/info8/7d99913dc910495bb51af6b793c3a414.png)
![梯度下降法——一元线性回归(实战)_第5张图片](http://img.e-com-net.com/image/info8/5a9199de872c4a9986fa5bba440d6d1a.jpg)
![梯度下降法——一元线性回归(实战)_第6张图片](http://img.e-com-net.com/image/info8/f4210bedf7fc4bda8008f5d5f8a89bef.jpg)
![梯度下降法——一元线性回归(实战)_第7张图片](http://img.e-com-net.com/image/info8/a5ed69c5ea8e4465b6653fcb9ed5b319.jpg)
![梯度下降法——一元线性回归(实战)_第8张图片](http://img.e-com-net.com/image/info8/6206133a9f4c465e86fac816a4d35e09.png)