How to get the positions of top n values from a numpy array?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

# How to get the positions of top n values from a numpy array?

This recipe helps you get the positions of top n values from a numpy array

0

## Recipe Objective

How to get the positions of top n values from a numpy array? So we can achieve this by using the "argpartition" so the syntax for the same is as follows:

np.argpartition(arr,-n)[-n:]

where, arr - array that we are going to take n - the desired number of indices.

## Step 1 - Import library

``` import numpy as np ```

## Step 2 - Take a Sample array

``` Sample_array = np.array([1,3,5,7,9]) n = 3 print("This is the original array:",Sample_array) ```
`This is the original array: [1 3 5 7 9]`

## Step 3 - Sort the array

``` sorted_index = np.argsort(Sample_array) sorted_values = Sample_array[sorted_index] print("The sorted array is:", sorted_values) ```
`The sorted array is: [1 3 5 7 9]`

## Step 4 - Print the top values

``` print("The top values are:",sorted_values[-n:]) ```
`The top values are: [5 7 9]`

#### Relevant Projects

##### Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

##### 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.

##### 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.

##### 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.

##### Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

##### 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.

##### Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

##### Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

##### Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

##### Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.