Finding the k smallest values of a NumPy array Last Updated : 02 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the first k values of the sorted array. Python3 # importing the modules import numpy as np # creating the array arr = np.array([23, 12, 1, 3, 4, 5, 6]) print("The Original Array Content") print(arr) # value of k k = 4 # sorting the array arr1 = np.sort(arr) # k smallest number of array print(k, "smallest elements of the array") print(arr1[:k]) Output: The Original Array Content [23 12 1 3 4 5 6] 4 smallest elements of the array [1 3 4 5] Method 2: Using np.argpartition() Approach: Create a NumPy array.Determine the value of k.Get the indexes of the smallest k elements using the argpartition() method.Fetch the first k values from the array obtained from argpartition() and print their index values with respect to the original array. Python3 # importing the module import numpy as np # creating the array arr = np.array([23, 12, 1, 3, 4, 5, 6]) print("The Original Array Content") print(arr) # value of k k = 4 # using np.argpartition() result = np.argpartition(arr, k) # k smallest number of array print(k, "smallest elements of the array") print(arr[result[:k]]) Output: The Original Array Content [23 12 1 3 4 5 6] 4 smallest elements of the array [4 3 1 5] Comment More infoAdvertise with us Next Article Finding the k smallest values of a NumPy array H hupphurr Follow Improve Article Tags : Python Python-numpy Python numpy-Sorting Searching Practice Tags : python Similar Reads Find the nearest value and the index of NumPy Array In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Arra 3 min read 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 Python heapq to find K'th smallest element in a 2D array Given an n x n matrix and integer k. Find the k'th smallest element in the given 2D array. Examples: Input : mat = [[10, 25, 20, 40], [15, 45, 35, 30], [24, 29, 37, 48], [32, 33, 39, 50]] k = 7 Output : 7th smallest element is 30 We will use similar approach like Kâth Smallest/Largest Element in Uns 3 min read How to get the n-largest values of an array using NumPy? 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, ax 2 min read Python | Find smallest element greater than K This problem involves searching through a list to identify the smallest number that is still larger than K. We will explore different methods to achieve this in Python In this article, we'll look at simple ways to find the smallest element greater than k in a list using Python.Using Binary SearchBin 3 min read Like