简单Python计算中的ValueError

简单Python计算中的ValueError,python,numpy,Python,Numpy,这是我的函数,变量轨迹是一个列表,列表中的每个元素都是一个nx3数组: temp = np.array(np.zeros((n, n))) for j in range(n-1): for w in range(j + 1, n): mindistance = np.zeros(len(tracks[j])) for i in range(len(tracks[j])): mindistance[i] = np.linal

这是我的函数,变量轨迹是一个列表,列表中的每个元素都是一个
nx3
数组:

temp = np.array(np.zeros((n, n)))
for j in range(n-1):
    for w in range(j + 1, n):  
        mindistance = np.zeros(len(tracks[j]))
        for i in range(len(tracks[j])):   
            mindistance[i] = np.linalg.norm(min(np.fabs(np.array(tracks[w]) - tracks[j][i])))
        temp[j][w]=np.sum(mindistance)/len(tracks[j])
我试图计算代表空间中3d线的列表阵列之间的最小距离,但我得到了错误:

ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

该错误可能与调用
min()
有关,但我无法解决它。以下是错误回溯:

Traceback (most recent call last):

  File "<ipython-input-14-7fb640816626>", line 1, in <module>
    runfile('/Users/G_Laza/Desktop/functions/Main.py', wdir='/Users/G_Laza/Desktop/functions')

  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)

  File "/Users/G_Laza/Desktop/functions/Main.py", line 42, in <module>
    tempA = distance_calc.dist_calc(len(subset_A), subset_A)  # distance matrix calculation

  File "distance_calc.py", line 23, in dist_calc
    mindistance[i] = np.linalg.norm(min(np.fabs(np.array(tracks[w]) - tracks[j][i])))

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/Users/G_-Laza/Desktop/functions/Main.py',wdir='/Users/G_-Laza/Desktop/functions')
runfile中的文件“/Applications/Spyder.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py”,第580行
execfile(文件名、命名空间)
文件“/Users/G_Laza/Desktop/functions/Main.py”,第42行,在
tempA=距离计算。距离计算(len(子集A),子集A)#距离矩阵计算
文件“distance_calc.py”,第23行,dist_calc
mindistance[i]=np.linalg.norm(min(np.fabs(np.array(tracks[w])-tracks[j][i]))
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()

发生错误是因为您无法确定完整数组是
True
还是
False
。如果数组的所有元素都是
True
但只有一个,那么该数组的布尔状态是什么

min
将iterable作为参数,并将每个元素与其他元素进行比较,每次比较产生一个布尔值。迭代一维
numpy
数组会生成单个元素-
min
适用于一维numpy数组

>>> a
array([-4, -3, -2, -1,  0,  1,  2,  3,  4])
>>> for thing in a:
     print thing,


-4 -3 -2 -1 0 1 2 3 4
>>> min(a)
-4
>>>
在二维
numpy
数组上迭代会生成行

>>> b
array([[-4, -3, -2],
       [-1,  0,  1],
       [ 2,  3,  4]])
>>> for thing in b:
     print thing

[-4 -3 -2]
[-1  0  1]
[2 3 4]
>>> 
min
不适用于二维数组,因为它正在比较数组和-
具有多个元素的数组的真值不明确

>>> c
array([0, 1, 2, 3])
>>> c < 2
array([ True,  True, False, False], dtype=bool)
>>> bool(c < 2)

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    bool(c < 2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 
>>> bool(np.array((True, True)))

Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    bool(np.array((True, True)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 
>>> bool(np.array((True, False)))

Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    bool(np.array((True, False)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> 

请发布完整的回溯。在抛出错误之前,它会进入嵌套循环多远?当np.fabs(np.array(tracks[w])-tracks[j][i])抛出错误时,它的值是多少?该值是一个数组,在第一次计算时停止。为了检查错误是否在
min
中,您是否尝试使用
np.min()
np.amin()
?一个int、float、bools数组?你明白错误的意思吗?如果np.ones(4)==1:pass,在shell中尝试
。您是否尝试过只使用该行的一部分添加语句以查看是哪部分计算导致了问题,然后查看这些操作数的值以了解发生问题的原因?
>>> 
>>> np.amin(b)
-4
>>> b.min()
-4
>>>