【发布时间】:2021-04-14 07:15:27
【问题描述】:
我正在尝试手动编写多元线性回归代码,为此我编写了这个逻辑,
#building the model
#lets start with a random value of m and c
import numpy as np
n_samples,n_features=X_train.shape
weight=np.zeros(n_features)
bias=0
lr=0.0001 #l is the learning rate
mse=[]
for i in range (0,20000):
Y_pred=np.dot(X_train,weight)+bias
mse.append(np.sum((Y_pred-Y_train)**2)/n_samples)
# compute gradients
dw = (1 / n_samples) * np.dot(X_train.T, (Y_pred - Y_train))
db = (1 / n_samples) * np.sum(Y_pred - Y_train)
# update parameters
weight -= lr * dw
bias -= lr * db
plt.plot(mse)
但我不知道为什么会出现以下错误
> ValueError Traceback (most recent call
> last) <ipython-input-17-6302f8353768> in <module>
> 22
> 23 # update parameters
> ---> 24 weight -= lr * dw
> 25 bias -= lr * db
> 26
>
> ValueError: operands could not be broadcast together with shapes (6,)
> (6,40) (6,)
请帮忙,因为我是 python 新手,我无法找出我哪里出错了。任何帮助都会非常有帮助。我已经看到this stackoverflow 问题,但我无法弄清楚我的错误。请帮忙。
【问题讨论】:
-
使用虚拟输入,我的工作正常。
X_train和Y_train的形状是什么? -
(40,6) for xtrain 和 (40,1) for y_train using jupyter notebook