7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[numpy] データの各要素を大小関係でランク付ける

Last updated at Posted at 2019-03-12

$[x_1,x_2,\ldots,x_n]$のデータが与えられた時に、それぞれの要素の(小さい方から数えた)順位を返すには, np.argsortを二回使えば良い。

import numpy as np

x = np.random.randn(6)
rank = np.argsort(np.argsort(x))
print(x)
print(rank)

>>>
[-0.6828045  -0.93062684 -0.994558    0.03204029  0.26457471  1.3978161 ]
[2 1 0 3 4 5]

それぞれの要素の順位に変換されていることがわかる。

7
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
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?