Q:

Write a NumPy program to find the k smallest values of a given numpy array

0

Write a NumPy program to find the k smallest values of a given numpy array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
array1 = np.array([1, 7, 8, 2, 0.1, 3, 15, 2.5])
print("Original arrays:")
print(array1)
k = 4
result = np.argpartition(array1, k)
print("\nk smallest values:")
print(array1[result[:k]])

Sample Output:

Original arrays:
[ 1.   7.   8.   2.   0.1  3.  15.   2.5]

k smallest values:
[0.1 1.  2.  2.5]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now