How to rank items in an array using numpy?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to rank items in an array using numpy?

How to rank items in an array using numpy?

This recipe helps you rank items in an array using numpy

0

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 2 - Sort the array

Sorted_array = Sample_array.argsort()

Step 3 - 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]

Relevant Projects

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

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.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.