Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Sorting with Fancy Indexing Explained – Advanced Array Sorting in Python

NumPy Sorting with Fancy Indexing

Fancy indexing in NumPy is a powerful technique that allows you to access and reorder array elements using arrays of indices.

When combined with sorting, fancy indexing helps you:

  • Reorder data based on sorted positions
  • Extract custom sorted views
  • Build advanced data manipulation logic

What is Fancy Indexing?

Fancy indexing means using arrays of indices instead of single values or slices.

Example:

arr[[3, 1, 0]]

This selects elements in custom order.


Why Use Fancy Indexing for Sorting?

  • Custom sorting control
  • Works with complex datasets
  • Combines well with argsort()
  • Useful in data science and ML
  • Allows reordering based on conditions

1. Basic Sorting with Fancy Indexing

We first use argsort() to get sorted indices.

import numpy as np

arr = np.array([50, 10, 40, 20])

sorted_indices = np.argsort(arr)

print(sorted_indices)
print(arr[sorted_indices])

Output

[1 3 2 0]
[10 20 40 50]

2. How It Works

Original array:   [50, 10, 40, 20]  
Sorted indices: [ 1, 3, 2, 0]
Sorted result: [10, 20, 40, 50]

3. Descending Order Sorting

import numpy as np

arr = np.array([50, 10, 40, 20])

sorted_indices = np.argsort(arr)[::-1]

print(arr[sorted_indices])

Output

[50 40 20 10]

4. Fancy Indexing with 2D Arrays

import numpy as np

arr = np.array([
[3, 1, 2],
[9, 5, 6]
])

indices = np.argsort(arr, axis=1)

print(indices)
print(np.take_along_axis(arr, indices, axis=1))

Output

[[1 2 0]
[1 2 0]]

[[1 2 3]
[5 6 9]]

5. Sorting Rows Using Fancy Indexing

import numpy as np

arr = np.array([30, 10, 20, 40])

sorted_arr = arr[np.argsort(arr)]

print(sorted_arr)

Output

[10 20 30 40]

6. Reordering Based on Custom Index

import numpy as np

arr = np.array([100, 200, 300, 400])

order = [3, 1, 0, 2]

print(arr[order])

Output

[400 200 100 300]

7. Sorting Strings with Fancy Indexing

import numpy as np

arr = np.array(["banana", "apple", "cherry"])

indices = np.argsort(arr)

print(arr[indices])

Output

['apple' 'banana' 'cherry']

8. Sorting Rows by a Column Value

import numpy as np

data = np.array([
[3, 100],
[1, 50],
[2, 150]
])

sorted_data = data[data[:, 1].argsort()]

print(sorted_data)

Output

[[  1  50]
[ 3 100]
[ 2 150]]

9. Real-World Example: Student Marks Sorting

import numpy as np

students = np.array(["A", "B", "C"])
marks = np.array([85, 70, 95])

sorted_students = students[np.argsort(marks)]

print(sorted_students)

Output

['B' 'A' 'C']

10. Real-World Example: Sales Ranking

import numpy as np

sales = np.array([500, 200, 900, 300])

ranked_sales = sales[np.argsort(sales)[::-1]]

print(ranked_sales)

Output

[900 500 300 200]

Key Concepts Summary

ConceptDescription
Fancy Indexing            Using index arrays
argsort()            Returns sorted indices
take_along_axis            Advanced sorting tool
[::-1]            Reverse order

Advantages of Fancy Index Sorting

  • Powerful custom sorting
  • Works with multi-dimensional arrays
  • Easy data rearrangement
  • Useful in ML pipelines
  • High flexibility for datasets

Summary

NumPy fancy indexing allows advanced sorting by using index arrays instead of direct values. Combined with argsort(), it enables powerful data reordering and structured dataset manipulation.

This functionality is part of NumPy and is widely used in applications built with Python.


Conclusion

Fancy indexing is an advanced NumPy technique that makes sorting and data manipulation highly flexible. It is especially useful in data science, machine learning, and structured data analysis.




Post a Comment

0 Comments