gpt4 book ai didi

machine-learning - 梯度下降算法引发 valueError

转载 作者:行者123 更新时间:2023-11-30 09:16:38 25 4
gpt4 key购买 nike

我有这些用于多元回归的梯度下降算法,但它提出了

ValueError: operands could not be broadcast together with shapes (3,) (3,140).

我在 stackoverflow 上查看了有关广播错误的其他答案以及说明矩阵的维度必须相同或其中一个矩阵必须为 1 的文档。但是我怎样才能使我的 theta 具有相同的维度。

请不要将其标记为重复。

我的 x 有暗淡 (140,3) ,y 有 (140,1),alpha=0.0001

def find_mse(x,y,theta):
return np.sum(np.square(np.matmul(x,theta)-y))*1/len(x)



def gradientDescent(x,y,theta,alpha,iteration):
theta=np.zeros(x.shape[1])
m=len(x)
gradient_df=pd.DataFrame(columns=['coeffs','mse'])

for i in range(iteration):
gradient = (1/m) * np.matmul(x.T, np.matmul(x, theta) - y)
theta = np.mat(theta) - alpha * gradient
cost = compute_cost(X, y, theta)
gradient_df.loc[i] = [theta,cost]

return gradient_df

最佳答案

您将 x 与形状 (140, 3)theta 相乘,以产生形状应为 (140, 1)。要实现此目的,您的 theta 形状应为 (3, 1)。您需要按如下方式初始化theta

theta = np.zeros((x.shape[1], y.shape[1]))

关于machine-learning - 梯度下降算法引发 valueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54459314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com