1
1

numpyで重複有配列のrankを計算する方法

Posted at

配列に対して,各要素のrankingを求めたいとします.
ただし,引き分けがあった場合は,同順位として,次の順位は前の順位+1から再開するものとします.
例えば,[1,1,2,2,3]という配列の順位は[0,0,1,1,2]というように得たいものとします.
そのような計算は実はnp.uniqueで得ることができます.

import numpy as np

A = [1, 1, 2, 2, 3]
_, ranks = np.unique(A, return_inverse=True)
print(ranks)
>>> [0, 0, 1, 1, 2]
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1