【发布时间】: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