Baixe o app para aproveitar ainda mais
Prévia do material em texto
NumPy: Find the k smallest values of a given numpy array Last update on May 15 2021 12:40:30 (UTC/GMT +8 hours) NumPy: Array Object Exercise-160 with Solution Write a NumPy program to find the k smallest values of a given numpy array. Sample Solution: Python Code: Sample Output: Original arrays: [ 1. 7. 8. 2. 0.1 3. 15. 2.5] k smallest values: [0.1 1. 2. 2.5] Pictorial Presentation: 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]]) 1 7 8 2 0.1 3 15 2.5 0.1 1 2 2.5 1 7 8 2 0.1 3 15 2.5 Finding the k smallest values k smallest values 1st Smallest values 2nd Smallest values 3 rd Smallest values 4 th Smallest values k 4= w3resource.com
Compartilhar