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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

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.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.