How to perform array slicing in numpy?
Slicing means get a specific part of an array. Here we will see how to slice 1D, 2D and 3D array. To slice the
array we will use the index number
1D array slicing
Example:
Input
import numpy as np
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
1
Example 2:
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[-2]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
16
Example:3
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0:7]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 2 3 4 5 6 7]
Example:4
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[0:15:2]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 3 5 7 9 12 14 16]
Example:5
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[:-5]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[1 2 3 4 5 6 7 8 9 10 11 12]
Example:6
Input
x=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17])
print(x)
y=x[7:]
print(y)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17]
[ 8 9 10 11 12 13 14 15 16 17]
2D array slicing
If we see a 2D array then we will see that, in a 2D array contains some 1D array. So using slicing we can get
the 1D array present in the 2D array and also get specific elements present in the 1D array of the 2D
array.
In the examples, first third bracket is to get the 1D array present in the 2D array and the second third
bracket second third bracket is to get the 1D array element.
Example:
Input
import numpy as np
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[0][1]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
2
Example:2
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[2,0]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
5
Example:3
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[2]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[5 6 5 9]
Example:4
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[3:,:-1]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[[ 7 8 9]
[10 11 12]]
Example:5
Input
x=np.array([[1,2,3,4],[3,4,4,7],[5,6,5,9],[7,8,9,9],[10,11,12,15]])
print(x)
y=x[0:3,0:2]
print(y)
Output
[[ 1 2 3 4]
[ 3 4 4 7]
[ 5 6 5 9]
[ 7 8 9 9]
[10 11 12 15]]
[[1 2]
[3 4]
[5 6]]
3D array slicing
Input:
import numpy as np
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
sliced_1 = arr_3d[0] print(sliced_1)
sliced_2 = arr_3d[1:]
print(sliced_2)
sliced_3 = arr_3d[:, 0]
print(sliced_3)
sliced_4 = arr_3d[:, :, 0]
print(sliced_4)
sliced_5 = arr_3d[:, :, ::-1]
print(sliced_5)
sliced_6 = arr_3d[0][0]
print(sliced_6)
sliced_7 = arr_3d[0][0][0]
print(sliced_7)
Output:
array([[1, 2],
[3, 4]])
array([[[ 5, 6],
[ 7, 8]],
[[ 9, 10],
[11, 12]]])
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
array([[ 1, 3],
[ 5, 7],
[ 9, 11]])
array([[[ 2, 1],
[ 4, 3]],
[[ 6, 5],
[ 8, 7]],
[[10, 9],
[12, 11]]])
array([1, 2])
1