Issue
I am trying to do these steps in NumPy. It was easy to do this with python list sort()
, and argsort()
.
How do I do this in Numpy?
a = np.array([10,30,20,40,50])
a_sorted = np.array([10,20,30,40,50])
Get mask of a_sorted
b = np.array(['one','three','two','four','five'])
Apply the mask to b
Expected array sorted according to a_sorted:
b_sorted = np.array(['one','two','three','four','five'])
Solution
Try like this
import numpy as np
a = np.array([10, 30, 20, 40, 50])
a_sorted_indices = np.argsort(a)
a_sorted = a[a_sorted_indices]
b = np.array(['one', 'three', 'two', 'four', 'five'])
b_sorted = b[a_sorted_indices]
print("Sorted array 'a':", a_sorted)
print("Sorted array 'b':", b_sorted)
Output:
Sorted array 'a': [10 20 30 40 50]
Sorted array 'b': ['one' 'two' 'three' 'four' 'five']
Answered By - Mahboob Nur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.