Finding the k smallest values of a NumPy array Last Updated : 02 Sep, 2020 Comments Improve Suggest changes 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 Software Developer (SDE) Interview/Placement Preparation Guide H hupphurr Follow Improve Article Tags : Python Python-numpy Python numpy-Sorting Searching Practice Tags : python Similar Reads Interview Preparation Interview Preparation For Software Developers Must Coding Questions - Company-wise Must Do Coding Questions - Topic-wise Company-wise Practice Problems Company Preparation Competitive Programming Software Design-Patterns Company-wise Interview Experience Experienced - Interview Experiences Internship - Interview Experiences Practice @Geeksforgeeks Problem of the Day Topic-wise Practice Difficulty Level - School Difficulty Level - Basic Difficulty Level - Easy Difficulty Level - Medium Difficulty Level - Hard Leaderboard !! Explore More... Data Structures Arrays Linked List Stack Queue Binary Tree Binary Search Tree Heap Hashing Graph Advance Data Structures Matrix String All Data Structures Algorithms Analysis of Algorithms Searching Algorithms Sorting Algorithms Pattern Searching Geometric Algorithms Mathematical Algorithms Randomized Algorithms Greedy Algorithms Dynamic Programming Divide & Conquer Backtracking Branch & Bound All Algorithms Programming Languages C C++ Java Python C# Go Lang SQL PHP Scala Perl Kotlin Web Technologies HTML CSS JavaScript Bootstrap Tailwind CSS AngularJS ReactJS jQuery NodeJS PHP Web Design Web Browser File Formats Computer Science Subjects Operating Systems DBMS Computer Network Computer Organization & Architecture TOC Compiler Design Digital Elec. & Logic Design Software Engineering Engineering Mathematics Data Science & ML Complete Data Science Course Data Science Tutorial Machine Learning Tutorial Deep Learning Tutorial NLP Tutorial Machine Learning Projects Data Analysis Tutorial Tutorial Library Python Tutorial Django Tutorial Pandas Tutorial Kivy Tutorial Tkinter Tutorial OpenCV Tutorial Selenium Tutorial GATE CS GATE CS Notes Gate Corner Previous Year GATE Papers Last Minute Notes (LMNs) Important Topic For GATE CS GATE Course Previous Year Paper: CS exams Like