使用python语言实现线性回归算法:
x = [[1,2.1],[1,2.5],[1,3.9],[1,5.1],[1,2.7]]
y = [4.2,5.1,7.75,10.18,5.35]
theta = [5,1]
threshould = 0.01
alpha = 0.001
def calculateLoss(theta, x, y):
loss =0
for i in range (len(y)):
hypothesis = 0
for j in range(len(theta)):
hypothesis += theta[j] *x[i][j]
loss += (hypothesis - y[i])**2
return loss /2 /len(y)
def calculateTheta(theta, x, y, alpha):
newtheta = [0,0]
for thetaj in range(len(theta)):
sum =0
for i in range(len(y)):
hypothesis = 0
for j in range(len(theta)):
hypothesis += theta[j] * x[i][j]
sum += (hypothesis-y[i]) * x[i][thetaj]
step = sum / len (y) * alpha
newtheta[thetaj] = theta[thetaj] - step
return newtheta
def gradientDescent(theta, x, y, alpha, threshould):
while True:
#判断是否满足要求
if calculateLoss(theta, x, y) < threshould:
return theta
# 计算出一组新的thete
theta = calculateTheta(theta, x, y, alpha)
# 如果不满足要求,则再次进入循环
# res = gradientDescent(theta, x, y, alpha, threshould)
#
# print (res)
if __name__=="__main__":
res = gradientDescent(theta, x, y, alpha, threshould)
print(res)