Home > Software engineering >  Resizing a 3D array and filling with zeros
Resizing a 3D array and filling with zeros

Time:08-19

I have a NumPy array made of ragged nested sequences such as the following:

arr = np.array((
    np.random.random((2, 2, 2)),
    np.random.random((4, 4, 4)),
    np.random.random((2, 2, 2))
))

I want to resize each of the nested arrays to the shape (4, 4, 4) by filling it with zeros.

I initially looked at this post numpy - resize array filling with 0 which works for 2D NumPy arrays but, I have struggled to modify it for a 3D NumPy array. So far I have tried iterating over the individual nested arrays however, even with some fairly basic code such as

for i, a in enumerate(arr[0]):
    arr[0][i] = np.hstack([a, np.zeros([a.shape[0], 2])])

It still creates an error.

ValueError: could not broadcast input array from shape (2,4) into shape (2,2)

I could create separate variables for every nested array except this feels very slow and inefficient and I'd need even messier code to extend this to all 3 dimensions.

An example of a test:

arr = [[[0.1, 0.4],
      [0.3, 0,7]],
      [[0.5, 0.2],
      [0.8, 0.1]]] 

If I wanted it to have the shape (2, 3, 4) the output would be the following

[[[0.1, 0.4, 0.0, 0.0],
[0.3, 0,7, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]],
[[0.5, 0.2, 0.0, 0.0],
[0.8, 0.1, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0]]] 

CodePudding user response:

IIUC, here is one way to do it:

Assuming your arr is actually a list or a tuple:

arr = (
    np.random.random((2, 2, 2)),
    np.random.random((4, 4, 4)),
    np.random.random((2, 2, 2)),
)

# new shape: max length in each dimension:
shape = np.c_[[x.shape for x in arr]].max(0)

>>> shape
array([4, 4, 4])

# pad all arrays
new = [np.pad(x, np.c_[[0]*len(shape), shape - x.shape]) for x in arr]

>>> new[0].shape
(4, 4, 4)

>>> new[0]
array([[[0.5488135 , 0.71518937, 0.        , 0.        ],
        [0.60276338, 0.54488318, 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        ]],

       [[0.4236548 , 0.64589411, 0.        , 0.        ],
        [0.43758721, 0.891773  , 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.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        ]]])

CodePudding user response:

UPDATE:

Don't even need to use pad then:

def pad_3d(arr: np.ndarray, out_shape: tuple[int, int, int]) -> np.ndarray:
    x, y, z = arr.shape
    output = np.zeros(out_shape, dtype=arr.dtype)
    output[:x, :y, :z] = arr
    return output


test_arr = np.array(
    [[[0.1, 0.4],
      [0.3, 0.7]],
     [[0.5, 0.2],
      [0.8, 0.1]]]
)
desired_shape = (2, 3, 4)

expected_output = np.array(
    [[[0.1, 0.4, 0.0, 0.0],
      [0.3, 0.7, 0.0, 0.0],
      [0.0, 0.0, 0.0, 0.0]],
     [[0.5, 0.2, 0.0, 0.0],
      [0.8, 0.1, 0.0, 0.0],
      [0.0, 0.0, 0.0, 0.0]]]
)

assert np.all(expected_output == pad_3d(test_arr, desired_shape))  # True

Original answer:

It's not entirely clear how you want to fill the resulting arrays with zeros around your data. Only on one side along each axis? Or do you want to essentially "center" your original data amidst the zeros?

Either way, I see no way around creating new arrays. The pad function does what you want, I think. Here is a simplified example for one array, where I "pad around" the data:

import numpy as np

a = np.arange(2*2*2).reshape((2, 2, 2))
x = np.pad(a, 0)

If you want to pad on one side with zeros:

x = np.pad(a, (0, 2))

Assuming your arrays are always cubic, i.e. of the shape (n, n, n), you can generalize like this:

def pad_with_zeros(arr, target_size):
    return np.pad(arr, (0, target_size - arr.shape[0]))
  • Related