【问题标题】:ValueError: Length of passed values is 1445, index implies 1ValueError:传递值的长度是 1445,索引意味着 1
【发布时间】:2020-11-09 14:49:29
【问题描述】:

我正在尝试进行一个非常简单的梯度下降实现,但在执行该函数时出现此错误,特别是指向在每个循环开始时计算预测值的 np.dot 函数。它给了我一些“ValueError:传递值的长度是 1445,索引意味着 1。”尽管点积是正确的,但它是 (1445, 4) * (4,1)。循环成功完成第一次迭代,然后抛出错误

代码如下:`

def gredientDescent(inputs, outputs, learning_rate) : 

weights = np.zeros((4,1))
bias = 0



for i in range(num_observations) :
    
    print(weights)

    
    predicted = np.dot(inputs, weights) +  bias



    
    deltas = predicted - outputs        
    
    
    cost = np.sum(deltas ** 2) / num_observations
    
    
    dw = np.dot(inputs.T, deltas)
    db = np.sum(deltas)
    
    
    
    weights =  weights - (learning_rate * dw)
    bias = bias - (learning_rate * db)
    
    print(weights)
            
    

    
gredientDescent(inputs,outputs, 0.001)

`

以及出现的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-177-5517d5583095> in <module>
     38 
     39 
---> 40 gredientDescent(inputs,outputs, 0.001)

<ipython-input-177-5517d5583095> in gredientDescent(inputs, outputs, learning_rate)
     11 
     12 
---> 13         predicted = np.dot(inputs, weights) +  bias
     14 
     15 

C:\anaconda3\envs\py3tf2\lib\site-packages\pandas\core\series.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
    634         # for binary ops, use our custom dunder methods
    635         result = ops.maybe_dispatch_ufunc_to_dunder_op(
--> 636             self, ufunc, method, *inputs, **kwargs
    637         )
    638         if result is not NotImplemented:

pandas\_libs\ops_dispatch.pyx in pandas._libs.ops_dispatch.maybe_dispatch_ufunc_to_dunder_op()

C:\anaconda3\envs\py3tf2\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
     62         other = item_from_zerodim(other)
     63 
---> 64         return method(self, other)
     65 
     66     return new_method

C:\anaconda3\envs\py3tf2\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right)
    503         result = arithmetic_op(lvalues, rvalues, op, str_rep)
    504 
--> 505         return _construct_result(left, result, index=left.index, name=res_name)
    506 
    507     wrapper.__name__ = op_name

C:\anaconda3\envs\py3tf2\lib\site-packages\pandas\core\ops\__init__.py in _construct_result(left, result, index, name)
    476     # We do not pass dtype to ensure that the Series constructor
    477     #  does inference in the case where `result` has object-dtype.
--> 478     out = left._constructor(result, index=index)
    479     out = out.__finalize__(left)
    480 

C:\anaconda3\envs\py3tf2\lib\site-packages\pandas\core\series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    290                     if len(index) != len(data):
    291                         raise ValueError(
--> 292                             f"Length of passed values is {len(data)}, "
    293                             f"index implies {len(index)}."
    294                         )

ValueError: Length of passed values is 1445, index implies 1.


【问题讨论】:

    标签: python-3.x pandas algorithm numpy jupyter-notebook


    【解决方案1】:

    这应该可行:

    weights = np.zeros((4,1))
    bias = 0
    
    
    
    for i in range(num_observations) :
    
        print(weights)
    
    
        predicted = np.dot(inputs, weights.T) +  bias
    
    
    
    
        deltas = predicted - outputs        
    
    
        cost = np.sum(deltas ** 2) / num_observations
    
    
        dw = np.dot(inputs.T, deltas)
        db = np.sum(deltas)
    
    
    
        weights =  weights - (learning_rate * dw)
        bias = bias - (learning_rate * db)
    
        print(weights)
            
    
    
    
    gredientDescent(inputs,outputs, 0.001)
    

    注意:

    1. predicted = np.dot(inputs, weights) + bias 更改为 predicted = np.dot(inputs, weights.T) + bias
    2. np.dot(inputs.T, deltas) 改为 np.dot(inputs, deltas.T)

    ,因为您希望通过其权重获得每个输入的乘法总和,而不是每个输入的权重乘法总和。 希望这可以帮助。干杯。

    【讨论】:

    • 你的意思是重量还是增量?当我这样做时,它抛出了一个错误,因为材料不匹配。提前致谢
    • 我认为这是因为预测 = np.dot(inputs, weights) + 偏差线。我已经添加了更正的代码。
    • ValueError: 形状 (1445,4) 和 (1,4) 未对齐:4 (dim 1) != 1 (dim 0)
    • 输入为 1445 * 4 ,输出应为 1445*1 。有四种权重和一种偏差
    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2023-03-12
    • 2013-11-06
    • 2018-12-02
    相关资源
    最近更新 更多