Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy - resize array filling with 0

I have the following numpy array:

a = np.array([[1.1,0.8,0.5,0,0],[1,0.85,0.5,0,0],[1,0.8,0.5,1,0]])

with shape = (3,5).

I would like to reshape and resize it to a new array with shape = (3,8), filling the new values in each row with 0. So far I tried the following approach:

b = np.resize(a,(3,8))

But it returns:

[[ 1.1   0.8   0.5   0.    0.    1.    0.85  0.5 ]
 [ 0.    0.    1.    0.8   0.5   1.    0.    1.1 ]
 [ 0.8   0.5   0.    0.    1.    0.85  0.5   0.  ]]

instead of the expected (for me):

[[ 1.1   0.8   0.5   0.    0.    0.    0.    0. ]
 [ 1.    0.85  0.5   0.    0.    0.    0.    0. ]
 [ 1.    0.8   0.5   1.    0.    0.    0.    0. ]]
like image 526
Fabio Lamanna Avatar asked Oct 21 '15 10:10

Fabio Lamanna


2 Answers

From the doc of np.resize():

If the new array is larger than the original array, then the new array is filled with repeated copies of a.

Zeros are not used, but actual values of a.

Instead, you could use np.hstack() and np.zeros() :

np.hstack([a, np.zeros([3, 3])])

Edit: I have not tested the speed, so I suggest you have a look a other solutions too.

like image 73
P. Camilleri Avatar answered Oct 07 '22 00:10

P. Camilleri


Use np.lib.pad -

np.lib.pad(a, ((0,0),(0,3)), 'constant', constant_values=(0))

Sample run -

In [156]: a
Out[156]: 
array([[ 1.1 ,  0.8 ,  0.5 ,  0.  ,  0.  ],
       [ 1.  ,  0.85,  0.5 ,  0.  ,  0.  ],
       [ 1.  ,  0.8 ,  0.5 ,  1.  ,  0.  ]])

In [157]: np.lib.pad(a, ((0,0),(0,3)), 'constant', constant_values=(0))
Out[157]: 
array([[ 1.1 ,  0.8 ,  0.5 ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 1.  ,  0.85,  0.5 ,  0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 1.  ,  0.8 ,  0.5 ,  1.  ,  0.  ,  0.  ,  0.  ,  0.  ]])

Runtime tests -

This section covers runtime tests for the approaches posted thus far for the size listed in the question and scaling it up by 100x. Here are the timing test results -

In [212]: def init_based(a,N):
     ...:   b = np.zeros((a.shape[0], a.shape[1]+N))
     ...:   b[:, :a.shape[1]] = a
     ...:   return b
     ...: 

In [213]: a = np.random.rand(3,5)

In [214]: N = 3

In [215]: %timeit np.lib.pad(a, ((0,0),(0,N)), 'constant', constant_values=(0))
     ...: %timeit np.hstack([a, np.zeros([a.shape[0], N])])
     ...: %timeit np.concatenate((a,np.zeros((a.shape[0],N))), axis=1)
     ...: %timeit init_based(a,N)
     ...: 
10000 loops, best of 3: 32.7 µs per loop
100000 loops, best of 3: 11.2 µs per loop
100000 loops, best of 3: 4.49 µs per loop
100000 loops, best of 3: 5.67 µs per loop

In [216]: a = np.random.rand(300,500)

In [217]: N = 300

In [218]: %timeit np.lib.pad(a, ((0,0),(0,N)), 'constant', constant_values=(0))
     ...: %timeit np.hstack([a, np.zeros([a.shape[0], N])])
     ...: %timeit np.concatenate((a,np.zeros((a.shape[0],N))), axis=1)
     ...: %timeit init_based(a,N)
     ...: 
100 loops, best of 3: 2.99 ms per loop
1000 loops, best of 3: 1.72 ms per loop
1000 loops, best of 3: 1.71 ms per loop
1000 loops, best of 3: 1.72 ms per loop
like image 19
Divakar Avatar answered Oct 07 '22 02:10

Divakar