How to sort NumPy Array?
×


How to sort NumPy Array?

555

How to Sort NumPy Array

Introduction

Sorting arrays is a fundamental operation in data analysis, enabling efficient data organization and retrieval. NumPy, a powerful library for numerical computations in Python, provides several methods to sort arrays. This article explores various techniques to sort NumPy arrays effectively.

1. Sorting Arrays with numpy.sort()

The numpy.sort() function returns a sorted copy of the input array. It offers flexibility to sort arrays along specific axes and supports different sorting algorithms.

Syntax:

numpy.sort(a, axis=-1, kind='quicksort', order=None)
  • a: The array to be sorted.
  • axis: Axis along which to sort. Default is -1 (last axis). If None, the array is flattened before sorting.
  • kind: Sorting algorithm. Options are 'quicksort', 'mergesort', 'heapsort', and 'stable'. Default is 'quicksort'.
  • order: When a is an array with fields defined, this argument specifies which fields to compare first, second, etc.

Example:

import numpy as np

arr = np.array([12, 15, 10, 1])
print("Array before sorting:", arr)
arr.sort()
print("Array after sorting:", arr)

Output:

Array before sorting: [12 15 10  1]
Array after sorting: [ 1 10 12 15]

2. Sorting Arrays Along Specific Axes

For multi-dimensional arrays, you can sort along a specific axis using the axis parameter.

Example:

import numpy as np

arr = np.array([[12, 15], [10, 1]])
sorted_arr_axis0 = np.sort(arr, axis=0)
sorted_arr_axis1 = np.sort(arr, axis=1)
print("Sorted along axis 0:\n", sorted_arr_axis0)
print("Sorted along axis 1:\n", sorted_arr_axis1)

Output:

Sorted along axis 0:
 [[10  1]
 [12 15]]
Sorted along axis 1:
 [[12 15]
 [ 1 10]]

3. Sorting Arrays In-Place

To sort an array in-place, modifying the original array without creating a new one, use the sort() method directly on the array.

Example:

import numpy as np

arr = np.array([12, 15, 10, 1])
print("Array before sorting:", arr)
arr.sort()
print("Array after sorting:", arr)

Output:

Array before sorting: [12 15 10  1]
Array after sorting: [ 1 10 12 15]

4. Using numpy.argsort() for Indirect Sorting

The numpy.argsort() function returns the indices that would sort an array. This is useful when you need to sort data based on the indices rather than the data itself.

Example:

import numpy as np

arr = np.array([9, 3, 1, 7, 4, 3, 6])
indices = np.argsort(arr)
sorted_arr = arr[indices]
print("Original array:", arr)
print("Sorted indices:", indices)
print("Sorted array:", sorted_arr)

Output:

Original array: [9 3 1 7 4 3 6]
Sorted indices: [2 1 5 4 6 3 0]
Sorted array: [1 3 3 4 6 7 9]

5. Sorting Arrays Using Multiple Keys with numpy.lexsort()

The numpy.lexsort() function performs an indirect stable sort using a sequence of keys. It is useful when sorting based on multiple criteria.

Example:

import numpy as np

a = np.array([9, 3, 1, 3, 4, 3, 6])
b = np.array([4, 6, 9, 2, 1, 8, 7])
indices = np.lexsort((b, a))
sorted_arr = a[indices]
print("Sorted indices:", indices)
print("Sorted array:", sorted_arr)

Output:

Sorted indices: [2 3 1 5 4 6 0]
Sorted array: [1 3 3 3 4 6 9]

Conclusion

Sorting is a fundamental operation in data analysis, and NumPy provides efficient methods to sort arrays in various ways. Whether you need to sort an array in-place, along specific axes, or based on multiple criteria, NumPy's sorting functions offer the flexibility and performance required for effective data manipulation.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat