我编写了python函数,但预测与事实不符,它预测的价格是负数。但是,我找不到错误的地方。当我计算导数[i]和权重[i]时是对还是错?请帮忙。
以下是图片中的功能所使用的功能:
def feature_derivative_ridge(errors, feature, weight, l2_penalty, feature_is_constant):
# If feature_is_constant is True, derivative is twice the dot product of errors and feature
if feature_is_constant == True:
derivative = 2*np.dot(errors, feature)
# Otherwise, derivative is twice the dot product plus 2*l2_penalty*weight
else:
derivative = (2*np.dot(errors, feature) + 2*l2_penalty*weight)
return derivative
最佳答案
哦,我找到了答案。
第一:错误=输出-预测
应该是:错误=预测
然后:weights [i] =(1 -....
应为:weights [i] = weights [i]-step_size * derivative [i]
(回想一下公式)
最后,输出正确