Python 为什么我的线性回归梯度下降算法发散
线性回归的梯度下降会发散,即使我减小alpha的值,甚至删除它Python 为什么我的线性回归梯度下降算法发散,python,machine-learning,linear-regression,gradient-descent,Python,Machine Learning,Linear Regression,Gradient Descent,线性回归的梯度下降会发散,即使我减小alpha的值,甚至删除它 def gradientDescent(sqft_living, price, theta, alpha, num_iters): m = price.size # make a copy of theta, to avoid changing the original array, since numpy arrays # are passed by reference to functions theta = theta.co
def gradientDescent(sqft_living, price, theta, alpha, num_iters):
m = price.size
# make a copy of theta, to avoid changing the original array, since numpy arrays
# are passed by reference to functions
theta = theta.copy()
J_history = [] # Use a python list to save cost in every iteration
theta_history = []
for z in range(num_iters):
term = 0
term0 = 0
for i in range(m):
h = np.dot(theta, sqft_living[i])
term += (h - price[i])
term0 += term * sqft_living[i][0]
temp0 = theta[0] - alpha * (1/m) * term0
term = 0
term1 = 0
for i in range(m):
h = np.dot(theta, sqft_living[i])
term += (h - price[i])
term1 += term * sqft_living[i][1]
temp1 = theta[1] - alpha * (1/m) * term1
theta[0] = temp0
theta[1] = temp1
cost = computeCost(sqft_living, price, theta)
print(cost)
J_history.append(cost)
theta_history.append(theta)
return theta, J_history
这就是算法的使用
# initialize fitting parameters
theta = np.zeros(2)
# some gradient descent settings
iterations = 100
alpha = 0.00001
theta, J_history = gradientDescent(sqft_living, price, theta, alpha, iterations)
print(J_history)
这是输出的一部分:
成本=3.8144815615142405e+22
成本=8.337226954930875e+33
成本=1.8222489338606256e+45
成本=3.9828487760674E+56
成本=8.705222311669148e+67
成本=1.9026808508648173e+79
成本=4.158646718757122e+90
成本=9.089460549082665e+101
成本=1.9866629377457605e+113
成本=4.342204476162054e+124
成本=9.49065860875058e+135
成本=2.074351894810886e+147
成本=4.533864256311113e+158