-1

I have a 2d numpy array which varies in size from one value to other.

How can I generalize the size by filling in zeros if the input value has less dimension than required?

Exmaple:

Input list with numpy array dimension:
[(40, 173),
(40, 14),
(40, 56),
(40, 173)]

And I want to have all the arrays to be (40, 173), where if it is has lesser size, than filling rest with zeros.

Deep
  • 616
  • 1
  • 9
  • 17

2 Answers2

0

This is called zero-padding. Assuming you want to pad an array A:

np.pad(A, ((0, rows), (0, cols)), "constant")

You can set:

  • rows = 40 - A.shape[0]
  • cols = 173 - A.shape[1]

More info: Zero pad numpy array

wjakobw
  • 525
  • 1
  • 4
  • 11
0

Assigning to a zeros array:

In [160]: res = np.zeros((40,173),int)                                                               
In [161]: res[0:20, 0:30] = np.ones((20,30),int)  
hpaulj
  • 221,503
  • 14
  • 230
  • 353