1
0

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 1 year has passed since last update.

numpyの...(Ellipsis)の使い方メモ

Posted at

概要

numpyを使用するコードで偶に出てくるX[..., n]...
検索してもなかなかHitしない...
自分用にメモする

...の名称

pythonの機能でEllipisという。
日本語では”省略記号”。

numpyでの使い方

配列を省略して記載する

# 確認用の配列。2進数っぽく値を取得。
# ex.
#   X[0,0,0,0] = 0
#   X[1,0,1,0] = 10
X = np.arange(0,16).reshape(2,2,2,2)

# 後方を省略
X[1,0,1,...] # <- array([10, 11])
X[0,...] # <- array([[[0, 1],[2, 3]],[[4, 5],[6, 7]]])

# 前方を省略
X[...,0,0,0] # <- array([0, 8])
X[...,1] # <- array([[[ 1,  3],[ 5,  7]],[[ 9, 11],[13, 15]]])

# 中間を省略
X[1,...,0] # <- array([[ 8, 10],[12, 14]])

配列の軸を追加する

Noneと共に使用して、単位軸を追加する。

Y = np.array([1,2,3,4,5])

# 配列の形状を[5]から[5,1]に変更
Y[..., None].shape # <- (5, 1)

# 配列の形状を[5]から[5,1]に変更
# 見かけたことは無い!
Y[None, ...].shape # <- (1, 5)

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?