利用梯度下降法求解“最小二乘法”各个参数问题
程序员文章站
2022-07-12 14:10:20
...
参考了这个博客中的数据
# -*- coding:utf-8 -*-
import numpy as np
import math
m = 20
X = [[1, 1],
[1, 2,],
[1, 3,],
[1, 4,],
[1, 5,],
[1, 6,],
[1, 7,],
[1, 8,],
[1, 9,],
[1,10,],
[1,11,],
[1,12,],
[1, 13,],
[1, 14,],
[1, 15,],
[1, 16,],
[1, 17,],
[1, 18,],
[1, 19,],
[1, 20,]
]
y = [3, 4, 5, 5, 2, 4, 7, 8, 11, 8, 12,
11, 13, 13, 16, 17, 18, 17, 19, 21]
alpha = 0.01
# 梯度函数
def gradient_function(theta, X, y):
diff = []
for index in range(len(y)):
diff.append((theta[0] * X[index][0] + theta[1] * X[index][1]) - y[index])
result = [0, 0]
for index in range(len(y)):
result[0] += (X[index][0] * diff[index])
result[1] += (X[index][1] * diff[index])
result[0] = result[0] / 20.0
result[1] = result[1] / 20.0
return result
def gradient_descent(X, y, alpha):
theta = [100000000000.0, 2.0]
gradient = gradient_function(theta, X, y)
while True :
theta[0] = theta[0] - (alpha * gradient[0])
theta[1] = theta[1] - (alpha * gradient[1])
gradient = gradient_function(theta, X, y)
print theta
return theta
gradient_descent(X, y, alpha)
下一篇: 梯度下降和最小二乘法