How to rank items in an array using numpy?

This recipe helps you rank items in an array using numpy

Recipe Objective

How to rank items in an array using numpy? This is possible by using "empty_like" and "argsort" functions available in the numpy library.

Step 1 - Import library

import numpy as np

Step 2 - Take a Sample array

Sample_array = np.array([1,4,8,7,10,11,6,2]) print("This is the original array:",Sample_array)

This is the original array: [ 1  4  8  7 10 11  6  2]

Step 3 - Sort the array

Sorted_array = Sample_array.argsort()

Step 4 - Print the Ranks

array_ranks = np.empty_like(Sorted_array) array_ranks[Sorted_array] = np.arange(len(Sample_array)) print("This is the original Sample array:", Sample_array, "\n") print("This is the Sorted array:", Sample_array[Sorted_array], "\n") print("This is the rank of element present in the array:", array_ranks)

This is the original Sample array: [ 1  4  8  7 10 11  6  2] 

This is the Sorted array: [ 1  2  4  6  7  8 10 11] 

This is the rank of element present in the array: [0 2 5 4 6 7 3 1]

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.