python - Difference between A[1:3][0:2] and A[1:3,0:2] -


i can't figure out difference between these 2 kinds of indexing. seems should produce same results not. explanation?

a[1:3, 0:2] takes rows 1-3 , columns 0-2 returning 2x2 array.

a[1:3][0:2] first takes rows 1-3 and subarray takes rows 0-2, resulting in 2xn array n original number of columns.

in [1]: import numpy np  in [2]: = np.arange(16).reshape(4,4)  in [3]: out[3]:  array([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11],        [12, 13, 14, 15]])  in [4]: a[1:3,0:2] out[4]:  array([[4, 5],        [8, 9]])  in [5]: a[1:3] out[5]:  array([[ 4,  5,  6,  7],        [ 8,  9, 10, 11]])  in [6]: a[1:3][0:2]  out[6]:  array([[ 4,  5,  6,  7],        [ 8,  9, 10, 11]]) 

the equivalent of a[1:3,0:2] using 2 [] is: a[1:3][:,0:2]:

in [7]: a[1:3][:,0:2] out[7]:  array([[4, 5],        [8, 9]]) 

where : means "all rows". first selecting rows via [1:3] , then, rows select columns 0-2.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -