gpt4 book ai didi

python - 更新 numpy 后,np.lib.stride_tricks.as_strided 给出错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:03 25 4
gpt4 key购买 nike

我将 numpy 从旧版本更新到了 1.12.0 版本,不幸的是我没有检查哪个版本。我有一个在更新之前运行得非常好的脚本。它使用以下代码行:

from numpy.lib.stride_tricks import as_strided as _ast

raw_shp = _ast(raw, shape=sh, strides=stride).squeeze()

给出错误:

  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/stride_tricks.py", line 102, in as_strided
array = np.asarray(DummyArray(interface, base=x))
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: 'float' object cannot be interpreted as an index

_ast 或 np.asarray 发生了什么变化,引发了此错误?我该如何避免它?

感谢您的任何提示

最佳答案

显然,较新的 numpy 版本在索引时对使用 float 更加挑剔。将所有用于索引的 float 转换为整数解决了问题正如沃伦·韦凯瑟所说:确保 sh 中的所有值实际上都是整数,而不是整数值的 float 。要么修复 sh 的创建方式(推荐),要么执行类似 shape=tuple(int(i) for i in sh) 的操作。

关于python - 更新 numpy 后,np.lib.stride_tricks.as_strided 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42839388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com