Uncategorized

partition() – Partial Sort in NumPy

partition() – Partial Sort in NumPy

Hi Enthusiastic Learners! In this article we will be studying about “partition() – partial sort in NumPy“. Have you ever wondered, if you are given an array and you only want n smallest numbers from your array instead of completely sorting it? Well, that is exactly the case where we should use partition() - partial sort & and we are calling partition() a partial sort because we actually will be sorting only few parts of given array or you can say getting a partition of n smallest values. Before jumping on to partition() – partial sort in NumPy, please check out other articles related to sorting in NumPy:

  1. lexsort() – Indirect Sort in NumPy
  2. Sort Arrays in NumPy

Watch video tutorial here:

YouTube player

partition() – Partial Sort Syntax

np.partition(arr, pos, axis=-1, kind='introselect', order=None)

Where,

  • arr is the array which has to be sorted
  • pos position in array.
  • axis defines the axis along which you need to do sorting & -1 value means last axis.
  • kind gives you liberty to choose sorting algorithm. Default algo ‘introselect’
  • order — if arr is a structured array, this argument specifies which fields to compare first, second, etc.

Let’s begin with creating a array arr.

In [1]:
import numpy as np

arr = np.array([3, 7, 4, 2, 8, 9, 0, 18, 1])
print("-- Base Array --")
print(arr)
-- Base Array --
[ 3  7  4  2  8  9  0 18  1]

Sorting array for pos = 3. Now, partition() will create a copy of original array and will arrange number in such a way such that till pos all values will be the smallest values in array & after that position other values will be as it is.

Note that they may not be ordered. Its just that, till pos = 3, that is 4 elements as indexing in arrays starts from 0, we will be getting 4 smallest values & remaining values will be shifted as it is towards right and there is no order in sorted values.

Syntax for it will be — np.partition(arr, 2)

In [2]:
sort_for_3 = np.partition(arr, 3)

print("-- Sorted Array till 3rd Position --")
print(sort_for_3)
-- Sorted Array till 3rd Position --
[ 2  1  0  3  7  9  4 18  8]

As, we can clearly from above example, that 1st four elements (till pos = 3) are smallest among complete array & other values are just shifted towards right in the same order they naturally were. Also, there is no order in sorted values.

partition() on 2-dimensional array

We can do partial sort for 2-D arrays as well & we can choose axis along which we want values to be sorted.

Let’s begin with creating a 2-D array

In [3]:
arr_2d = np.random.randint(0, 10, (4, 6))

print("-- 2D Array --\n")
print(arr_2d)
-- 2D Array --

[[2 5 6 1 6 3]
 [7 0 2 6 0 0]
 [1 6 2 6 4 3]
 [9 5 9 6 9 3]]

Get 2 smallest values for each row of array.

Syntax for it will be — np.partition(arr_2d, 1, axis=1)

In [4]:
sorted_2d = np.partition(arr_2d, 1, axis=1)

print("-- Partially Sorted 2D Array -- \n")
print(sorted_2d)
-- Partially Sorted 2D Array -- 

[[1 2 6 5 6 3]
 [0 0 2 6 7 0]
 [1 2 6 6 4 3]
 [3 5 9 6 9 9]]

From above example it clear, that for each row we are getting smallest value till index (pos) value = 1, that is 2 smallest elements in each row.

Stay tuned & keep learning! In our next post we will be covering more sorting techniques that can be achieved in NumPy.

Leave a Reply

Back To Top
%d bloggers like this: