【问题标题】:ValueError: Length of values (959) does not match length of index (5)ValueError:值的长度 (959) 与索引的长度 (5) 不匹配
【发布时间】:2022-01-13 15:57:20
【问题描述】:

请帮我解决以下错误。在stackoverflow上尝试了各种帖子,仍然无法弄清楚。即使形状和数据框完好无损且形状正确,它也会为循环的第二次迭代抛出值错误。

提前致谢。

Please find the link of the Jupyter notebook here

def compute_cost(X,Y,W,b,lambda_=0):
    
    m = Y.shape[0]
    print(X.shape,W.shape)
    
    Z = np.dot(X,W) + b
    A = sigmoid(Z)
    J = (1/m) * np.sum((-Y * np.log(A) - (1 - Y) * np.log(1 - A)).values)
    
    return J.item(), A
def gradient_descent(X,Y,alpha=0.1,num_iters=100,lambda_=0):
    
    m = Y.shape[0]
    W = initialiaze_weights(X,Y) 
    b = 1
    
    for i in range(num_iters): 
        print('loop'+str(i))
        J, A = compute_cost(X,Y,W,b,lambda_)
        grad_W = (1/m) * np.dot(X.T,(A - Y)) # 959x11-959x5
        grad_b = (1/m) * np.sum(A - Y)
        #print(grad_W)
        W = W - alpha * grad_W
        b = b - alpha * grad_b
    
    return J
gradient_descent(X_train_norm,Y_train_dum)
loop0
(959, 11) (11, 5)
loop1
(959, 11) (11, 5)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-390-465bed6a5594> in <module>
----> 1 gradient_descent(X_train_norm,Y_train_dum)

<ipython-input-378-286b8d84d0b7> in gradient_descent(X, Y, alpha, num_iters, lambda_)
      7     for i in range(num_iters):
      8         print('loop'+str(i))
----> 9         J, A = compute_cost(X,Y,W,b,lambda_)
     10         grad_W = (1/m) * np.dot(X.T,(A - Y)) # 959x11-959x5
     11         grad_b = (1/m) * np.sum(A - Y)

<ipython-input-387-a8b943dff921> in compute_cost(X, Y, W, b, lambda_)
      4     print(X.shape,W.shape)
      5 
----> 6     Z = np.dot(X,W) + b
      7     A = sigmoid(Z)
      8     J = (1/m) * np.sum((-Y * np.log(A) - (1 - Y) * np.log(1 - A)).values)

c:\python38\lib\site-packages\pandas\core\generic.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
   2030         self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any
   2031     ):
-> 2032         return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
   2033 
   2034     # ideally we would define this to avoid the getattr checks, but
---skipped----some---part--of---this----error--
c:\python38\lib\site-packages\pandas\core\common.py in require_length_match(data, index)
    529     """
    530     if len(data) != len(index):
--> 531         raise ValueError(
    532             "Length of values "
    533             f"({len(data)}) "

ValueError: Length of values (959) does not match length of index (5)

【问题讨论】:

    标签: python pandas numpy data-science valueerror


    【解决方案1】:

    如果我在将数据帧发送到梯度下降之前将其转换为 numpy,则此错误得到解决。

    gradient_descent(X_train_norm.to_numpy(),Y_train_dum.to_numpy())
    

    但仍在寻找错误发生的原因。

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多