0
こんにちは、回帰アルゴリズムについて学びたいと思っていましたが、勾配降下を伴う線形回帰を実装し、収束を決定するために残余平方和を使用しようとしました。 繰り返しのある時点では、残余平方和の評価がわかりますが、この問題を解決する方法はわかりません。私は何か間違っている?勾配降下を伴う線形回帰、平方和オーバーフローの残余
import math
import numpy as num
def get_regression_predictions(input_feature, intercept, slope):
predicted_output = [intercept + xi*slope for xi in input_feature]
return(predicted_output)
def get_residual_sum_of_squares(input_feature, output, intercept,slope):
return num.sum([(output.iloc[i] - (intercept + slope*input_feature.iloc[i]))**2 for i in range(0,len(output))])
def train(input_feature,output,intercept,slope):
#the start value of intercept and slope are 0
last = 0
now = math.sqrt(get_residual_sum_of_squares(input_feature,output,intercept,slope))
while abs(last - now) >= 0.01:
last = now
predictions = get_regression_predictions(input_feature,intercept,slope)
errors = [output.iloc[i] - predictions[i] for i in range(0,len(predictions))]
adjustements = (sum(errors)*0.05,sum([errors[i]*output.iloc[i] for i in range(0,len(errors))]) *0.05)
intercept ,slope = (intercept - adjustements[0],slope - adjustements[1])
now = math.sqrt(get_residual_sum_of_squares(input_feature,output,intercept,slope))
return intercept,slope
掲載されたコードを「そのまま」実行することができず、適合するデータがないようです。 –