Q:

Write a NumPy program to create an array that represents the rank of each item of a given array

0

Write a NumPy program to create an array that represents the rank of each item 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 numpy
array = numpy.array([24, 27, 30, 29, 18, 14])
print("Original array:")
print(array)
argsort_array = array.argsort()
ranks_array = numpy.empty_like(argsort_array)
ranks_array[argsort_array] = numpy.arange(len(array))
print("\nRank of each item of the said array:")
print(ranks_array)

Sample Output:

Original array:
[24 27 30 29 18 14]

Rank of each item of the said array:
[2 3 5 4 1 0]

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