Slicing in NumPy arrays is similar to how it works with a Python list.
Program 1:
import numpy as np arr = np.array([[1,2,3,4,5], [4,5,6,7,8], [9,8,7,6,5]]) print("The NumPy Array is:") print(arr) arr1 = arr[1:3, :3] # row 1 to 3 (not inclusive) and first 3 columns print("The Sliced NumPy Array is:") print(arr1)
Output:
The NumPy Array is: [[1 2 3 4 5] [4 5 6 7 8] [9 8 7 6 5]] The Sliced NumPy Array is: [[4 5 6] [9 8 7]]
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start: end].
We can also define the step, like this: [start: end: step].
If we don't pass start its considered 0.
If we don't pass end it’s considered length of array in that dimension.
If we don't pass step it’s considered 1.
Program 2: Slice elements from index 1 to index 5 from the following array.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5])
Output:
[2 3 4 5]
Note: The result includes the start index, but excludes the end index.
Program 3: Slice elements from index 4 to the end of the array.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:])
Output:
[5 6 7]
Program 4: Slice elements from the beginning to index 4 (not included).
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4])
Output:
[1 2 3 4]
Negative Slicing: Use the minus operator to refer to an index from the end.
Program 5: Slice from the index 3 from the end to index 1 from the end.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1])
Output:
[5 6]
STEP: Use the step value to determine the step of the slicing.
Program 6: Return every other element from index 1 to index 5.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5:2])
Output:
[2 4]
Program 7: Return every other element from the entire array.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[::2])
Output:
[1 3 5 7]
Slicing 2-D Array’s
Program 8: From the second element, slice elements from index 1 to index 4 (not included).
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[1, 1:4])
Output:
[7 8 9]
Note: Remember that second element has index 1.
Program 9: From both elements, return index 2.
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 2])
Output:
[3 8]
Program 10: From both elements, slice index 1 to index 4 (not included), this will return a 2-D array.
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 1:4])
Output:
[[2 3 4] [7 8 9]]
NumPy Slice as a reference: It is noteworthy that the result of a NumPy slice is a reference and not a copy of the original array.
Program 11:
import numpy as np arr = np.array([[1,2,3,4,5], [4,5,6,7,8], [9,8,7,6,5]]) print("The NumPy Array is:") print(arr) arr1 = arr[1:, 2:] # row 1 onwards and column 2 onwards , arr1 is now pointing to a subset of arr. print("The Sliced NumPy Array is:") print(arr1)
Output:
The NumPy Array is: [[1 2 3 4 5] [4 5 6 7 8] [9 8 7 6 5]] The Sliced NumPy Array is: [[6 7 8] [7 6 5]]
Here, arr1 is actually a reference to the original array arr. Hence, if we change one of the elements in arr1, then:
Program 12:
import numpy as np arr = np.array([[1,2,3,4,5], [4,5,6,7,8], [9,8,7,6,5]]) print("The NumPy Array is:") print(arr) arr1 = arr[1:, 2:] # row 1 onwards and column 2 onwards , arr1 is now pointing to a subset of arr. print("The Sliced NumPy Array is:") print(arr1) arr1[0,2]=88 print("Updates NumPy Array:") print(arr)
Output:
The NumPy Array is: [[1 2 3 4 5] [4 5 6 7 8] [9 8 7 6 5]] The Sliced NumPy Array is: [[6 7 8] [7 6 5]] Updates NumPy Array: [[ 1 2 3 4 5] [ 4 5 6 7 88] [ 9 8 7 6 5]]
Another salient point to note is that the result of the slicing is dependent on how we slice it.
Program 13:
import numpy as np arr = np.array([[1,2,3,4,5], [4,5,6,7,8], [9,8,7,6,5]]) print("The NumPy Array is:") print(arr) arr1 = arr[2:, :] # row 2 onwards and all columns print("The Sliced NumPy Array is:") print(arr1) print("The shape of Sliced NumPy Array is:") print(arr1.shape)
Output:
The NumPy Array is: [[1 2 3 4 5] [4 5 6 7 8] [9 8 7 6 5]] The Sliced NumPy Array is: [[9 8 7 6 5]] The shape of Sliced NumPy Array is: (1, 5)
Program 14:
import numpy as np arr = np.array([[1,2,3,4,5], [4,5,6,7,8], [9,8,7,6,5]]) print("The NumPy Array is:") print(arr) arr1 = arr[2, :] # row 2 onwards and all columns print("The Sliced NumPy Array is:") print(arr1) print("The shape of Sliced NumPy Array is:") print(arr1.shape)
Output:
The NumPy Array is: [[1 2 3 4 5] [4 5 6 7 8] [9 8 7 6 5]] The Sliced NumPy Array is: [9 8 7 6 5] The shape of Sliced NumPy Array is: (5,)
Thank u sir for making such a topics topics so simple