Python Program Introduction Site

新しい次元の追加(numpy.newaxis)

2021.04.04

1件

配列に「bnumpy.newaxis」を用いると

行ベクトルや列ベクトルに変えることが出来ます。

W = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])
x = np.array([8, 9, 10]) # 3次元配列
print(W.shape)
print(W.T.shape)

print(x.shape)
print(x.T.shape) # x と x.T の shape は同じ

print(x[np.newaxis, :]) # 新しい次元を追加し、行ベクトルに変換
print(x[np.newaxis, :].shape)
print(x[np.newaxis, :].T.shape)

print(x[:, np.newaxis]) # 列操作により列ベクトルに変換
print(x[:, np.newaxis].shape)
print(x[:, np.newaxis].T.shape)

print(x[np.newaxis].shape) # x[np.newaxis,:]と同じ


(2, 4)
(4, 2)

(3,)
(3,)

[[ 8 9 10]]
(1, 3)
(3, 1)

[[ 8]
[ 9]
[10]]
(3, 1)
(1, 3)

(1, 3)

関連記事

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

python3X