NumPy Array Slicing
The purpose of this article is to assist you in achieving your learning goals by introducing Numpy array slicing and presenting examples.
Slicing NumPy arrays
Numpy array slicing in Python is moving data from one index to another index.
In place of an index, we provide a slice: [start:end].
It is also possible to define the step as follows: [start:end:step].
In the absence of a start, it is assumed to be zero.
It is assumed the length of the array in that dimension if the limit is not specified.
It’s counted as 1 if we don’t complete step
From the below array, slice items from index 1 to index 6:
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[1:8])
print(type(prime_arr))
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[1:6])
print(type(silent_arr))
Reminder: In the outcome, the beginning index is kept, but the last index is not kept.
From index 3 to the tail of the array, slice the items as follows:
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[3:])
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[3:])
When slicing through a Numpy array, you can extract items from the start up to (but not including) index 6.
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[:6])
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[:6])
Negative Array Slice
In Numpy array slicing, utilizing a negative index indicates starting from the last:
Slice from index -7 to index -4.
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[-7:-4])
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[-7:-4])
Python NumPy STEP
The step value is utilized to decide the interval of the slicing.
From index 1 to index 7, the following items have an increment of 2:
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[1:7:2])
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[1:7:2])
Provide all items with an increment of two from the whole array:
import numpy as npy
prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19])
print(prime_arr[::2])
import numpy as npy
silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"])
print(silent_arr[::2])
Slice 2-D Arrays
Take items index 2 to index 4 from the first array:
import numpy as npy
mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]])
print(mrx_arr[0, 2:5])
import numpy as npy
mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]])
print(mrx_arr[0, 2:5])
Reminder: Keep in mind that the second item has an index of 1.
Display the 5th item from both arrays:
import numpy as npy
mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]])
print(mrx_arr[0:2, 4])
import numpy as npy
mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]])
print(mrx_arr[0:2, 4])
Take items from index 2 to 4 of two arrays to obtain a two-dimensional array:
import numpy as npy
mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]])
print(mrx_arr[0:2, 2:5])
import numpy as npy
mrx_arr = npy.array([["Major League Soccer","Premier League","La Liga","Serie A","Süper Lig"], ["Bundesliga","Ligue 1","Primeira Liga","Eredivise:","Scottish Premiership"]])
print(mrx_arr[0:2, 2:5])