Q:

Write a NumPy program to sort the specified number of elements from beginning of a given array

0

Write a NumPy program to sort the specified number of elements from beginning of a given array.

All Answers

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

import numpy as np
nums =  np.random.rand(10)
print("Original array:")
print(nums)
print("\nSorted first 5 elements:")
print(nums[np.argpartition(nums,range(5))])

Sample Output:

Original array:
[0.39536213 0.11779404 0.32612381 0.16327394 0.98837963 0.25510787
 0.01398678 0.15188239 0.12057667 0.67278699]

Sorted first 5 elements:
[0.01398678 0.11779404 0.12057667 0.15188239 0.16327394 0.25510787
 0.39536213 0.98837963 0.32612381 0.67278699]

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