?
import numpy as np# 數據準備
X = np.array([1, 2, 3])
y = np.array([3, 5, 7])# 參數初始化
w0, w1 = 0, 0
alpha = 0.1
n = len(X)# 迭代10次
for epoch in range(10):# 計算預測值y_pred = w1 * X + w0# 計算梯度grad_w0 = (1/n) * np.sum(y_pred - y)grad_w1 = (1/n) * np.sum((y_pred - y) * X)# 更新參數w0 = w0 - alpha * grad_w0w1 = w1 - alpha * grad_w1# 計算損失函數loss = (1/(2*n)) * np.sum((y_pred - y)**2)print(f'Epoch {epoch+1}: w0={w0:.3f}, w1={w1:.3f}, Loss={loss:.3f}')
?
?六、代碼實現(Python示例)?
import numpy as np# 數據準備
X = np.array([1, 2, 3])
y = np.array([3, 5, 7])# 參數初始化
w0, w1 = 0, 0
alpha = 0.1
n = len(X)# 迭代10次
for epoch in range(10):# 計算預測值y_pred = w1 * X + w0# 計算梯度grad_w0 = (1/n) * np.sum(y_pred - y)grad_w1 = (1/n) * np.sum((y_pred - y) * X)# 更新參數w0 = w0 - alpha * grad_w0w1 = w1 - alpha * grad_w1# 計算損失函數loss = (1/(2*n)) * np.sum((y_pred - y)**2)print(f'Epoch {epoch+1}: w0={w0:.3f}, w1={w1:.3f}, Loss={loss:.3f}')
?七、總結?
- ?線性回歸?:通過線性模型擬合數據,核心是最小化預測誤差。
- ?梯度下降?:通過計算損失函數的梯度,逐步調整參數逼近最優解。
- ?核心公式?:參數更新規則?wj:=wj?α?J?wjwj?:=wj??α?wj??J?。
- ?實際應用?:需注意學習率選擇、特征縮放和收斂判斷。
"""
2.1線性回歸模型?與?梯度下降\
"""import numpy as np
import matplotlib.pyplot as plt# 設置隨機種子(保證可重復性)
np.random.seed(42)# 生成特征 X(單變量)和標簽 y
m = 100 # 樣本數量
X = 2 * np.random.rand(m, 1) # 生成 [0, 2) 之間的均勻分布數據
y = 4 + 3 * X + np.random.randn(m, 1) # 真實關系: y = 4 + 3X + 高斯噪聲# 可視化數據
plt.scatter(X, y, alpha=0.7)
plt.xlabel("X")
plt.ylabel("y")
plt.title("Simulated Linear Data")
plt.show()def linear_model(X, theta):"""線性回歸的預測函數"""# 公式: h_θ(X) = θ? + θ?X? + ... + θ?X?# X 形狀: (m, n+1)(包含偏置項 1)# theta 形狀: (n+1, 1)return X.dot(theta)def compute_cost(X, y, theta):"""計算均方誤差(MSE)損失函數"""m = len(y)predictions = linear_model(X, theta)error = predictions - ycost = (1 / (2 * m)) * np.sum(error ** 2) # 公式: J(θ) = 1/(2m) * Σ(hθ(X?) - y?)2return cost
def gradient_descent(X, y, theta, alpha, num_iters):"""批量梯度下降算法"""m = len(y)cost_history = [] # 記錄每次迭代的損失值for _ in range(num_iters):predictions = linear_model(X, theta)error = predictions - ygradients = (1 / m) * X.T.dot(error) # 公式: ?J(θ) = 1/m * X?(Xθ - y)theta -= alpha * gradients # 參數更新: θ := θ - α?J(θ)cost = compute_cost(X, y, theta)cost_history.append(cost)return theta, cost_history# 在特征矩陣 X 中添加偏置項(x? = 1)
X_b = np.c_[np.ones((m, 1)), X] # 形狀: (m, 2)# 初始化參數 θ(θ?, θ?)
theta_initial = np.random.randn(2, 1)# 設置超參數
alpha = 0.1 # 學習率
num_iters = 1000 # 迭代次數# 運行梯度下降
theta_optimized, cost_history = gradient_descent(X_b, y, theta_initial, alpha, num_iters)# 打印最優參數
print(f"最優參數: θ? = {theta_optimized:.3f}, θ? = {theta_optimized:.3f}")# 繪制損失函數下降曲線
plt.plot(range(num_iters), cost_history)
plt.xlabel("Iteration")
plt.ylabel("Cost (MSE)")
plt.title("Cost Function Convergence")
plt.show()# 繪制擬合直線和數據點
plt.scatter(X, y, alpha=0.7, label="Data")
plt.plot(X, X_b.dot(theta_optimized), color='red', linewidth=2, label="Linear Regression Fit")
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.title("Linear Regression with Gradient Descent")
plt.show()
?