1

i'm still fairly new to numpy in python so please go easy on me. I tried the docs but can't really understand it.

I have an array with multiple lists inside. ArrayABC=[[A1 B1 C1] [A2 B2 C2] [A3 B3 C3] ...] so on..

First question: To flatten the list, i do the following:

chain = itertools.chain(*ArrayABC.tolist())
Array_ABC=list(chain)

Is this the proper way to do it ?

Second Question: So for ArrayABC, i want to find the maximum, minimum and average of A1, A2, A3... I can't seem to find a way to do it without using list and appending A1,A2,A3... B1,B2,B3 and so on... This slows down everything :( What is the proper way to do this ?

Thanks!

chad
  • 627
  • 1
  • 8
  • 24
  • are the missing commas a typo? – Drewdin Nov 26 '14 at 04:27
  • When I print the array, it doesn't have any comma... it looks like this: [[ 12.97082329 12.97601938 12.9747138 ..., -99. -99. -99. ] [ 12.9813199 12.98001671 12.97871304 ..., -99. -99. -99. ] [ 12.97898579 12.98288298 12.98287821 ..., -99. -99. -99. ] ..., [ 12.97687674 12.97687292 12.9731698 ..., -99. -99. -99. ] [ 12.97086382 12.97206116 12.97205782 ..., -99. -99. -99. ] ] – chad Nov 26 '14 at 04:28
  • The guts of the array should be separated by commas ArrayABC = [['A1', 'B1', 'C1'], ['A2', 'B2', 'B2'], etc...] – Drewdin Nov 26 '14 at 04:31
  • What about using numpy for this, e.g. to get minimum: `np.min(ArrayABC, axis=0)` – Marcin Nov 26 '14 at 04:32
  • @Drewdin I'm not sure, I just copied what I had from my output window. – chad Nov 26 '14 at 04:42
  • 2
    Printing numpy arrays is different from printing a Python list of lists. The missing commas are apparently normal. – Alex Nov 26 '14 at 04:50
  • Underlying your questions is a confusion between `numpy` arrays, and Python lists. `chain` is a nice tool for flattening lists of lists, but multidimensional arrays have their own 'reshaping' tools. – hpaulj Nov 26 '14 at 05:35

2 Answers2

2

I think you are trying to flatten your np array into a single list. You can treat np array as regular python list ( someone please educate, i might not be entire correct )

So you could flatten a list of list like this ::

[item for sublist in x for item in sublist]

You can check out this post on making a flat list out of a flat list :

Making a flat list out of list of lists in Python

Or what Alex suggested :

np.flatten()

you can use np.max(), np.sum() and np.mean() for your question two

for instance

import numpy as np 
x = np.array([[1,2,3,4],[2,3,4,5]])

to find the max :

max_val = np.max(x, axis=0)
## returns array([2, 2, 3, 4])

min_val = np.min(x, axis=0)
## returns array([1, 2, 3, 4])

mean = np.mean(x, axis=0)
##returns array([ 1.5,  2. ,  3. ,  4. ])
Community
  • 1
  • 1
biobirdman
  • 4,060
  • 1
  • 17
  • 15
  • is there a reason why my lists in array has no commas? I can't seem to do the axis=0, it still returns me the whole list for some reasons.. – chad Nov 26 '14 at 04:44
  • 1
    If there are no commas, it probably mean that you have only 1 element. Can you do a x.shape in your array and see what you get? – biobirdman Nov 26 '14 at 04:47
  • it returns this (28L, 87114L) – chad Nov 26 '14 at 04:47
  • Hi Arbitel, the reason why it appears to not have a comma, I think is because what you have are long integers ( see the L behind), Therefore, when you do a print, instead of printing the super long interger, numpy will only return the first few numbers. To indicate that is not the end, it will not include the comma at the back . It might be misleading to add a comma at the end as this will drive the user thinking that the element is only 10 numbers long. – biobirdman Nov 26 '14 at 04:58
  • See the nearly-simultaneous comment to the question, by Alex above. `>>>a` in the interpreter where a is an np.array will show commas; `print a` will not. See [https://docs.python.org/2/reference/datamodel.html#object.__repr__] regarding this. Evidently the numpy people have taken the liberty of not providing anything that could be a valid Python expression but also not opting for the recommended `<...some useful description...>` format. – Mike O'Connor Mar 14 '15 at 05:24
1

Say we have a 5x5 numpy array of ones:

>>> a = np.arange(25).reshape((5, 5))
>>> a
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]])

To flatten the array:

>>> b = a.flatten()
>>> b
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])

We can directly find the min, max and average of a

>>> np.amin(a)
0

According to the docs, by default, np.amin flattens a.

The same goes for max and average:

>>> np.amax(a)
24
>>> np.mean(a)
12.0
Alex
  • 428
  • 4
  • 14