LoginSignup
0
0

More than 1 year has passed since last update.

numpyでargsortの逆操作を行う配列を (sort1回のみで) 簡潔に得る

Posted at

表題のとおりです.

以下のように行うとsortは一回で済み,for文を使う必要もないです.

import numpy as np

# 配列を定義
size = 1 << 20
x = np.random.random(size)

# sortを行う
order = np.argsort(x)

# 逆操作を得る (以下2行)
order_inv = np.zeros_like(order)
order_inv[order] = np.arange(size)

# 検証
np.allclose(x, x[order][order_inv])
>>> True
0
0
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
0
0