How to get the n-largest values of an array using NumPy? Last Updated : 29 Aug, 2020 Comments Improve Suggest changes Like Article Like Report Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, axis=-1, kind=’quicksort’, order=None) Return: [index_array, ndarray] Array of indices that sort arr along the specified axis.If arr is one-dimensional then arr[index_array] returns a sorted arr. Let's see an example: Example 1: Getting the 1st largest value from a NumPy array. Python3 # import library import numpy as np # create numpy 1d-array arr = np.array([2, 0, 1, 5, 4, 1, 9]) print("Given array:", arr) # sort an array in # ascending order # np.argsort() return # array of indices for # sorted array sorted_index_array = np.argsort(arr) # sorted array sorted_array = arr[sorted_index_array] print("Sorted array:", sorted_array) # we want 1 largest value n = 1 # we are using negative # indexing concept # take n largest value rslt = sorted_array[-n : ] # show the output print("{} largest value:".format(n), rslt[0]) Output: Given array: [2 0 1 5 4 1 9] Sorted array: [0 1 1 2 4 5 9] 1 largest value: 9 Example 2: Getting the 3-largest values from a NumPy array. Python3 # import library import numpy as np # create numpy 1d-array arr = np.array([2, 0, 1, 5, 4, 1, 9]) print("Given array:", arr) # sort an array in # ascending order # np.argsort() return # array of indices for # sorted array sorted_index_array = np.argsort(arr) # sorted array sorted_array = arr[sorted_index_array] print("Sorted array:", sorted_array) # we want 3 largest value n = 3 # we are using negative # indexing concept # find n largest value rslt = sorted_array[-n : ] # show the output print("{} largest value:".format(n), rslt) Output: Given array: [2 0 1 5 4 1 9] Sorted array: [0 1 1 2 4 5 9] 3 largest value: [4 5 9] Comment More infoAdvertise with us Next Article How to get the n-largest values of an array using NumPy? ankthon Follow Improve Article Tags : Python Python-numpy Python numpy-program Python numpy-Mathematical Function Practice Tags : python Similar Reads How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi 5 min read How to get the indices of the sorted array using NumPy in Python? We can get the indices of the sorted elements of a given array with the help of argsort() method. This function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that would sort the arra 2 min read How to calculate the element-wise absolute value of NumPy array? Let's see the program for finding the element-wise absolute value of NumPy array. For doing this task we are using numpy.absolute() function of NumPy library. This mathematical function helps to calculate the absolute value of each element in the array. Syntax: numpy.absolute(arr, out = None, ufunc 2 min read How to delete last N rows from Numpy array? In this article, we will discuss how to delete the last N rows from the NumPy array. Method 1: Using Slice Operator Slicing is an indexing operation that is used to iterate over an array.  Syntax: array_name[start:stop] where start is the start is the index and stop is the last index. We can also do 4 min read How to Calculate the Mode of NumPy Array? The goal here is to calculate the mode of a NumPy array, which refers to identifying the most frequent value in the array. For example, given the array [1, 1, 2, 2, 2, 3, 4, 5], the mode is 2, as it appears most frequently. Let's explore different approaches to accomplish this. Using scipy.stats.mod 2 min read Find the most frequent value in a NumPy array In this article, let's discuss how to find the most frequent value in the NumPy array. Steps to find the most frequency value in a NumPy array: Create a NumPy array.Apply bincount() method of NumPy to get the count of occurrences of each element in the array.The n, apply argmax() method to get the 1 min read Finding the k smallest values of a NumPy array In this article, let us see how to find the k number of the smallest values from a NumPy array. Examples: Input: [1,3,5,2,4,6] k = 3 Output: [1,2,3] Method 1: Using np.sort() . Approach: Create a NumPy array.Determine the value of k.Sort the array in ascending order using the sort() method.Print th 2 min read Get row numbers of NumPy array having element larger than X Let's see how to getting the row numbers of a numpy array that have at least one item is larger than a specified value X. So, for doing this task we will use numpy.where() and numpy.any() functions together. Syntax: numpy.where(condition[, x, y]) Return: [ndarray or tuple of ndarrays] If both x and 2 min read Get First and Second Largest Values in Pandas DataFrame When analyzing data in Python using the pandas library, you may encounter situations where you need to find the highest and second-highest values in a DataFrame's columns. This task can be crucial in various contexts, such as ranking, filtering top performers, or performing threshold-based analysis. 4 min read NumPy Array Sorting | How to sort NumPy Array Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.ar 4 min read Like