首页 > 解决方案 > 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 的值,但它仍然给我错误。

请帮忙。

标签: pythonpython-3.xmachine-learninggradient-descent

解决方案


看起来你的错误发生在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使维度为thetaas (14,1)


推荐阅读