1 Sep 01:17 2011

### reclassify values in an array

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
```
1 Sep 01:23 2011

### Re: Quote of the Day version 1.0

```On 31/08/11 20:14, Cranky Frankie wrote:

> This code works. Now I just have to figure out:
> - how to associate .py files in Ubuntu to IDLE

You probably don't want to do that. IDLE is fine for developing code but
you don't want to run  it in IDLE for general use you want to use the
interpreter. And the shebang (#!) line at the start will do that for you.

--

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 01:32 2011

### Re: meaning of % in: if n % x == 0:

```On 31/08/11 20:35, Lisi wrote:
> ??  If either n or x or both were 0, and % were the same thing as *, the
> statement would be true, but from the context I don't think that % does mean
> the same as *, because * appears very soon after in the same fragment of
> code.

It's the remainder operator:

IF N MOD X = 0

in BASIC

It's explained in the Simple Sequences topic of my tutorial and
mentioned again in the Raw Materials topic under integers.

The use of % as the operator goes back to C. Many of Python's
operators are just inherited directly from C.

--

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 01:41 2011

### Re: reclassify values in an array

```On 01/09/11 00:17, 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,...
> And then finally sum them together.

And what has been the problem?

> I have attempted a few methods, see code below.

I'm not familiar with NumPy (which I assume is what you are using?)
However the second approach looks more likely to succeed than the first.
Assuming enumerate works with NumPy arrays. Is there any
reason why you cannot use a normal list?

Then it would just be:

result = sum(1 for item in array if item >= 100)

HTH,

--

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 02:09 2011

### Re: meaning of % in: if n % x == 0:

```Hugo Arts wrote:

>  n % y == 0 if n is divisible by y. This is useful in factoring prime
> numbers

If you find a way to factor *prime numbers*, you're doing something wrong.

:)

(By definition, a prime number has no factors apart from itself and one,
which are trivial.)

You mean, factorising numbers into the product of primes.

--

--
Steven
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 04:29 2011

### Re: meaning of % in: if n % x == 0:

```% is not remainder - it is modulo.

Difference shows up when left agument is negative.

--

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 04:29 2011

### Re: meaning of % in: if n % x == 0:

```ah, my mistake. I was in a hurry writing that post, which is a really
bad idea :/

On Thu, Sep 1, 2011 at 2:09 AM, Steven D'Aprano <steve <at> pearwood.info> wrote:
> Hugo Arts wrote:
>
>>  n % y == 0 if n is divisible by y. This is useful in factoring prime
>> numbers
>
> If you find a way to factor *prime numbers*, you're doing something wrong.
>
> :)
>
> (By definition, a prime number has no factors apart from itself and one,
> which are trivial.)
>
> You mean, factorising numbers into the product of primes.
>
>
>
> --
> Steven
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
1 Sep 07:02 2011

### Re: reclassify values in an array

```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

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```
1 Sep 10:12 2011

### Re: reclassify values in an array

On Thu,  1 Sep 2011, 01:17:45 CEST, questions anon <questions.anon <at> gmail.com> wrote:

> 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.

*If* you do not need the array after you summed you can do something like below (untested!):

big_array = N.ma.concatenate(all_FFDI)
rusult = 0

for i in big_array:
if i >= 100:
result += 1

greets
sander

```_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
```
1 Sep 10:33 2011

### Re: reclassify values in an array

```questions anon wrote:

> 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.

>>> import numpy
>>> all_FFDI = numpy.arange(60).reshape((3,4,5))
>>> all_FFDI
array([[[ 0,  1,  2,  3,  4],
[ 5,  6,  7,  8,  9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],

[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],

[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
>>> all_FFDI >= 25
array([[[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False]],

[[False, False, False, False, False],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True]],

[[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True],
[ True,  True,  True,  True,  True]]], dtype=bool)
>>> (all_FFDI >= 25).sum()
35

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

```

Gmane