28

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?

1
  • 2
    Why not lose the if and do... for j in range(int(np.floor(N[i]/2)))
    – Basic
    Commented Jun 2, 2014 at 21:33

7 Answers 7

27
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)):
2
  • I use Pyzo to run again, but it also reports type error: 'numpy.float64' object cannot be interpreted as an integer'
    – user3700852
    Commented Jun 2, 2014 at 21:23
  • 5
    well just see if you're doing the same mistake again; line numbers are not just for fun in the traceback.
    – Pavel
    Commented Jun 2, 2014 at 21:25
11

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.
0
4

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.

2

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
1
  • 1
    There is no link with the question. Is there one ?
    – Malo
    Commented Jun 8, 2021 at 22:01
1

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!

1
  • This approach will work, but you can't always be a nigilist of numpy, math libraries.
    – Eugene W.
    Commented Mar 12, 2021 at 19:34
0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.