Reputation: 1260
This is my function and the variable tracks is a list and each element of the list is a n x 3
array:
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])
I'm trying to calculate the minimum distances between the arrays of the list which represent 3d lines in space but I am getting the error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
The error is probably related to the call to min()
but I can't solve it. Following is the error traceback:
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()
Upvotes: 0
Views: 1119
Reputation: 23753
The error occurs because you cannot determine whether a complete array is True
or False
. What would be the boolean state of an array where all elements are True
but one?
min
takes an iterable for an argument and compares each element to the other, each comparison results in a boolean value. Iterating over a 1-d numpy
array produces individual elements - min
works for a 1-d numpy array.
>>> 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
>>>
Iterating over a 2-d numpy
array produces rows.
>>> 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
won't won't work for 2-d arrays because it is comparing arrays and - The truth value of an array with more than one element is ambiguous
.
>>> 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()
>>>
If you need to find the element with the minimum value, use numpy.amin
or the ndarray.min
method.
>>>
>>> np.amin(b)
-4
>>> b.min()
-4
>>>
Upvotes: 2