[Numpy-discussion] Slice nested arrays, How to

Sebastian Berg sebastian at sipsolutions.net
Mon Jul 24 10:54:27 EDT 2017


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
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170724/94c87516/attachment.sig>


More information about the NumPy-Discussion mailing list