We often need to rank data in a non-parametric test. We can use the rankdata() function from the scipy.stats module to rank data.
from scipy.stats import rankdata data = [1, 3, 2, 2, 5, 8, 10, 3, 4, 3, 10] ranks = rankdata(data) print("Data: ", data) print("Ranks: ", ranks)
The output of the above program will be like the following:
Data: [1, 3, 2, 2, 5, 8, 10, 3, 4, 3, 10] Ranks: [ 1. 5. 2.5 2.5 8. 9. 10.5 5. 7. 5. 10.5]
Please note that if the data associated with two or more ranks are the same, we replace the ranks with the average rank. For example, the data associated with ranks 2 and 3 is 2. So, we replace the ranks 2 and 3 with the average ranks (2 + 3)/2 = 2.5.






0 Comments