[Tutor] Extract several arrays from a large 2D array

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Jan 21 05:35:24 EST 2016


On 21 January 2016 at 04:22, Ek Esawi <esawiek at gmail.com> wrote:
> I have a 2D array (2000, 4);

Do you mean a numpy array? I'm going to assume that you do.

> an example is given abelow.  On the 1st column
> I have 15 variables, on the 2nd 4 variables. Ignore column 3 for now. I
> want a code that generate 4 arrays for each variable on the 1st column;
> each array contains all the values from column 4. Then I want to find the
> average of each array.
>
> For example; pick the 1st variable in column 1 which is 1; then pick the 1st
> variable on the 2nd column which is 5. Now I want an array that contains
> all the values on the 4th column that match variable 1 on the 1st and
> variable 5 on the 2nd column. I need to get the average of each array.
>
> A             b             c              d
>
> 1              5              3              4
>
> 1              3              2              7
>
> 2              5              7              5
>
> 3              2              8              5
>
> 2              3              2              3

I'll generate a numpy array with this data:

In [1]: import numpy as np

In [2]: M = np.array([[1,5,3,4],[1,3,2,7],[2,5,7,5],[3,2,8,5],[2,3,2,3]])

In [3]: M
Out[3]:
array([[1, 5, 3, 4],
       [1, 3, 2, 7],
       [2, 5, 7, 5],
       [3, 2, 8, 5],
       [2, 3, 2, 3]])

First we want to extract the rows where 1st and 2nd columns have 1 and
5 respectively:

In [5]: M[(M[:, 0] == 1) & (M[:, 1] == 5), :]
Out[5]: array([[1, 5, 3, 4]])

Then we want to get only the 4th column of that:

In [7]: M[(M[:, 0] == 1) & (M[:, 1] == 5), 3]
Out[7]: array([4])

And now we want the mean of that:

In [8]: M[(M[:, 0] == 1) & (M[:, 1] == 5), 3].mean()
Out[8]: 4.0

--
Oscar


More information about the Tutor mailing list