Python does not follow the same rules as written math. You must explicitly indicate multiplication.
Bad:
(a)(b)
(unless a is a function)
Good:
(a) * (b)
Answer from Ignacio Vazquez-Abrams on Stack Overflow'numpy.float64' object cannot be interpreted as an integer
python - TypeError: 'numpy.float64' object is not callable - Stack Overflow
`np.float64` is not accepted as `float` under static type checks
python - How to stop numpy floats being displayed as "np.float64"? - Stack Overflow
I am trying to do a spectral flux analysis of an .wav input signal and I am getting this error and I don't know how to fix it. The code is:
! The errors are fixed now and the solutions are added as comments. Thank you all for the help ^
import numpy as np
from numpy.lib import stride_tricks
def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
from numpy.lib import stride_tricks
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(np.floor(frameSize/2.0)), sig)
#fix error:samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)
# cols for windowing
#fix error:cols = int(np.ceil( (len(samples) - frameSize) /float(hopSize))+1)
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)spectral flux function:
def spectral_Flux(wavedata, window_size, sample_rate):
# convert to frequency domain
magnitude_spectrum = stft(wavedata, window_size)
timebins, freqbins = np.shape(magnitude_spectrum)
# when do these blocks begin (time in seconds)?
timestamps = (np.arange(0,timebins - 1) * (timebins / float(sample_rate)))
sf = np.sqrt(np.sum(np.diff(np.abs(magnitude_spectrum))**2, axis=1)) / freqbins
return sf[1:] #, np.asarray(timestamps)
the Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-157-54c4d632ffbf> in <module>
----> 1 sFlux=spectral_Flux(data,512,fs)
<ipython-input-156-f14509755e4e> in spectral_Flux(wavedata, window_size, sample_rate)
1 def spectral_Flux(wavedata, window_size, sample_rate):
2 # convert to frequency domain
----> 3 magnitude_spectrum = stft(wavedata, window_size)
4 timebins, freqbins = np.shape(magnitude_spectrum)
5
<ipython-input-155-b145f5690a25> in stft(sig, frameSize, overlapFac, window)
11
12 # zeros at beginning (thus center of 1st window should be for sample nr. 0)
---> 13 samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)
14 # cols for windowing
15 cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
TypeError: 'numpy.float64' object cannot be interpreted as an integerThe seccond error:
TypeError Traceback (most recent call last)
<ipython-input-193-54c4d632ffbf> in <module>
----> 1 sFlux=spectral_Flux(data,512,fs)
<ipython-input-192-f14509755e4e> in spectral_Flux(wavedata, window_size, sample_rate)
1 def spectral_Flux(wavedata, window_size, sample_rate):
2 # convert to frequency domain
----> 3 magnitude_spectrum = stft(wavedata, window_size)
4 timebins, freqbins = np.shape(magnitude_spectrum)
5
<ipython-input-191-0ee2f8ea37ed> in stft(sig, frameSize, overlapFac, window)
17 samples = np.append(samples, np.zeros(frameSize))
18
---> 19 frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
20 frames *= win
21
C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\stride_tricks.py in as_strided(x, shape, strides, subok, writeable)
99 interface['strides'] = tuple(strides)
100
--> 101 array = np.asarray(DummyArray(interface, base=x))
102 # The route via `__interface__` does not preserve structured
103 # dtypes. Since dtype should remain unchanged, we set it explicitly.
C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
TypeError: 'numpy.float64' object cannot be interpreted as an integer
Python does not follow the same rules as written math. You must explicitly indicate multiplication.
Bad:
(a)(b)
(unless a is a function)
Good:
(a) * (b)
This error also occurs when your function has the same name as your return value
def samename(a, b):
samename = a*b
return samename
This might be a super rookie mistake, I am curious how often this answer will be helpful.
numpy allows you to control this without downgrading:
np.set_printoptions(legacy='1.25')
See numpy.set_printoptions for details.
This is also stated here: Representation of NumPy scalars changed - NumPy 2.0.0 Release Notes
This is due to a change in how scalars are printed in numpy 2:
numpy 1.x.x:
>>> repr(np.array([1.0])[0])
'1.0'
numpy 2.x.x:
>>> repr(np.array([1.0])[0])
'np.float64(1.0)'
You should restrict the version of numpy to be 1.x.x in your requirements file to make sure you don't end up installing numpy 2.x.x:
numpy ~> 1.26
(same as numpy >= 1.26, == 1.*, see this answer)
or update your code to work with numpy 2 and change it to numpy ~> 2.0.
N=np.floor(np.divide(l,delta))
...
for j in range(N[i]/2):
N[i]/2 will be a float64 but range() expects an integer. Just cast the call to
for j in range(int(N[i]/2)):
I came here with the same Error, though one with a different origin.
It is caused by unsupported float index in 1.12.0 and newer numpy versions even if the code should be considered as valid.
An int type is expected, not a np.float64
Solution: Try to install numpy 1.11.0
sudo pip install -U numpy==1.11.0.