Python 2.6.6 と NumPy バージョン 1.5.0 を使用して、2D NumPy 配列をゼロで埋め込む方法を知りたいです。しかし、これらは私の制限です。したがって、 は使用できません。たとえば、形状が と一致するようにゼロでnp.pad
埋め込む必要があります。これを行う理由は、次の操作を実行できるようにするためです。a
b
b-a
そのような
>>> a
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]])
>>> b
array([[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.],
[ 3., 3., 3., 3., 3., 3.]])
>>> c
array([[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0]])
これを行うには追加するしか方法がありませんが、これはかなり見苦しいようです。 を使用したよりクリーンなソリューションはありますかb.shape
?
編集、MSeiferts の回答に感謝します。少し整理する必要がありましたが、次のような結果になりました:
def pad(array, reference_shape, offsets):
"""
array: Array to be padded
reference_shape: tuple of size of ndarray to create
offsets: list of offsets (number of elements must be equal to the dimension of the array)
will throw a ValueError if offsets is too big and the reference_shape cannot handle the offsets
"""
# Create an array of zeros with the reference shape
result = np.zeros(reference_shape)
# Create a list of slices from offset to offset + shape in each dimension
insertHere = [slice(offsets[dim], offsets[dim] + array.shape[dim]) for dim in range(array.ndim)]
# Insert the array in the result at the specified offsets
result[insertHere] = array
return result
ベストアンサー1
NumPy 1.7.0(numpy.pad
追加された)はかなり古い(2013年にリリースされた)ので、質問では使うことなくその機能をどのように実現できるかを知ることは役に立つと思いましたnumpy.pad
。
実のところ、とても簡単です。
>>> import numpy as np
>>> a = np.array([[ 1., 1., 1., 1., 1.],
... [ 1., 1., 1., 1., 1.],
... [ 1., 1., 1., 1., 1.]])
>>> np.pad(a, [(0, 1), (0, 1)], mode='constant')
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
この場合、0
のデフォルト値を使用しましたmode='constant'
。ただし、明示的に渡すことで指定することもできます。
>>> np.pad(a, [(0, 1), (0, 1)], mode='constant', constant_values=0)
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
2番目の引数()がわかりにくい場合は[(0, 1), (0, 1)]
、各リスト項目(この場合はタプル)は次元に対応し、その中の項目はパディングを表します。前に(最初の要素) と後(2 番目の要素)。つまり、
[(0, 1), (0, 1)]
^^^^^^------ padding for second dimension
^^^^^^-------------- padding for first dimension
^------------------ no padding at the beginning of the first axis
^--------------- pad with one "value" at the end of the first axis.
この場合、最初の軸と 2 番目の軸のパディングは同じなので、2 つのタプルを渡すこともできます。
>>> np.pad(a, (0, 1), mode='constant')
array([[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0.]])
前後のパディングが同一の場合は、タプルを省略することもできます (ただし、この場合は適用されません)。
>>> np.pad(a, 1, mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 1., 1., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
または、軸の前後のパディングが同じだが、軸ごとに異なる場合は、内部タプルの 2 番目の引数を省略することもできます。
>>> np.pad(a, [(1, ), (2, )], mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
ただし、NumPys の期待が意図と異なる場合など、間違いが起こりやすいため、明示的な方法を使用することを常に好みます。
>>> np.pad(a, [1, 2], mode='constant')
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 1., 1., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
ここで NumPy は、すべての軸を各軸の前に 1 つの要素、各軸の後に 2 つの要素で埋め込むことを意図していると考えます。たとえ、軸 1 に 1 つの要素、軸 2 に 2 つの要素で埋め込むことを意図していたとしてもです。
パディングにはタプルのリストを使用しましたが、これは単なる「私の慣例」であり、リストのリストやタプルのタプル、さらには配列のタプルを使用することもできます。NumPy は引数の長さ (または長さがないか) と各項目の長さ (または長さがあるか) をチェックするだけです。