NumPy - Slicing



Slicing is the way to extract a subset of data from a NumPy array. It can be performed on one or more dimensions of a NumPy array. We can define which part of the array to be sliced by specifying the start and end index values using [start : end] along with the array name.

Slicing can also use Python's built-in function slice object, which is constructed by the same start, stop, and step parameters to define the range. This slice object is passed to the array to extract a part of array.

The syntax for slicing an array is [start:stop:step] where −

  • Start is the index where the slice object begins (index inclusive) if we don't pass start then by default, it is considered as '0'.

  • Stop is the index where the slice ends (exclusive, meaning the element at this index is not included) if we don't pass stop by default it is considered as length of array in that dimension.

  • Step determines the interval between indices (i.e., how many elements to skip) if we don't pass the step by default it is 1 and the step cannot be zero.

Slicing in 1D NumPy Arrays

Slicing in 1D array is used to access specific elements using start:stop:step parameters. It enables efficient sub-setting, skipping elements, or reversing an array.

Example: Using start:stop:step

Let us create a 1D array, where we have a row of books labeled 0 to 9 on a shelf and we need to pick every second book from the 1st book to the 8th book.

In this example we use slicing parameters separated by a colon : (start:stop:step) directly to the ndarray object. Here start is 1 (second book), stop is 8 ends before the 8th book and step is 2, picks every second book between the indexes.

Open Compiler
import numpy as np arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print(arr[1:8:2])

Following is the output of the above code −

[1 3 5 7]

Example: Using Slice Object

The same result that we got in example 1 can be achieved by using the built-in slice function. Following is the code −

Open Compiler
import numpy as np arr = np.arange(10) s = slice(1,8,2) print(arr[s])

Output of the above code is as follows −

[1 3 5 7]

Example: Slicing with Start Parameter

Slice items starting from the index. When we use only the start parameter it will start from that index and as we didn't specify stop by default it will consider the length of the array −

Open Compiler
import numpy as np a = np.arange(10) print(a[2:])

Following is the output of the above code −

[2 3 4 5 6 7 8 9]

Example: Slicing with Stop Parameter

Slice items only using the stop parameter. When we only use the stop parameter and omit the start then it access elements from the beginning of the array up to, but not including the, specified stop index. Following is the code −

Open Compiler
import numpy as np a = np.arange(10) print(a[:7])

Following is the output of the above code −

[0 1 2 3 4 5 6]

Example: Using Step Parameter

Slice items only using the step parameter. The below code extracts every second element from the array, starting from the beginning till the end, as we didn't specify start and stop.

Open Compiler
import numpy as np a = np.arange(10) print(a[::2])

When we run above program, it produces following result −

[0 2 4 6 8]

Slicing in 2D NumPy Arrays

A 2D NumPy array resembles a matrix where it has 2 indices row and a column. To slice a 2D array we use same syntax as the 1D array. The only thing that changes is that we have to define a slice for every dimension of the array.

Example

Let us create a 2D array for employee data where 3 columns contain details of employee ID, age, and salary and we will use slice parameters to get the information of employee 2 and get ages of all employees from index 2 −

Open Compiler
import numpy as np employees = np.array([ [1, 25, 50000], [2, 30, 60000], [3, 28, 55000], [4, 35, 65000], [5, 40, 70000] ]) print("Information of Employee 2:", employees[1]) print("Ages of employees from index 2 onwards:", employees[2:, 1])

Following is the output of the above code −

Information of Employee 2: [ 2  30 60000]
Ages of employees from index 2 onwards: [28 35 40]

Slicing in 3D NumPy Arrays

A 3D array is a collection of 2D arrays, with three indices: depth(or plane), row, and a column. 3D array also has a the same syntax as 1D and 2D but we need to define slices for all three dimensions.

Example

Let us create a 3D array using the arange() function and reshape it with values containing 0 to 23 then reshape this 1D into 3D representing a (2*3*4) matrix and use slice object for slicing to get a subarray which selects the first layer (depth), all rows, and the first 2 columns −

Open Compiler
import numpy as np arr_3d = np.arange(24).reshape(2, 3, 4) print("Original 3D array: \n" , arr_3d) subarray = arr_3d[0, :, :2] print("\nSliced subarray:", subarray)

When we run above program, it produces following result −

