如何解决'numpy.float64个'对象不能解释为整数?

2024-06-28 11:27:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我犯了这个错误,有人能帮我吗?你知道吗

TypeError: 'numpy.float64' object cannot be interpreted as an integer.

   def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):


     win = window(frameSize)
     hopSize = int(frameSize - np.floor(overlapFac * frameSize))

     # zeros at beginning (thus center of 1st window should be for sample nr. 0)
     samples = np.append(np.zeros(int(frameSize/2.0)), sig)    
     # cols for windowing
     cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
     # zeros at end (thus samples can be fully covered by frames)
     samples = np.append(samples, np.zeros(frameSize))

     frames = stride_tricks.as_strided(samples, shape=(cols, frameSize),strides(samples.strides[0]*hopSize,samples.strides[0])).copy()
     frames *= win

     return np.fft.rfft(frames)
<ipython-input-113-e40a989a9c6b> in stft(sig, frameSize, overlapFac, window)
     10     samples = np.append(samples, np.zeros(frameSize))
     11 
---> 12     frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
     13     frames *= win
     14 

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

~\AppData\Roaming\Python\Python37\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    536 
    537     """
--> 538     return array(a, dtype, copy=False, order=order)
    539 
    540 

TypeError: 'numpy.float64' object cannot be interpreted as an integer

我不知道问题出在哪里,我读到的是python版本的问题,但不是,我不知道怎么解决!你知道吗


Tags: numpyframesasnpzerosbewindowwin
2条回答
def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
    win = window(frameSize)
    hopSize = int(frameSize - np.floor(overlapFac * frameSize))

    # zeros at beginning (thus center of 1st window should be for sample nr. 0)   
    samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)    
    # cols for windowing
    cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
    # zeros at end (thus samples can be fully covered by frames)
    samples = np.append(samples, np.zeros(frameSize))

    frames = stride_tricks.as_strided(samples, shape=(int(cols), frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
    frames *= win

    return np.fft.rfft(frames)    

最后,我找到了解决办法。谢谢!你知道吗

问题可能出在cols变量上。np.ceil返回np.float64;是的,它是一个整数值,但仍然是一个float数据类型。重读np.ceil文档。你知道吗

In [77]: np.ceil(1.23)                                                          
Out[77]: 2.0
In [78]: type(_)                                                                
Out[78]: numpy.float64

In [79]: np.ones((2,_77))                                                       
                                     -
TypeError                                 Traceback (most recent call last)
<ipython-input-79-af1036080a73> in <module>
  > 1 np.ones((2,_77))

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in ones(shape, dtype, order)
    212 
    213     """
 > 214     a = empty(shape, dtype, order)
    215     multiarray.copyto(a, 1, casting='unsafe')
    216     return a

TypeError: 'numpy.float64' object cannot be interpreted as an integer

math包中可以找到另一种方法:

In [81]: import math                                                            
In [82]: math.ceil                                                              
Out[82]: <function math.ceil>
In [83]: math.ceil(1.23)                                                        
Out[83]: 2
In [84]: np.ones((1,math.ceil(1.23)))                                           
Out[84]: array([[1., 1.]])

cols = int(cols)也应该起作用。你知道吗

相关问题 更多 >