1

I am using a 2D list, and I'd like to calculate its mean value by row. The following is my code:

import numpy as np

mylist = np.zeros((2,120))    # This gives you a 2 by 120 2D list with 2 rows, and 120 columns
average_list = np.zeros(120)

for col in xrange(120):
    average_list[col] = np.mean(mylist[:][col])

However, the above chunk generates this:

IndexError: index 2 is out of bounds for axis 0 with size 2

As I find during debugging, the problem happens at the col in np.mean(mylist[:][col])

May I know what am I wrong about this?

Thanks.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • Does this answer your question? [How to access a column in a list of lists in python](https://stackoverflow.com/questions/44360162/how-to-access-a-column-in-a-list-of-lists-in-python) – stwykd May 03 '20 at 05:13

4 Answers4

2

One way to fix your code (with minimal changes) would be

for col in xrange(120):
    average_array[col] = np.mean(myarray[:, col])

However, a better way would be to avoid the for-loop and use axis=0:

average_array = myarray.mean(axis=0)   # 1

axis=0 tells mean to take the mean over the first axis, i.e. the rows.


A small example may help you see the difference between myarray[:][col] and myarray[:, col]:

In [7]: myarray = np.arange(6).reshape(2,3)

In [8]: myarray
Out[8]: 
array([[0, 1, 2],
       [3, 4, 5]])

In [9]: myarray[:][0]
Out[9]: array([0, 1, 2])

In [10]: myarray[:, 0]
Out[10]: array([0, 3])

As you can see myarray[:][0] selects the 0th row of a (copy of) myarray. So myarray[:][col] raises an IndexError when col is greater than 1, since there are only 2 rows.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1

mylist has 2 lists in it. So index 2 is out of bounds.

>>> mylist
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  0.,  0.]])
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • When I say `np.mean(mylist[:][col])`, I think I am visiting the `col`th element across all lists in the 2D list. Please correct me if I am wrong. Thanks. :) – ChangeMyName Feb 18 '14 at 13:30
  • 1
    `mylist[:][col]` means make a copy of mylist and get the `col`-th item. but mylist has just 2 items, 2 lists. So index 2 is out of bounds – Bibhas Debnath Feb 18 '14 at 13:32
1

When you do mylist[:] you making a copy of the 2D array and then with mylist[:][col] you are indexing the first dimension. Try this:

for col in xrange(120):
    average_list[col] = np.mean([ x[col] for x in mylist] )

But unutbu's answer is far more efficient.

Community
  • 1
  • 1
perreal
  • 94,503
  • 21
  • 155
  • 181
1

Not directly an answer to your question, but you can specify an axis to calculate the mean on:

np.mean(mylist, axis=0)

axis=0 will give you row-wise mean, whereas axis=1 will give you the column-wise mean.

Matt
  • 17,290
  • 7
  • 57
  • 71