NumPy Array Indexing & Array Slicing
Array Indexing
Array indexing is same as accessing the array element. We can access the array elements by using index. Index should be starting with 0 means index value of first element is 0.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])From the above code we get the output as 1. If we are running print(arr[1]) then it will return 2 like that the array indexing works.
2 Dimensional array
In 2 D array we need to pass 2 parameter as index.
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])In the above eg. it will be return 2nd element on 1st row: 2.
3 Dimensional array
In 3D array we need to pass 3 parameter as index
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])In the above eg. it will be return 6. Here in the first dimension having the value with [[1, 2, 3], [4, 5, 6]] and within this the index with 1 should be taken so it will return [4,5,6] and within this return index with 2 so it will return 6. By this way the indexing will be work.
Negative Indexing
We can able to put the negative value in the index position when we accessing array value. Negative indexing to access an array from the end.
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])in the above sample it will return Last element from 2nd dim: 10.
Array Slicing
Array slicing means we can access the array values based on the parameter we input. Means we can put the parameter like start index, end index and step([start:end:step]). Using start index we can set the accessing starting position. The default value should be 0 means if we are not pass any value then, its considered as 0. Using end index we can set the accessing ending position. The default value should be array length. Using step we can define which of the element need to be skipped and not skipped. Means if we put the step as 2 then first item should be taken and the next will be skipped and the next will be taken and so on the series continue till the ending index.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])In the above sample the 1 is starting index 5 is ending index and 2 is the step. so it will be start from 2 to 5 because the ending position - 1 will be take. Then the series with [2,3,4,5] will be get. With in this on applying step the step - 1 value will be skipped as well as taken. So finally it will be return [2,4].
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print('Array with stepper only: ',arr[::2])The above code will return Array with stepper only: [1,3,5,7].
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print('Array with starting index only: ',arr[1:])The above code will return Array with starting index only: [2,3,4,5,6,7].
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print('Array with ending index only: ',arr[:3])The above code will return Array with ending index only: [1,2,3].
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print('Array with starting index and step: ',arr[1::3])The above code will return Array with ending index only: [2,5].
Negative Slicing
Use the minus operator to refer to an index from the end like as it same as array accessing with negative index. For example
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])The above code will return [5,6].
Slicing 2 Dimensional array
We can slice the array based on the 1 Dimensional array itself and we can put each dimensional parameter with coma(,). For example,
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])The above code will return [7,8,9].