Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: difference b/w A[:i][:j] and A[:i,:j]

Why is there a difference in the following operations, How are they implemented in the library?

print(prov_img[:19][:20].shape)
Output : (19, 1250)

print(prov_img[:19,:20].shape)
Output : (19, 20)
like image 661
SinOfWrath Avatar asked Feb 05 '23 01:02

SinOfWrath


1 Answers

prov_img is a 2d array here.

This code snippet is providing you the first 19 rows (0th row to 18th row) and 20 columns (0th column to 19th column) of prov_img:

>>> prov_img[:19,:20].shape
(19, 20)

Here, prov_img[:19] gives you the first 19 rows of prov_img and then adding [:20] means you are again taking the first 20 rows from the matrix you got from prov_img[:19]:

>>> prov_img[:19][:20].shape
(19, 1250)

Since there are 19 rows in prov_img[:19], slicing the first 20 rows from it eventually providing you all the 19 rows. There is no slicing in columns and as a result you are getting output as (19, 1250) where 1250 is the number of columns in your prov_img matrix (2d array).

like image 63
Wasi Ahmad Avatar answered Feb 08 '23 16:02

Wasi Ahmad