Numpy-expand-dims

提供:Dev Guides
移動先:案内検索

numpy.expand_dims

この関数は、指定された位置に新しい軸を挿入することにより、配列を展開します。 この関数には2つのパラメーターが必要です。

numpy.expand_dims(arr, axis)

どこで、

Sr.No. Parameter & Description
1

arr

入力配列

2

axis

新しい軸が挿入される位置

import numpy as np
x = np.array(([1,2],[3,4]))

print 'Array x:'
print x
print '\n'
y = np.expand_dims(x, axis = 0)

print 'Array y:'
print y
print '\n'

print 'The shape of X and Y array:'
print x.shape, y.shape
print '\n'
# insert axis at position 1
y = np.expand_dims(x, axis = 1)

print 'Array Y after inserting axis at position 1:'
print y
print '\n'

print 'x.ndim and y.ndim:'
print x.ndim,y.ndim
print '\n'

print 'x.shape and y.shape:'
print x.shape, y.shape

上記のプログラムの出力は次のようになります-

Array x:
[[Array y:
[[The shape of X and Y array:
(2, 2) (1, 2, 2)

Array Y after inserting axis at position 1:
[[x.ndim and y.ndim:
2 3

x.shape and y.shape:
(2, 2) (2, 1, 2)