类型错误:“ float”对象在stride_trick.as_strided

时间:2019-12-12 23:02:49

标签: python-3.x numpy scipy

尝试复制代码given here时。

import numpy as np
n=4
m=5
a = np.arange(1,n*m+1).reshape(n,m)

sz = a.itemsize
h,w = a.shape
bh,bw = 2,2
shape = (h/bh, w/bw, bh, bw)

strides = sz*np.array([w*bh,bw,w,1])


blocks=np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
print(blocks)

我收到以下错误消息,可能是什么原因?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-0c3a23be3e7f> in <module>
     12 
     13 
---> 14 blocks=np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
     15 print(blocks)

~\AppData\Local\Continuum\anaconda3\envs\dropletflow\lib\site-packages\numpy\lib\stride_tricks.py in as_strided(x, shape, strides, subok, writeable)
    100         interface['strides'] = tuple(strides)
    101 
--> 102     array = np.asarray(DummyArray(interface, base=x))
    103     # The route via `__interface__` does not preserve structured
    104     # dtypes. Since dtype should remain unchanged, we set it explicitly.

~\AppData\Local\Continuum\anaconda3\envs\dropletflow\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    499 
    500     """
--> 501     return array(a, dtype, copy=False, order=order)
    502 
    503 

TypeError: 'float' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:2)

您的形状是(2.0, 2.5, 2, 2),但是shape参数需要一个整数序列(如np.lib.stride_tricks.as_strided的API中所示)