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

Deadly 提交于 2019-12-10 12:30:27

问题


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 has an idea of solving this problem? Running these codes successfully?

Thanks,
Jeremy


回答1:


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


来源:https://stackoverflow.com/questions/24003431/python-typeerror-numpy-float64-object-cannot-be-interpreted-as-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!