Skip to main content

How to sort arrays in NumPy

How to sort arrays in NumPy
Hi guys. welcome to this interesting session on this platform. If you are new here, please do checkout our previous articles on NumPy and python. Do checkout our YouTube channel to get the full video tutorial of our articles.

In the previous article, we saw how we could search for arrays in NumPy using different functions. In this article of today, we are going to see how we can sort different elements in input array by using different built-in functions in NumPy.

To begin with sorting refers means arranging elements in an orderly manner such as alphabetically, numerically, ascending and many more.

Using sort () functions

This is a built-in function in NumPy that returns a copy of the sorted array in order. This array is arranged in sorted format.

Example 

import numpy as np
arr = np.array(['e', 'b', 'a', 'c'])
print(np.sort(arr))

NumPy passes the command to the sort function which instructs the compiler to arrange the alphabets in order.

OUTPUT

How to sort arrays in NumPy
Using np.argsort()

This function is used when we want the indices of the array, after when it has been sorted. 

Example

import numpy as np
arr = np.array([[6,1,3,8,12,2]])
print(np.argsort(arr))

OUTPUT

How to sort arrays in NumPy

In the table below, is a list functions with their uses we can use to sort out different arrays.

Functions

When to use them

np.ndarraysort()

Use to sort arrays in order

np.msort()

Returns arrays that are sorted along the first axis

np.partition()

Divides the array sorted into smaller arrays or partition

np.argpartition(

It returns the specified array after it has been partition

np.sort_compex()

It sorts out the real and imaginary parts of an array

Arrays of different data-types can be sorted using the sort() functions

Example

Sorting the array of a Boolean expressions

import numpy as np
arr = np.array([True, False, True])
print(np.sort(arr))

NumPy passes the command to the function, which then tells the compiler to sort out given expression.

OUTPUT

How to sort arrays in NumPy
Sorting out 2-D array

When the sort function is used on 2-D array, both the arrays in the given 2-D are sorted

Example

import numpy as np
arr = np.array([['a', 'm', 'd'], ['z', 'f', 'j']])
print(np.sort(arr))

Just like in the example above, the functions are being sorted by the compiler, but in this case the two given arrays are sorted simultaneously.

OUTPUT

How to sort arrays in NumPy

There are many other functions used in sorting that were not mention in this article. Sorting is very important in arranging data in a particular order. Hope this article was very interesting. Have a nice day coding.


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...