梯度公式:
import numpy as np
def linear_regression_gradient_descent(X, y, alpha, iterations):
h, w = X.shape
theta = np.zeros((w, 1))
for _ in range(iterations):
predictions = X @ theta
errors = y.reshape(h, 1) - predictions
updates = X.T @ errors / h
theta += alpha * updates
return np.round(theta.flatten(), 4)

京公网安备 11010502036488号