0. 原始数据准备
0.1 导包
import numpy as np
import matplotlib.pyplot as plt
0.2 数据
x_data = np.arange(20)
y_data = np.array([0.4, 0.8, 1.1, 2.1, 2.8, 2.7, 3.5, 4.6, 5.1, 4.5, 6.0, 5.5, 6.9, 6.8, 7.6, 8.0, 8.8, 8.5, 9.5, 9.3])
print(x_data)
print(y_data)
0.3 作图展示
plt.scatter(x_data, y_data)
plt.show()
![原始数据](https://i-blog.csdnimg.cn/blog_migrate/43fecf9a0166b602ffa23470539302d9.png)
1. Python原生实现
1.1 计算均方差
def compute_mse(b, w, x_data, y_data):
"""
求均方差
:param b: 截距
:param w: 斜率
:param x_data: 特征数据
:param y_data: 标签数据
"""
total_error = 0.0
for i in range(0, len(x_data)):
total_error += (y_data[i] - (b + w * x_data[i])) ** 2
return total_error / len(x_data)
1.2 梯度下降
def run_gradient_descent(x_data, y_data, b, w, learn_rate, epochs):
"""
运行梯度下降
:param x_data: 待训练的特征数据
:param y_data: 标签数据
:param b: 截距
:param w: 斜率
:param learn_rate: 学习曲率
:param epochs: 训练次数
"""
m = float(len(x_data))
for i in range(epochs):
b_grad = 0
w_grad = 0
for j in range(0, len(x_data)):
b_grad += (1/m) * ((b + w * x_data[j]) - y_data[j])
w_grad += (1/m) * ((b + w * x_data[j]) - y_data[j]) * x_data[j]
b -= learn_rate * b_grad
w -= learn_rate * w_grad
if i % 50 == 0:
print("epochs:", i)
plt.plot(x_data, y_data, "b.")
plt.plot(x_data, b + w * x_data, "r")
plt.show()
print("mse:", compute_mse(b, w, x_data, y_data))
print("------------------------------------------------------------------------------------------------------------")
return b, w
1.3 开始运行
learn_rate = 0.0001
b = 0
w = 0
epochs = 500
print("Start args: b = {0}, w = {1}, mse= {2}".format(b, w, compute_mse(b, w, x_data, y_data)))
print("Running...")
b, w = run_gradient_descent(x_data, y_data, b, w, learn_rate, epochs)
print("Finish args: iterations = {0} b = {1}, w = {2}, mse= {3}".format(epochs, b, w, compute_mse(b, w, x_data, y_data)))
1.4 回归最终结果作图
plt.plot(x_data, y_data, "b.")
plt.plot(x_data, b + w * x_data, "r")
plt.show()
![回归结果](https://i-blog.csdnimg.cn/blog_migrate/e2327c3cd39b4a5117c181ea3f2d89f4.png)
2. sklearn实现
2.1 导包
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
2.2 建模与训练
x_data = x_data[:, np.newaxis]
y_data = y_data[:, np.newaxis]
model = LinearRegression()
model.fit(x_data, y_data)
2.3 结果展示
print("coefficients: ", model.coef_)
w = model.coef_[0]
print("intercept: ", model.intercept_)
b = model.intercept_
x_test = np.array([[7]])
predict = model.predict(x_test)
print("predict: ", predict)
2.4 回归最终结果作图
plt.plot(x_data, y_data, "b.")
plt.plot(x_data, b + w * x_data, "r")
plt.show()
![sklearn回归结果](https://i-blog.csdnimg.cn/blog_migrate/3917ddb1642b3ad03a1f84e32d87621f.png)