Original 3D array: 
 [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

Sliced subarray: [[0 1]
 [4 5]
 [8 9]]

Negative Slicing

Negative indexing allows accessing elements from the end of an array. The index -1 refers to the last element, -2 to the second last and so on. It is basically used when you have to access a specific items from the end. It gets combined with the slicing syntax (start:stop:step) to extract elements in the reverse order.

Example: Accessing Lowest 5 Marks

Let us create an array where we store student marks and need to quickly identify the lowest 5 marks we use negative slicing with the start parameter −

Open Compiler
import numpy as np marks = np.array([93, 87, 98, 89, 67, 65, 54, 32, 21]) print("Lowest 5 marks is:", marks[-5:])

When we run above program, it produces following result −

Lowest 5 marks is: [67 65 54 32 21]

Example: Slicing Every Second Element (Reverse Order)

Let us create a 1D array where we need to slice every second element from the end of an array −

Open Compiler
import numpy as np data=np.array(['H','A','R','R','Y']) print(data[-1::-2])

When we run above program, it produces following result −

['Y' 'R' 'H']

Example: Reversing an Array

We can also use negative slicing for reversing an array. Following is the code −

Open Compiler
import numpy as np data=np.array([98,87,86,65,54,32,21]) print("Reversed data :", data[::-1])

Following is the output of the above code −

Reversed data : [21 32 54 65 86 87 98]

Special cases

In NumPy, Slicing we have special cases that enhance the flexibility of data manipulation. These include ellipsis (...) for partial indexing, full slices (:) to access all elements across dimensions, and newaxis for reshaping arrays.

Ellipsis

Slicing can also include the ellipsis '()' to make a selection tuple of the same length as the dimension of an array. If the ellipsis is used at the row position, it will return an ndarray comprising of items in rows.

Ellipsis (...) makes slicing in multiple dimensions much easier. It can use as many colons (:) as it wants to fill in the un-specified dimensions.

For example, in a three-dimensional array, a[..., 1] will pick all depths and rows but only the second column.

Example: Accessing Items in Specific Dimensions

Let us create a 2D array where we slice all the items in the second column using Ellipsis −

Open Compiler
import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5,6]]) print('The items in the second column are:', a[...,1] ) print('The items in the second row are:',a[1,...] ) print('The items column 1 onwards are:',a[...,1:])

Following is the output of the above code −

The items in the second column are: [2 4 5]
The items in the second row are: [3 4 5]
The items column 1 onwards are: [[2 3]
 [4 5]
 [5 6]]

Full Slices

Full slices are used to access all depths, rows, and columns from an array using (:). Let us see examples of each use case

Example: Accessing Rows, Columns, and Entire Array

Let us create a Food rating system where rows are users and columns are ratings of 4 different restaurants.

Open Compiler
import numpy as np Food_ratings = np.array([ [4, 5, 3, 4], # User 1 [3, 4, 2, 5], # User 2 [5, 5, 4, 4] # User 3 ]) # Get all ratings by User 1 user1 = Food_ratings[0, :] print("User 1 Ratings:", user1) # Get all ratings for restaurant 1 restuarent_ratings = Food_ratings[:, 0] print("restaurant 1 Ratings:", restuarent_ratings) # Get all ratings (entire table) all_ratings = Food_ratings[:, :] print("All Ratings:\n", all_ratings)

Following is the output of the above code −

User 1 Ratings: [4 5 3 4]
restaurant 1 Ratings: [4 3 5]
All Ratings:
 [[4 5 3 4]
 [3 4 2 5]
 [5 5 4 4]]

NewAxis

Newaxis in NumPy is the function that adds a new axis to an array, increasing it's dimensions. It is helpful in reshaping arrays and doing matrix transformations. By using newaxis, one can easily change a 1D array into a 2D row or column vector and vice versa: a 2D array into a 3D array and more.

Example: Converting 1D Array to 2D Column Vector

Let us create a 1D array and convert that into a 2D column vector

Open Compiler
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[:, np.newaxis])

Following is the output of the above code −

[[1]
 [2]
 [3]
 [4]]

Example: Combining 1D and 2D Arrays with hstack()

Let us consider two 1D arrays: one for rainfall monthly amounts in millimeter units and the other is names of months. Then we will convert this 1D into 2D arrays and then concats both with its horizontally axis using the hstack() function. This 2-dimensional array will then hold down both rainfall totals with respect to the month.

When we convert this into 2D, NumPy converts a mixed data type into a single type. In this case, the result will be an array of strings because the Months array consists of strings. So Numerical operations on rainfall array will no longer work.

Open Compiler
import numpy as np Rainfall = np.array([120, 85, 60, 90, 150]) Months = np.array(['Jan', 'Feb', 'Mar', 'Apr', 'May']) Rainfall_2d = Rainfall[:, np.newaxis] Months_2d = Months[:, np.newaxis] Rainfall_Data = np.hstack((Months_2d, Rainfall_2d)) print("Monthly Rainfall Data:") print(Rainfall_Data)

Following is the output of the above code −

Monthly Rainfall Data:
[['Jan' '120']
 ['Feb' '85']
 ['Mar' '60']
 ['Apr' '90']
 ['May' '150']]
Advertisements