python - ValueError:形状(20,14)和(13,1)未对齐:14(dim 1)!= 13(dim 0)
问题描述
def cal_cost(theta,X,y):
m = len(y)
predictions = X.dot(theta)
cost = (1/2*m) * np.sum(np.square(predictions-y))
return cost
def minibatch_gradient_descent(X,y,theta,learning_rate=0.01,iterations=10,batch_size =20):
m = len(y)
cost_history = np.zeros(iterations)
#n_batches = int(m/batch_size)
for it in range(iterations):
cost =0.0
indices = np.random.permutation(m)
X = X[indices]
y = y[indices]
for i in range(0,m,batch_size):
X_i = X[i:i+batch_size]
y_i = y[i:i+batch_size]
X_i = np.c_[np.ones(len(X_i)),X_i]
prediction = np.dot(X_i,theta)
theta = theta -(1/m)*learning_rate*( X_i.T.dot((prediction - y_i)))
cost += cal_cost(theta,X_i,y_i)
cost_history[it] = cost
return theta, cost_history
theta = np.zeros((X_train.shape[1], 1))
minibatch_gradient_descent(X_train,y_train,theta)
当我运行上面的代码时,我收到以下错误:
ValueError: shapes (20,14) and (13,1) not aligned: 14 (dim 1) != 13 (dim 0)
X_train.shape 是 (404,13),y_train.shape 是 (404,1)。我正在更新 theta 的值,但它仍然给我错误。
请帮忙。
解决方案
看起来你的错误发生在prediction = np.dot(X_i,theta)
. 如果您检查 的形状X_i
,它将打印出来(20,14)
。同样,如果您检查 的形状theta
,它会打印出来(13,1)
。现在您可以了解为什么不能采用X_i
&的点积theta
。由于您添加了一个额外的列来X_i
使用X_i = np.c_[np.ones(len(X_i)),X_i]
它的尺寸已经改变(最初是 (20,13) )。因此,您需要在数组中添加额外的行以theta
使维度为theta
as (14,1)
。
推荐阅读
- javascript - 将百分比相关验证添加到 html 输入框
- python - 根据数据框中的内容从 SQL Server 中删除行
- javascript - JAVASCRIPT 在用户 SCROLL DOWN 和 SCROLL TOP 时执行函数
- python - 如何在 python-telegram-bot 中获取 file_id(语音)?
- objective-c - 当我已经有 %end 时,如何修复“警告:缺少 %end(在 Tweak.x:14 打开的%hook 扩展到 EOF)”错误
- javascript - 如何通过 TTML 浏览并将所有时间\标题放入 JSON 文件
- python - 如何使用 CBV 或 FBV 从选定的表单中获取数据
- python - 无法将文件名作为参数传递给我的 python 脚本
- excel - Excel-VBA循环不循环遍历文件夹中的所有文件
- react-native - 媒体查询在 react-native 中不起作用