Analyze Data/Python Libraries

numpy-pad

Naranjito 2023. 12. 26. 18:15
  • pad

Pad an array.

numpy.pad(array, pad_width, mode='constant', **kwargs)

Example 1.

 

(1,2) : low_before, low_after

(6,7) : col_before, col_after

b=[[1,2],[3,4],]
np.pad(b, ((1,2),(6,7)),'constant', constant_values=0)

>>>
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Example 2.

c=np.random.rand(1,3,3,3)
>>>
array([[[[0.93716004, 0.62682997, 0.29226598],
         [0.63802039, 0.4492049 , 0.52920476],
         [0.7873862 , 0.13877556, 0.43151375]],

        [[0.43483542, 0.27225612, 0.03929342],
         [0.63393174, 0.34305449, 0.99672895],
         [0.750275  , 0.96819218, 0.32382205]],

        [[0.77675184, 0.68849961, 0.38730129],
         [0.17889086, 0.16309005, 0.91900711],
         [0.00571749, 0.20271086, 0.70276315]]]])

The result is below.

 

(0,0),(0,1),(0,1),(0,0)

  1d,    2d,   3d,   4d

(before, after) ・・・

np.pad(c,((0,0),(0,1),(0,1),(0,0)), 'constant', constant_values=0)
>>>
array([[[[0.93716004, 0.62682997, 0.29226598],
         [0.63802039, 0.4492049 , 0.52920476],
         [0.7873862 , 0.13877556, 0.43151375],
         [0.        , 0.        , 0.        ]],

        [[0.43483542, 0.27225612, 0.03929342],
         [0.63393174, 0.34305449, 0.99672895],
         [0.750275  , 0.96819218, 0.32382205],
         [0.        , 0.        , 0.        ]],

        [[0.77675184, 0.68849961, 0.38730129],
         [0.17889086, 0.16309005, 0.91900711],
         [0.00571749, 0.20271086, 0.70276315],
         [0.        , 0.        , 0.        ]],

        [[0.        , 0.        , 0.        ],
         [0.        , 0.        , 0.        ],
         [0.        , 0.        , 0.        ],
         [0.        , 0.        , 0.        ]]]])

'Analyze Data > Python Libraries' 카테고리의 다른 글

numpy-zeros, zeros_like  (0) 2024.03.22
numpy-ogrid VS mgrid  (0) 2023.11.17
EasyDict  (0) 2023.10.18
numpy-concatenate  (0) 2023.06.07
numpy-identity  (0) 2023.06.02