Numpy - Slicing
Slicing in Numpy Arrays.
As we all know that the slicing is about to print subpart of something.
Like when our parents bring cake we get the slice of that not the whole cake, it's something similar to this.
Now slicing in numpy array is like printing or getting the value from first index till last index.
I am going to discuss slicing in advance.
| Numpy array with two dimensions. |
Basic information regarding slicing.
- Index is always start from 0. Negative indexing is also possible.
- We always try to figure our solution in [ row, column] format.
- If it's a single element the column number will be the exact one (see question 1 and 2).
- If it's more than one element then the previous rule will gonna apply like column - 1 (see question 3 and 4).
- We can access the particular element by giving rows and column index separated by comma.
- For accessing the group of elements you should pass the rows and columns by using the : symbol.
Answer - As we all know that the 10 is at 2nd row and 1st column. So, a[2 , 1]
Question - 2 print 3.
Answer -As we all know that the 3 is at 0th row and 2nd column. So, a[1 , 2]
Question -3 print [2, 3, 4]
Answer - Firstly, think in which rows these all value lies, so these all value lies in 0th row Now, we are clear that in row section 0 will be there what about column, let's found out on which column all these three elements or value lies. they lies in column 1, 2 and 3. You can't write 1,2 and 3 for this we have a method which is :, The solution will be a[0, 1:]
Question - 4 print [7 , 8 , 11, 12] 11 and 12 should be in the next line.
Answer - Same approach find out in which rows all the elements are there, clearly 7, 8, 11, 12 are in the row index 1 and 2, so you can write it as 1:3 ( I have written 3 means all the elements from rows 1 till 3 but not 3 last index - 1). Now, find out in which column all the elements are present, they are from 2 and 3.
Solution is, a[1:3 , 2:4]
Answer - Think about row index in which 6 is belonging. It is at -2 now, column -3. So the solution is a[-2, -3]

Comments
Post a Comment