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:

Numpy Array Slicing Example: 1 

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))

Numpy array slicing examples

Numpy Array Slicing Example: 2 

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:

Numpy Array Slicing Example: 3 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) print(prime_arr[3:])

Numpy Array Slicing Example: 4 

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.

Numpy Array Slicing Example: 5 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) print(prime_arr[:6])

Numpy Array Slicing Example: 6 

import numpy as npy silent_arr = npy.array(["align", "bridge", "design", "edge", "anchor", "muscle", "echo", "doubt"]) print(silent_arr[:6])

Numpy array slicing output


Negative Array Slice

In Numpy array slicing, utilizing a negative index indicates starting from the last:

Slice from index -7 to index -4.

NumPy Array Negative Slicing Example: 1 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) print(prime_arr[-7:-4])

NumPy Array Negative Slicing Example: 2 

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:

Python NumPy STEP Example: 1 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) print(prime_arr[1:7:2])

Python NumPy STEP Example: 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:

Python NumPy STEP Example: 3 

import numpy as npy prime_arr = npy.array([2, 3, 5, 7, 11, 13, 17, 19]) print(prime_arr[::2])

Python NumPy STEP Example: 4 

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:

Example: 1 

import numpy as npy mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]]) print(mrx_arr[0, 2:5])

 

Example: 2 

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:

Example: 3 

import numpy as npy mrx_arr = npy.array([[2,4,6,8,10], [1,3,5,7,9]]) print(mrx_arr[0:2, 4])

Example: 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:

Example: 5 

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])

Example: 6 

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])
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *