Finding the k smallest values of a NumPy array


Finding the k smallest values of a NumPy arra

Installation and Syntax

Pip, Python's package installer, is often used to install NumPy.

pip install numpy

The following function may be used to identify the k NumPy array elements with the smallest values −

np.sort(array)[:k]

This returns the first k items of the sorted array after sorting it in ascending order. The array may be be sorted using the alternative syntax shown below, which will retrieve the last k entries and sort the array in descending order −

np.sort(array)[::-1][:k]

Algorithm

  • The algorithm for finding the k smallest values of a NumPy array is relatively simple −

  • Sort the array in ascending order

  • Return the first k elements of the sorted array

  • To discover the k greatest values, you may also sort the array in decreasing order and retrieve the last k entries.

Example

import numpy as np
array = np.array([3, 7, 1, 4, 2, 8, 5, 9, 6])
k = 3
result = np.sort(array)[:k]
print(result)

Output

[1 2 3]

We want to identify the three lowest values from an array of numbers. The array is sorted in ascending order using the np.sort() method, and the first three entries are then cut using [:k].

Example

import numpy as np
array = np.array([3.2, 7.8, 1.5, 4.6, 2.9, 8.1, 5.4, 9.3, 6.7])
k = 4
result = np.sort(array)[::-1][:k]
print(result)

Output

[9.3 8.1 7.8 6.7]

We are looking for the four greatest values in a floating-point numeric array. We slice the first four entries using [:k] after using the np.sort() method to sort the array in decreasing order using [::-1].

Example

import numpy as np
array = np.array([[1, 2], [3, 4]])
k = 2
result = np.sort(array.flatten())[:k]
print(result)

Output

[1 2]

The flatten() function is used to convert a two-dimensional array into a one-dimensional one, and then np.sort() and slicing are used to locate the two lowest values in the array.

Example

import numpy as np

# Generate a random 2-dimensional array of size 5x5
array = np.random.randint(0, 10, size=(5, 5)).astype(float) # convert to float

# Print the original array
print("Original array:")
print(array)

# Flatten the array to make it 1-dimensional
flat_array = array.flatten()

# Find the 3 smallest values in the flattened array
k = 3
smallest_values = np.sort(flat_array)[:k]

# Create a mask for the smallest values
mask = np.isin(array, smallest_values)

# Replace the smallest values with NaNs
array[mask] = np.nan 

# Print the modified array
print("Modified array:")
print(array)

Output

Original array:
[[5. 7. 7. 6. 4.]
 [6. 2. 5. 4. 3.]
 [5. 1. 2. 0. 9.]
 [7. 9. 7. 1. 5.]
 [3. 0. 2. 5. 2.]]
Modified array:
[[ 5.  7.  7.  6.  4.]
 [ 6.  2.  5.  4.  3.]
 [ 5. nan  2. nan  9.]
 [ 7.  9.  7. nan  5.]
 [ 3. nan  2.  5.  2.]]

Generate a random 2-dimensional array of integers using `np.random.randint()`. We then flatten the array using `array.flatten()` and find the 3 smallest values using `np.sort()`. We create a mask for the smallest values using `np.isin()` and replace them with NaNs using `np.nan`. Finally, we print the modified array to verify that the smallest values have been replaced with NaNs.

Applications

  • Identifying outliers in a dataset

  • Ranking items based on their values

  • Analyzing stock market trends

  • Machine learning and data mining algorithms

Conclusion

The challenge of identifying the k lowest values in a NumPy array has been covered in this blog. Together with numerous short code examples and one large example with a thorough explanation, we have included the syntax and technique for overcoming this issue and also discussed some of the ways that this problem is applied in domains like data analysis and other ones. Finding the k lowest values is only one of many helpful features that NumPy's robust toolkit for working with multi-dimensional arrays and matrices offers.

Updated on: 21-Aug-2023

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements