为了计算具有梯度下降的多元线性回归训练,我必须实现以下公式,我尝试了各种方法,但我想我缺少了一些东西。
我具有附加的(见上文)数据集,该数据集具有要训练的权重向量,即初始权重均为0,alpha和y_training集以及迭代次数。
vector_of_weights=[0,0,0]
我已经实现了一种预测方法:
def predict(x_featureVectors,trained_w):
results=[]
rows=x_featureVectors.shape[0]
for i in range(0,rows):
results.append(np.dot(x_featureVectors.iloc[i,:],trained_w))
results=pd.Series(results)
return results
我的问题是实现第一个公式,这是我到目前为止所做的。 结果是错误的,我将问题分解为小问题。
for
循环中j为零。i
,直到完成我的向量在第0位-4的结果将插入到临时列表中。
X_train_w_fictive_attr = insert_fictive_attr_to_df(X_train, fictive_attr_name)
temp_list=list_of_w=[0]*X_train_w_fictive_attr.shape[1]
rows=X_train_w_fictive_attr.shape[0]
prior=-1*alpha*2/rows
w0=0
for i in range(0,rows):
w0+=sum(predict(X_train_w_fictive_attr,list_of_w))#scalar multiply
w0-=y_train.iloc[i]
w0=w0*X_train_w_fictive_attr.iloc[i,0]
w0_trained=list_of_w[0]+(prior*w0)
temp_lst[0]=w0_trained
w1=0
for i in range(0,rows):
w1+=sum(predict(X_train_w_fictive_attr,list_of_w))
w1-=y_train.iloc[i]
w1*=X_train_w_fictive_attr.iloc[i,1]
w1_trained=prior*w1
temp_lst[1]=w1_trained
w2=0
for i in range(0,rows):
w2+=sum(predict(X_train_w_fictive_attr,list_of_w))
w2-=y_train.iloc[i]
w2*=X_train_w_fictive_attr.iloc[i,2]
w2_trained=prior*w2
temp_lst[2]=w2_trained
for i in range(0,len(temp_lst)):
list_of_w[i]=temp_lst[i]
#now we have w0,w1,w2...wN trained.
list_of_w=pd.Series(list_of_w)
return list_of_w