错误:TypeError:只能将length-1数组转换为Python标量

问题描述 投票:-1回答:1

我运行代码时的第一个错误是:

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

我尝试使用numpy 1.11.0安装,但它不起作用。如果y更改为int(),则为新错误:

这是代码:

# Load airfoil from file
def airfoil_from_file(fname, n=256):
    dat = np.loadtxt(fname, skiprows=1)
    eps=1e-8

    num1 = round(dat[0,0])
    num2 = round(dat[0,1])

    if num1>1: 
        xy = zeros((num1+num2-1,2))
        xy[:num1,:] = dat[1:1+num1,:][::-1,:]
        xy[num1:,:] = dat[2+num1:,:]
    else:
        xy=dat.copy()

第一个错误:

Traceback (most recent call last):
  File "compute.py", line 18, in <module>
    rx, ry = airfoil_from_file('clarky.dat', n=128)
  File "/home/david/Descargas/L04PanelMethod/geometry.py", line 29, in airfoil_from_file
    xy = zeros((num1+num2-1,2))
TypeError: 'numpy.float64' object cannot be interpreted as an integer

如果我改为int():

def airfoil_from_file(fname, n=256):
    dat = int(np.loadtxt(fname, skiprows=1))
    eps=1e-8

新错误:

Traceback (most recent call last):
  File "compute.py", line 18, in <module>
    rx, ry = airfoil_from_file('clarky.dat', n=128)
  File "/home/david/Descargas/L04PanelMethod/geometry.py", line 22, in airfoil_from_file
    dat = int(np.loadtxt(fname, skiprows=1))
TypeError: only length-1 arrays can be converted to Python scalars

请帮忙

python python-3.x numpy
1个回答
0
投票

在原始代码中将xy = zeros((num1+num2-1,2))更改为xy = zeros((int(num1+num2)-1,2))

仅舍入舍入,但不转换为int。

num1 = round(dat[0,0])

num2 = round(dat[0,1])

在您的情况下,舍入到最近的浮点数,例如回合(12.4)= 12.0。

© www.soinside.com 2019 - 2024. All rights reserved.