python "TypeError: 'numpy.float64' object cannot be interpreted as an integer"
Question:
This is the code which is produing the error:
import numpy as np
for i in range(len(x)):
if (np.floor(N[i]/2)==N[i]/2):
for j in range(N[i]/2):
pxd[i,j]=x[i]-(delta*j)*np.sin(s[i]*np.pi/180)
pyd[i,j]=y[i]-(delta*j)*np.cos(s[i]*np.pi/180)
else:
for j in range((N[i]-1)/2):
pxd[i,j]=x[i]-(delta*j)*np.sin(s[i]*np.pi/180)
pyd[i,j]=y[i]-(delta*j)*np.cos(s[i]*np.pi/180)
Does anyone have an idea as to how to solve this problem? How can I get the code to run successfully?
Answers:
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.
Similar situation. It was working. Then, I started to include pytables. At first view, no reason to errors. I decided to use another function, that has a domain constraint (elipse) and received the following error:
TypeError: 'numpy.float64' object cannot be interpreted as an integer
or
TypeError: 'numpy.float64' object is not iterable
The crazy thing: the previous function I was using, no code changed, started to return the same error. My intermediary function, already used was:
def MinMax(x, mini=0, maxi=1)
return max(min(x,mini), maxi)
The solution was avoid numpy
or math
:
def MinMax(x, mini=0, maxi=1)
x = [x_aux if x_aux > mini else mini for x_aux in x]
x = [x_aux if x_aux < maxi else maxi for x_aux in x]
return max(min(x,mini), maxi)
Then, everything calm again. It was like one library possessed max
and min
!
I had the same problems when I was training a retained object detection model (faster RCNN) and this worked for me perfectly:
pip uninstall pycocotools
pip install pycocotools-windows
While I appreciate this is not the OP’s problem, I just had this error message for a very different reason and this is the top result so I’m posting my problem and resolution here.
I had this code:
x = np.ndarray([1.0, 2.0, 3.0], dtype=np.float_)
Notice the subtle mistake? ndarray
is the numpy array class, but you usually don’t construct it directly. Instead you use the array()
helper function:
x = np.array([1.0, 2.0, 3.0], dtype=np.float_)
Switching to the second form solved my problem.
This problem may occur when we use an old version of numpy. In my case, I was using 1.18.5. I upgraded to 1.19.5 and the fail finished.
After this, if you are using Jupyter, you shall shutdown Kernell.
If you are running any Object detection algorithm and facing this issue, it is because of version conflicts in ‘pycocotools
‘. Uninstall and reinstall it, your problem will be solved.
pip uninstall pycocotools
pip install pycocotools
This is the code which is produing the error:
import numpy as np
for i in range(len(x)):
if (np.floor(N[i]/2)==N[i]/2):
for j in range(N[i]/2):
pxd[i,j]=x[i]-(delta*j)*np.sin(s[i]*np.pi/180)
pyd[i,j]=y[i]-(delta*j)*np.cos(s[i]*np.pi/180)
else:
for j in range((N[i]-1)/2):
pxd[i,j]=x[i]-(delta*j)*np.sin(s[i]*np.pi/180)
pyd[i,j]=y[i]-(delta*j)*np.cos(s[i]*np.pi/180)
Does anyone have an idea as to how to solve this problem? How can I get the code to run successfully?
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.
Similar situation. It was working. Then, I started to include pytables. At first view, no reason to errors. I decided to use another function, that has a domain constraint (elipse) and received the following error:
TypeError: 'numpy.float64' object cannot be interpreted as an integer
or
TypeError: 'numpy.float64' object is not iterable
The crazy thing: the previous function I was using, no code changed, started to return the same error. My intermediary function, already used was:
def MinMax(x, mini=0, maxi=1)
return max(min(x,mini), maxi)
The solution was avoid numpy
or math
:
def MinMax(x, mini=0, maxi=1)
x = [x_aux if x_aux > mini else mini for x_aux in x]
x = [x_aux if x_aux < maxi else maxi for x_aux in x]
return max(min(x,mini), maxi)
Then, everything calm again. It was like one library possessed max
and min
!
I had the same problems when I was training a retained object detection model (faster RCNN) and this worked for me perfectly:
pip uninstall pycocotools
pip install pycocotools-windows
While I appreciate this is not the OP’s problem, I just had this error message for a very different reason and this is the top result so I’m posting my problem and resolution here.
I had this code:
x = np.ndarray([1.0, 2.0, 3.0], dtype=np.float_)
Notice the subtle mistake? ndarray
is the numpy array class, but you usually don’t construct it directly. Instead you use the array()
helper function:
x = np.array([1.0, 2.0, 3.0], dtype=np.float_)
Switching to the second form solved my problem.
This problem may occur when we use an old version of numpy. In my case, I was using 1.18.5. I upgraded to 1.19.5 and the fail finished.
After this, if you are using Jupyter, you shall shutdown Kernell.
If you are running any Object detection algorithm and facing this issue, it is because of version conflicts in ‘pycocotools
‘. Uninstall and reinstall it, your problem will be solved.
pip uninstall pycocotools
pip install pycocotools