[Tutor] reclassify values in an array

Andre' Walker-Loud walksloud at gmail.com
Sat Sep 3 00:55:07 CEST 2011


> thank you for all of the resonses, I have attempted all of the suggestions. It is a numpy array so I can try another list if you would prefer but I thought I would show the error anyway.
> the error I am receiving is 
> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

this is telling you that "value" is not a scalar element, but it has multiple dimensions.

do the following:

>>> big_array=N.ma.concatenate(all_FFDI)
>>> print big_array.shape

>>> print big_array[0].shape


the first print will report the dimensionality of your big_array.  The second print statement will tell you the dimensionality of the 0th element of big_array.  This is the first value of "value" in your for loop.

Only if the shape is given as "( )", a single (scalar) element, can you compare it to an integer or float.  example

>>> a = N.zeros([3]) #this builds a one-dimensional array with 3 elements and puts zeros for each value
>>> a
array([ 0.,  0.,  0.])
>>> a.shape
(3,)
>>> a[0].shape
( )
>>> a[0]
0.

imagine you have a 2-dimensional array

>>> b = N.zeros([3,3]) # a 3 by 3 array of zeros
>>> b.shape
(3, 3)
>>> b[0]
array([ 0.,  0.,  0.])
>>> for i,value in enumerate(b):
...		print value
[ 0.,  0.,  0.]
[ 0.,  0.,  0.]
[ 0.,  0.,  0.]

you are trying to compare the "value" [ 0.,  0.,  0.], to an integer.  This is why your code fails - your big_array is a multi-dimensional array.  

The above example is what I mean by "you should play around with the python interpreter".  By doing these things (above) you will begin to learn the structure of these objects (defined in this case with numpy).


Regards,

Andre




More information about the Tutor mailing list