2014-11-04 132 views
0

这是我的函数和变量轨道是一个列表,并且该列表的每个元素是一个n x 3数组:ValueError异常在简单的Python计算

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]) 

我想要计算的阵列之间的最小距离

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or 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

请发布完整的Traceback。 – wwii 2014-11-04 12:41:20

+0

在引发错误之前它会到达嵌套循环多远? 'np.fabs(np.array(tracks [w]) - tracks [j] [i])的值是什么时候抛出错误? – wwii 2014-11-04 12:50:22

+0

该值是一个数组,它在第一次计算中停止。 – gelazari 2014-11-04 12:51:53

回答

2

错误发生,因为你不能确定一个完整的阵列是TrueFalse。一个数组的布尔状态是什么,其中所有元素都是True,但是一个?

min需要一个可迭代的参数,并将每个元素与另一个进行比较,每个比较结果为一个布尔值。迭代1-d 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 
>>> 

对2-d numpy数组的迭代产生行。 The truth value of an array with more than one element is ambiguous -

>>> 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不会不会为2-d阵列,因为它是比较阵列和工作。

>>> 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() 
>>> 

如果你需要找到元素与最小值,使用numpy.aminndarray.min方法。

>>> 
>>> np.amin(b) 
-4 
>>> b.min() 
-4 
>>>