index the last several members of a ndarray

Dear all, if I have In [395]: a Out[395]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) In [396]: a[...,-1] Out[396]: array([4, 9]) In [397]: a[...,-4:-1] Out[397]: array([[1, 2, 3], [6, 7, 8]]) In [398]: a[...,-4:0] Out[398]: array([], shape=(2, 0), dtype=int64) how can I pick up something like: array([[1, 2, 3, 4], [6, 7, 8, 9]]) I want to index the final 4 rows. I cannot figure out how to do this? Thanks for any help, Chao -- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************

Thanks Jean I just want the last several numbers by indexing from the end. In [400]: b=np.arange(20).reshape(2,10) In [401]: b Out[401]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]) I want something like b[...,...(index from the end by using negative number)] to get: array([[ 6, 7, 8, 9], [16, 17, 18, 19]]) but it's strange that if you use b[...,-1], you get: In [402]: b[...,-1] Out[402]: array([ 9, 19]) if use b[...,-4:-1], you get: Out[403]: array([[ 6, 7, 8], [16, 17, 18]]) but you cannot use b[...,-4:-1] to get array([[ 6, 7, 8, 9], [16, 17, 18, 19]]) because In [403]: b[...,-4:-1] Out[403]: array([[ 6, 7, 8], [16, 17, 18]]) I don't know I am more clear this time.... Chao 2011/10/18 Jean-Luc Menut <jeanluc.menut@free.fr>
how can I pick up something like:
array([[1, 2, 3, 4], [6, 7, 8, 9]])
I'm not sure to understand, should not a[:,1:] be sufficient ? Did I miss something in your message ?
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************

you are right Eric, In [405]: b[...,-4:] Out[405]: array([[ 6, 7, 8, 9], [16, 17, 18, 19]]) cheers, Chao 2011/10/18 Chao YUE <chaoyuejoy@gmail.com>
Thanks Jean I just want the last several numbers by indexing from the end.
In [400]: b=np.arange(20).reshape(2,10)
In [401]: b Out[401]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
I want something like b[...,...(index from the end by using negative number)] to get: array([[ 6, 7, 8, 9], [16, 17, 18, 19]])
but it's strange that if you use b[...,-1], you get: In [402]: b[...,-1] Out[402]: array([ 9, 19])
if use b[...,-4:-1], you get: Out[403]: array([[ 6, 7, 8], [16, 17, 18]])
but you cannot use b[...,-4:-1] to get array([[ 6, 7, 8, 9], [16, 17, 18, 19]]) because In [403]: b[...,-4:-1] Out[403]: array([[ 6, 7, 8], [16, 17, 18]])
I don't know I am more clear this time....
Chao
2011/10/18 Jean-Luc Menut <jeanluc.menut@free.fr>
how can I pick up something like:
array([[1, 2, 3, 4], [6, 7, 8, 9]])
I'm not sure to understand, should not a[:,1:] be sufficient ? Did I miss something in your message ?
--
*********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************

On 18 October 2011 13:56, Chao YUE <chaoyuejoy@gmail.com> wrote:
but it's strange that if you use b[...,-1], you get: In [402]: b[...,-1] Out[402]: array([ 9, 19])
if use b[...,-4:-1], you get: Out[403]: array([[ 6, 7, 8], [16, 17, 18]])
That's because you're mixing two different indexing constructs. In the first case, you're using direct indexing, so you get the values in b at the index you specify. In the second example you're using slicing syntax, where you get the values in b at the range of indices starting with -4 and ending *one before* -1 i.e. the values at b[..., -2]. Here's a simpler example: In [1]: a = range(5) In [2]: a Out[2]: [0, 1, 2, 3, 4] In [3]: a[0] Out[3]: 0 In [4]: a[2] Out[4]: 2 In [5]: a[0:2] Out[5]: [0, 1] In [6]: a[-3] Out[6]: 2 In [7]: a[-1] Out[7]: 4 In [8]: a[-3:-1] Out[8]: [2, 3] Cheers, Scott

thanks Scott. very good explanation. cheers, Chao 2011/10/18 Scott Sinclair <scott.sinclair.za@gmail.com>
On 18 October 2011 13:56, Chao YUE <chaoyuejoy@gmail.com> wrote:
but it's strange that if you use b[...,-1], you get: In [402]: b[...,-1] Out[402]: array([ 9, 19])
if use b[...,-4:-1], you get: Out[403]: array([[ 6, 7, 8], [16, 17, 18]])
That's because you're mixing two different indexing constructs. In the first case, you're using direct indexing, so you get the values in b at the index you specify.
In the second example you're using slicing syntax, where you get the values in b at the range of indices starting with -4 and ending *one before* -1 i.e. the values at b[..., -2].
Here's a simpler example:
In [1]: a = range(5)
In [2]: a Out[2]: [0, 1, 2, 3, 4]
In [3]: a[0] Out[3]: 0
In [4]: a[2] Out[4]: 2
In [5]: a[0:2] Out[5]: [0, 1]
In [6]: a[-3] Out[6]: 2
In [7]: a[-1] Out[7]: 4
In [8]: a[-3:-1] Out[8]: [2, 3]
Cheers, Scott _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************

Le mardi 18 octobre 2011 13:44:00, Chao YUE a écrit :
Dear all,
if I have
In [395]: a Out[395]: array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]])
In [396]: a[...,-1] Out[396]: array([4, 9])
In [397]: a[...,-4:-1] Out[397]: array([[1, 2, 3], [6, 7, 8]])
In [398]: a[...,-4:0] Out[398]: array([], shape=(2, 0), dtype=int64)
how can I pick up something like: array([[1, 2, 3, 4], [6, 7, 8, 9]])
I want to index the final 4 rows. I cannot figure out how to do this?
Don't you want to do : In [1]: a[:,-4:] Out[1]: array([[1, 2, 3, 4], [6, 7, 8, 9]]) Éric.
Thanks for any help,
Chao
Un clavier azerty en vaut deux ---------------------------------------------------------- Éric Depagne eric@depagne.org
participants (4)
-
Chao YUE
-
Jean-Luc Menut
-
Scott Sinclair
-
Éric Depagne