6-4 在线性回归模型中使用梯度下降法

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(666)
x = 2 * np.random.random(size=100)
y = x * 3. + 4. + np.random.normal(size=100)

X = x.reshape(-1, 1)

plt.scatter(x, y)
plt.show()

使用梯度下降法训练

输出结果: array([4.02145786, 3.00706277])

封装线性回归算法

使用fit_gd

输入:lin_reg.coef_ 输出:array([3.00706277])

输入:lin_reg.interception_ 输出:4.021457858204859

Last updated