[Tutor] reclassify values in an array

questions anon questions.anon at gmail.com
Mon Sep 5 06:21:31 CEST 2011


Thanks all, that helped me work out that I needed to try something else.
If anyone else needs to know what method worked:

big_array=N.ma.concatenate(all_FFDI)
for x in N.nditer(big_array, op_flags=['readwrite']):
    if x<100:
        x[...]=x=0
    elif x>=100:
        x[...]=x=1
sum=big_array.sum(axis=0)


On Sat, Sep 3, 2011 at 8:55 AM, Andre' Walker-Loud <walksloud at gmail.com>wrote:

> > 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
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110905/cd0df9e3/attachment.html>


More information about the Tutor mailing list