[Tutor] reclassify values in an array

Andre' Walker-Loud walksloud at gmail.com
Thu Sep 1 07:02:32 CEST 2011


Hi anonymous questioner,

Like Alan, I suspect you are using numpy as

import numpy as N

there is probably a numpy email list where this would be more appropriate (personally I don't object, but I don't want to speak for all the subscribers).

the 2nd attempt is closer to the right answer.  To help yourself answer the question, try

> for i, value in enumerate(big_array):
	print i,value

and see what you get.  Are you allowed to compare value with 100?

Then, when performing the sum, you are asking to sum over axis=0.  I assume you are trying to sum all the individual elements, rather than sum the rows.  asking to sum over axis=0 is telling numpy to treat each row as an object, and sum all those objects, preserving all other dimensions of your array.  In your case, you have a 2 dimensional array, so summing over axis=0 is taking all the rows of your array (matrix) and summing them to produce a new row.  Specifically, it will take the first entry of each row, and add them to make the first  entry of the summed row, then likewise for each additional entry.

In math language, you are doing

r_j = sum_i big_array_{i,j}

if you do

big_array.sum()

then it will sum all of the individual elements

sum = sum_i sum_j big_array_{i,j}


play around more with the interactive interpreter.  If you try these things, and they fail, reproduce your code from the top to bottom, adding only one line at a time, and see what happens (at least for these simple short code snippets).  That should help you improve your understanding faster - which I assume is one of your goals :)


Andre






On Aug 31, 2011, at 4:17 PM, questions anon wrote:

> Dear All,
> I have been going round in circles trying to solve something that sounds simple. I have a huge array and I would like to reclassify the values. Firstly just make them zeros and ones, for example if the values in the array are less than 100 make them 0 and if greater than 100 make them 1. And then finally sum them together. 
> I have attempted a few methods, see code below. Any feedback will be greatly appreciated.
> 
> Attempt 1:
> big_array=N.ma.concatenate(all_FFDI)
> for i in big_array:
>     if i<100:
>         i=0
>     elif i>=100:
>         i=1
>     print big_array
> sum=big_array.sum(axis=0)
> print "the sum is", sum
> 
> 
> Attempt 2:
> big_array=N.ma.concatenate(all_FFDI)
> for i, value in enumerate(big_array):
>     if value==100:
>         big_array[i]=0
>     elif value>=100:
>         big_array[i]=1
>     print big_array
> sum=big_array.sum(axis=0)
> print "the sum is", sum
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list