python“类型错误:'numpy.float64'对象不能解释为整数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24003431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:48:50  来源:igfitidea点击:

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

pythonloopsnestedinteger

提问by

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?

有没有人有解决这个问题的想法?成功运行这些代码?

采纳答案by Pavel

N=np.floor(np.divide(l,delta))
...
for j in range(N[i]/2):

N[i]/2will be a float64but range()expects an integer. Just cast the call to

N[i]/2将是一个float64range()需要一个整数。只需将电话转至

for j in range(int(N[i]/2)):

回答by mrk

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.

它是由 1.12.0 和更新的 numpy 版本中不受支持的浮点索引引起的,即使代码应该被认为是有效的。

An inttype is expected, not a np.float64

需要一个int类型,而不是一个np.float64

Solution: Try to install numpy 1.11.0

解决方法:尝试安装 numpy 1.11.0

sudo pip install -U numpy==1.11.0.