2014-11-04 128 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 
>>>