Slicing in Numpy Arrays
What is Slicing in Numpy Array?
General syntax for accessing element in an Numpy array
array_name[start_index:end_index]
Create an Numpy array for slicing
import numpy as np
array_1 = np.array([1,2,3,4,5])
array_2 = np.array([[0,9,8,7,6], [5,4,3,2,1]])
print("1D array : ", array_1)
print("2D array : ", array_2)
The above code creates an 1D array and 2D array in array_1 and array_2 respectively.
Output
1D array : [1 2 3 4 5]
2D array : [[0 9 8 7 6]
[5 4 3 2 1]]
Types of Slicing in Numpy array
Positive Slicing
In this slicing, the positive numbers from 0 to n are used. Start index of an array is 0 and End index of an array is n-1.
Example
print("Positive Slicing : ", array_1[1])
Output
Positive Slicing : 2
Negative Slicing
In this slicing, the negative numbers are used. -1 represents the end index of an last element in an array.
Example
print("Negative Slicing : ", array_1[-1])
Output
Negative Slicing : 5
Slicing in 1D array
print("First 3 elements in 1D array : ", array_1[0:3])
print("Last 3 elements in 1D array : ", array_1[2:5])
Using the start index and end index, the first three and last three elements are sliced from an array.
Output
First 3 elements in 1D array : [1 2 3]
Last 3 elements in 1D array : [3 4 5]
Note : The output shows that it includes the start value and excludes the end value in python.
Slicing in 1D array by without start index or end index
print("First 2 elements in 1D array : ", array_1[:2])
print("Last 2 elements in 1D array: ", array_1[3:])
Output
First 2 elements in 1D array : [1 2]
Last 2 elements in 1D array: [4 5]
Here, the start index and end index are not mentioned for slicing operation. But in python, if start index is not mentioned, then it will automatically takes the all values before the end index or if end index is not mentioned, then it will automatically takes the all values after the start index.
Slicing in 1D array by without start index and end index
print("All elements in 1D array : ", array_1[:])
Output
All elements in 1D array : [1 2 3 4 5]
If both the start index and end index are not mentioned, then it will takes the whole array to display on the output.
Step Index in Slicing
syntax : array_name[start_index:end_index:step_index]
Step index is similar to the increment of an count. The default count of step index is 0.
Example
print("Step index in 1D array : ", array_1[::2])
Output
Step index in 1D array : [1 3 5]
In the above code, we can set the step index as 2, so it will displays the elements in the index values of 0, 2, 4 as an output.
Slicing in 2D array
Slicing of 2D array is also same as the slicing in 1D array.
Example
print("First 3 elements of row_1 in 2D array : ", array_2[0, 0:3])
print("Last 3 elements of row_1 in 2D array : ", array_2[0, 2:6])
print("First 3 elements of row_2 in 2D array : ", array_2[1, 0:3])
print("Last 3 elements of row_2 in 2D array : ", array_2[1, 2:6])
print("First 2 elements of row_1 in 2D array : ", array_2[0, :2])
print("Last 2 elements of row_1 in 2D array : ", array_2[0, 3:])
print("First 2 elements of row_2 in 2D array : ", array_2[1, :2])
print("Last 2 elements of row_2 in 2D array : ", array_2[1, 3:])
print("First 2 elements of row_1 & row_2 in 2D array : ", array_2[0:2, 0:2])
print("Last 2 elements of row_1 & row_2 in 2D array : ", array_2[0:2, 3:5])
print("First 3 elements of row_1 & row_2 in 2D array : ", array_2[:2, :3])
print("Last 3 elements of row_1 & row_2 in 2D array : ", array_2[:2, 2:])
print("All elements in 2D array : ", array_2[:, :])
print("Step index in 2D array : ", array_2[0, ::2])
Here, we can mention the rows and columns as seen before on the accessing of 2D array. Refer here!
Output
First 3 elements of row_1 in 2D array : [0 9 8]
Last 3 elements of row_1 in 2D array : [8 7 6]
First 3 elements of row_2 in 2D array : [5 4 3]
Last 3 elements of row_2 in 2D array : [3 2 1]
First 2 elements of row_1 in 2D array : [0 9]
Last 2 elements of row_1 in 2D array : [7 6]
First 2 elements of row_2 in 2D array : [5 4]
Last 2 elements of row_2 in 2D array : [2 1]
First 2 elements of row_1 & row_2 in 2D array : [[0 9]
[5 4]]
Last 2 elements of row_1 & row_2 in 2D array : [[7 6]
[2 1]]
First 3 elements of row_1 & row_2 in 2D array : [[0 9 8]
[5 4 3]]
Last 3 elements of row_1 & row_2 in 2D array : [[8 7 6]
[3 2 1]]
All elements in 2D array : [[0 9 8 7 6]
[5 4 3 2 1]]
Step index in 2D array : [0 8 6]
Challenge - Try to slice the elements from 1D & 2D arrays using the negative slicing.
Follow on LinkedIn - Trillionaire TK
Follow on Instagram - Trillionaire TK
Visit our website - Information Technology
Learn, Code, Practice!!!