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?
Asked
Active
Viewed 1,156 times
0
-
You need to [do some research](//meta.stackoverflow.com/a/261593/843953) before asking on Stack Overflow. – Pranav Hosangadi Jul 28 '21 at 15:56
1 Answers
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

Salio
- 1,058
- 10
- 21