0

I have a 2D numpy array of shape (144,X) where X can be any positive integer. I want to pad my numpy array with zeros such that the final shape of my array is (208,5000). How can I achieve that?

1
  • You need to do some research before asking on Stack Overflow. Commented Jul 28, 2021 at 15:56

1 Answer 1

2

It's also possible to pad a 2D numpy arrays by passing a tuple of tuples as padding width, which takes the format of ((top, bottom), (left, right))

example:

import numpy as np
A=np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(A.shape)

Output:

(2, 4)

If I want to make one array shape((5, 9) :

B = np.pad(A, (((5-2),0),(7-2,0)), 'constant')
C = np.pad(A, (((5-3),1),(7-4,2)), 'constant')
print(B.shape)  #--->shape((5, 9)
print(C.shape)  #--->shape((5, 9)

then:

#((top, bottom), (left, right)):
#(144,X) -->(208,5000)
#B = np.pad(A, (((208-144),0),(5000-X,0)), 'constant'): One of the possible modes
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.