import numpy as np import matplotlib.pyplotas plt from sklearn.linear_modelimport LinearRegression
# 生成一些随机数据
np.random.seed(0)
x =2 * np.random.rand(100,1)
y =4 + 3 * x + np.random.randn(100,1)
# 初始化参数
w =0
b =0
learning_rate =0.1
n_iterations =1000
# 梯度下降 for i inrange(n_iterations):
y_pred = w * x + b
dw = -(2/len(x)) * np.sum(x * (y - y_pred))
db = -(2/len(x)) * np.sum(y - y_pred)
w = w - learning_rate * dw
b = b - learning_rate * db