python-3.x - ValueError:传递值的长度是 1445,索引意味着 1
问题描述
我正在尝试进行一个非常简单的梯度下降实现,但是在执行该函数时出现此错误,特别是指向在每个循环开始时计算预测值的 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.
解决方案
这应该有效:
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)
笔记:
- 预测 = np.dot(inputs, weights) + 偏差更改为预测 = np.dot(inputs, weights.T) + 偏差
- np.dot(inputs.T, deltas) 更改为 np.dot(inputs, deltas.T)
,因为您希望通过其权重获得每个输入的乘法总和,而不是每个输入的权重乘法总和。希望这可以帮助。干杯。
推荐阅读
- javascript - 如何使用 firestore 读取我的函数?
- amazon-web-services - EC2 实例存储性能下降
- amazon-web-services - Jenkins 与 AWS Fargate 使用 ECS-Fargate 插件
- fedora - Fedora Linux - 无法加载桌面错误 - 无法解析密钥符号 X86FullScreen
- sql - 色调中的奇怪符号
- python - 如果我有 3 个相同长度的值数组,我可以用搅拌机将它们绘制成 3d 吗?有没有一种只使用文本而不使用节点的简单方法?
- php - Laravel 5.8 在提交 ajax POST 时显示 CSRF 令牌不匹配
- javascript - 当数据来自 Javascript 并动态显示时,为什么我不能创建正确的弹性框?
- ssl - 获取桌面应用程序的有效 ssl 证书?
- fonts - 有没有办法在 Pycharm 中支持自定义连字?