I want to make an 4 dimensional array of zeros in python. I know how to do this for a square array but I want the lists to have different lengths.
Right now I use this:
numpy.zeros((200,)*4)
Which gives them all length 200 but I would like to have lengths 200,20,100,20
because now I have a lot of zeros in my array that I don't use
X = zeros( sz1,...,szN ) returns an sz1 -by-... -by- szN array of zeros where sz1,...,szN indicate the size of each dimension. For example, zeros(2,3) returns a 2-by-3 matrix.
You can use numpy. pad , which pads default 0 to both ends of the array while in constant mode, specify the pad_width = (0, N) will pad N zeros to the right and nothing to the left: N = 4 np. pad(x, (0, N), 'constant') #array([ 1., 2., 1., 2., 7., 9., 1., 1., 3., 4., 10., # 0., 0., 0., 0.])
You can use np.full
:
>>> np.full((200,20,10,20), 0)
numpy.full
Return a new array of given shape and type, filled with fill_value.
Example :
>>> np.full((1,3,2,4), 0)
array([[[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With