Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good ways to "expand" a numpy ndarray?

Are there good ways to "expand" a numpy ndarray? Say I have an ndarray like this:

[[1 2]  [3 4]] 

And I want each row to contains more elements by filling zeros:

[[1 2 0 0 0]  [3 4 0 0 0]] 

I know there must be some brute-force ways to do so (say construct a bigger array with zeros then copy elements from old smaller arrays), just wondering are there pythonic ways to do so. Tried numpy.reshape but didn't work:

import numpy as np a = np.array([[1, 2], [3, 4]]) np.reshape(a, (2, 5)) 

Numpy complains that: ValueError: total size of new array must be unchanged

like image 818
clwen Avatar asked Oct 01 '12 05:10

clwen


People also ask

How do I expand an array in NumPy?

To expand the shape of an array, use the numpy. expand_dims() method. Insert a new axis that will appear at the axis position in the expanded array shape. The function returns the View of the input array with the number of dimensions increased.

Is Ndarray in NumPy better than a list?

Numpy data structures perform better in: Size - Numpy data structures take up less space. Performance - they have a need for speed and are faster than lists. Functionality - SciPy and NumPy have optimized functions such as linear algebra operations built in.

Is a NumPy Ndarray is faster than a built in list?

Even for the delete operation, the Numpy array is faster. As the array size increase, Numpy gets around 30 times faster than Python List. Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster.


2 Answers

You can use numpy.pad, as follows:

>>> import numpy as np >>> a=[[1,2],[3,4]] >>> np.pad(a, ((0,0),(0,3)), mode='constant', constant_values=0) array([[1, 2, 0, 0, 0],        [3, 4, 0, 0, 0]]) 

Here np.pad says, "Take the array a and add 0 rows above it, 0 rows below it, 0 columns to the left of it, and 3 columns to the right of it. Fill these columns with a constant specified by constant_values".

like image 53
Richard Avatar answered Oct 06 '22 12:10

Richard


There are the index tricks r_ and c_.

>>> import numpy as np >>> a = np.array([[1, 2], [3, 4]]) >>> z = np.zeros((2, 3), dtype=a.dtype) >>> np.c_[a, z] array([[1, 2, 0, 0, 0],        [3, 4, 0, 0, 0]]) 

If this is performance critical code, you might prefer to use the equivalent np.concatenate rather than the index tricks.

>>> np.concatenate((a,z), axis=1) array([[1, 2, 0, 0, 0],        [3, 4, 0, 0, 0]]) 

There are also np.resize and np.ndarray.resize, but they have some limitations (due to the way numpy lays out data in memory) so read the docstring on those ones. You will probably find that simply concatenating is better.

By the way, when I've needed to do this I usually just do it the basic way you've already mentioned (create an array of zeros and assign the smaller array inside it), I don't see anything wrong with that!

like image 20
wim Avatar answered Oct 06 '22 11:10

wim