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

标签 python numpy

我将 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/

相关文章:

python - numpy char.array 从字符串中获取字符集

python - Scipy signal.find_peaks_cwt 挂断

python - 类型错误 : "quotechar" must be an 1-character string

python - 如何从 2 个值集的列表中提取 ID 列表

python - Numpy 和 16 位 PGM

python - 列出 ipython 和 jupyter 中的内存使用情况

python - 如何一次从 numpy 数组中排除几个范围?

python - 如何让我的程序根据 key.is_pressed 语句播放声音?

python - 如何使用 Python Regex 忽略字符

python - 结果取决于 python 中的其他结果