Write a NumPy program to find the k smallest values of a given numpy array.
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]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer