Slice nested arrays, How to

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Slice nested arrays, How to

Ibrahim EL MEREHBI
Hello,

I created the following array by converting it from a nested list:

    a = np.array([np.array([ 17.56578416,  16.82712825,  16.57992292,
15.83534836]),
       np.array([ 17.9002445 ,  17.35024876,  16.69733472,  15.78809856]),
       np.array([ 17.90086839,  17.64315136,  17.40653009,  17.26346787,
        16.99901931,  16.87787178,  16.68278558,  16.56006419,
16.43672445]),
       np.array([ 17.91147242,  17.2770623 ,  17.0320501 ,
16.73729491,  16.4910479 ])], dtype=object)

I wish to slice the first element of each sub-array so I can perform
basic statistics (mean, sd, etc...0).

How can I do that for large data without resorting to loops? Here's the
result I want with a loop:

    s = np.zeros(4)
    for i in np.arange(4):
        s[i] = a[i][0]

    array([ 17.56578416,  17.9002445 ,  17.90086839,  17.91147242])

Thank you
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Slice nested arrays, How to

Sebastian Berg
On Mon, 2017-07-24 at 16:37 +0200, Bob wrote:

> Hello,
>
> I created the following array by converting it from a nested list:
>
>     a = np.array([np.array([
> 17.56578416,  16.82712825,  16.57992292, 
> 15.83534836]),
>        np.array([ 17.9002445
> ,  17.35024876,  16.69733472,  15.78809856]),
>        np.array([
> 17.90086839,  17.64315136,  17.40653009,  17.26346787,
>         16.99901931,  16.87787178,  16.68278558,  16.56006419, 
> 16.43672445]),
>        np.array([ 17.91147242,  17.2770623 ,  17.0320501 , 
> 16.73729491,  16.4910479 ])], dtype=object)
>
> I wish to slice the first element of each sub-array so I can perform
> basic statistics (mean, sd, etc...0).
>
> How can I do that for large data without resorting to loops? Here's
> the
> result I want with a loop:
>

Arrays of arrays are not very nice in these regards, you could use
np.frompyfunc/np.vectorize together with `operator.getitem` to avoid
the loop. It probably will not be much faster though.

- Sebastian


>     s = np.zeros(4)
>     for i in np.arange(4):
>         s[i] = a[i][0]
>
>     array([ 17.56578416,  17.9002445 ,  17.90086839,  17.91147242])
>
> Thank you
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Slice nested arrays, How to

William Ray Wing
In reply to this post by Ibrahim EL MEREHBI

> On Jul 24, 2017, at 10:37 AM, Bob <[hidden email]> wrote:
>
> Hello,
>
> I created the following array by converting it from a nested list:
>
>    a = np.array([np.array([ 17.56578416,  16.82712825,  16.57992292,
> 15.83534836]),
>       np.array([ 17.9002445 ,  17.35024876,  16.69733472,  15.78809856]),
>       np.array([ 17.90086839,  17.64315136,  17.40653009,  17.26346787,
>        16.99901931,  16.87787178,  16.68278558,  16.56006419,
> 16.43672445]),
>       np.array([ 17.91147242,  17.2770623 ,  17.0320501 ,
> 16.73729491,  16.4910479 ])], dtype=object)
>
> I wish to slice the first element of each sub-array so I can perform
> basic statistics (mean, sd, etc...0).
>

Have you considered using Pandas?  Assuming I understand what you are trying to do, that nested list could read directly into a Pandas 2D data frame. Extracting the first element of each column (or row) is then fast and efficient.

Bill

> How can I do that for large data without resorting to loops? Here's the
> result I want with a loop:
>
>    s = np.zeros(4)
>    for i in np.arange(4):
>        s[i] = a[i][0]
>
>    array([ 17.56578416,  17.9002445 ,  17.90086839,  17.91147242])
>
> Thank you
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Loading...