Home » Numpy Sort for Machine Learning

Numpy Sort for Machine Learning

Numpy helps your machine learning python code by adding the power and flexibility of arrays and matrices to it. Hence, there are times when you need to sort those arrays to suit your algorithm. For such occasions, we use NumPy sort function, written below.

numpy.sort()

This function returns a sorted copy of your original array. Now let’s take a look at the function in detail starting from creating a numpy array.

Create a numpy array to test numpy sort function

I’m creating an array called nnl_array, nnl standards for neural network lab. (In case if you are wondering why I’m using nnl naming convention in a lot of tutorials.. 🙂 ) Let us create our two-dimensional array so we can learn how this function actually works.

nnl_array = np.array([[1,5,3], [6,5,7]])

Let’s print this array out.

print ("original array", "\n", nnl_array)

The Result:

print two dimensional array

It prints our original array, nothing fancy. Now let’s sort this using the numpy sort function.

Sorting the two dimentional array using numpy sort default function

numpy sort

The minimum required parameter for the function numpy.sort() to work is the array itself. In this case it’s our nnl_array

 nnl_array = np.sort(nnl_array)

Now shall we run a print command to investigate the sorted array?

The Result:

Numpy sort array against original array

As we can see both rows have been sorted in ascending order, but separately. This is perfect!

Parameters of numpy.sort() function

We can use all the available parameters for this function when we need more control or to suit what we need from our machine learning algorithm.

What parameters do numpy.sort() function take? Let’s write the full function with all the parameters in it.

numpy.sort(aaxis=- 1kind=Noneorder=None)

Brief explanation of the numpy.sort() parameters

We need to understand what these parameters are before moving on and write some code!

a” is the only mandatory parameter. All the others are optional.

a – The Array that you wish to sort

axis – axis where the array is sorted. arguments can be int or none. The default value is -1 which is the last axis. If none is selected the rows will be flattened before sorting takes place.

kind – This parameter defines the sorting algorithm. The default value is ‘quicksort” You can use quicksort, heapsort, mergesort as you wish.

order – When the array has fields defined, this parameter tells which fields to be sorted first, second, and so on.

Complete Numpy Array Sort example for machine learning

Let’s write some code, shall we? 🙂

First, we need to import the NumPy module into our code. I’m using PyCharm by the way. You can use your preferred python IDE.

Importing Numpy

import numpy as np

Creating our original array

I’m going to use the same 2-dimensional array I used earlier.

nnl_array = np.array([[1,5,3], [6,5,7]])

Sorting the array along axis: axis = none, -1, 0 and 1 and printing

Let’s name our return arrays for axis = none, -1, 0, 1 respectively a_nnl_array, b_nnl_array, c_nnl_array, and, d_nnl_array.

#original 2-dimentional array
nnl_array = np.array([[1,5,3], [6,5,7]])

print ("original array", "\n", nnl_array)

#axis=none
a_nnl_array = np.sort(nnl_array, axis=None)

print("axis=none", "\n", a_nnl_array)

#axis=-1
b_nnl_array = np.sort(nnl_array, axis=-1)

print("axis=-1", "\n", b_nnl_array)

#axis=0
c_nnl_array = np.sort(nnl_array, axis=0)

print("axis=0", "\n", c_nnl_array)

#axis=1
d_nnl_array = np.sort(nnl_array, axis=1)

print("axis=1", "\n", d_nnl_array)

The Result:

sorting numpy array axis=none axis=-1 axis=0 axis=-1

Testing different arguments for the parameter “order”

The numpy sort parameter called “order” can be very useful when it comes to developing real-world python programs, especially for machine learning projects. So I decided to write a separate code example just to explain how we can use this parameter to sort some meaningful data.

Let’s assume that we have information about several buildings in an urban area with their names, building height, and how old they are.

Creating a structured array

We can create a structured array to store this information, written below.

import numpy as np

building_dtypes = [('building_name', 'S10'), ('building_height', float), ('buidling_age', int)]
builiding_values = [('unity', 35, 10), ('grand Palace', 50, 30), ('AM Moters', 15, 4)]

#our building structured array
building_array = np.array(builiding_values, dtype=building_dtypes)

print("original Structured Array", building_array)

The Result:

Creating a structured array

Sorting the structured array using the heights of the buildings

buidlingheight_array = np.sort(building_array, order='building_height')
Prinitng the original structured array & sorted structured array
print("original Structured Array", building_array)
print("sorted against height", buidlingheight_array)

The Result:

sorting a structured array using order parameter

Conclusion

Numpy is by far the most popular Python library for multi-dimensional arrays and metrics. Numpy Sort function provides all the parameters to sort out NumPy arrays for any kind of machine learning algorithm.

What’s next?

Did you like this post regarding the Numpy Sort function? Do you want to learn about the NumPy argsort function as well? Then read the post below.

To learn everything about python for data science and get hired by Top Tech Companies Enroll With Eduraka Today.

1 thought on “Numpy Sort for Machine Learning”

  1. Pingback: Numpy Argsort with Example - Neural Net Lab

Leave a Reply