Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

4 dimensional array of zeros in python

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

like image 305
Steven Avatar asked Apr 28 '15 13:04

Steven


People also ask

How do you create an array of zeros?

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.

How do you add 5 zeros in an NumPy array?

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.])


1 Answers

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.]]]])
like image 98
Mazdak Avatar answered Sep 27 '22 18:09

Mazdak