Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stack vectors of different lengths in NumPy?

Tags:

python

numpy

How do I stack column-wise n vectors of shape (x,) where x could be any number?

For example,

from numpy import * a = ones((3,)) b = ones((2,))  c = vstack((a,b)) # <-- gives an error c = vstack((a[:,newaxis],b[:,newaxis])) #<-- also gives an error 

hstack works fine but concatenates along the wrong dimension.

like image 913
mac389 Avatar asked Feb 16 '13 23:02

mac389


People also ask

How do I stack in NumPy?

NumPy: stack() functionThe stack() function is used to join a sequence of arrays along a new axis. The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

How do you concatenate two arrays of different dimensions in Python?

You can either reshape it array_2. reshape(-1,1) , or add a new axis array_2[:,np. newaxis] to make it 2 dimensional before concatenation.

How do I add NumPy vectors together?

Use the numpy. add() Function to Perform Vector Addition in NumPy. The add() function from the numpy module can be used to add two arrays. It performs addition over arrays that have the same size with elements at every corresponding position getting summed up.


2 Answers

Short answer: you can't. NumPy does not support jagged arrays natively.

Long answer:

>>> a = ones((3,)) >>> b = ones((2,)) >>> c = array([a, b]) >>> c array([[ 1.  1.  1.], [ 1.  1.]], dtype=object) 

gives an array that may or may not behave as you expect. E.g. it doesn't support basic methods like sum or reshape, and you should treat this much as you'd treat the ordinary Python list [a, b] (iterate over it to perform operations instead of using vectorized idioms).

Several possible workarounds exist; the easiest is to coerce a and b to a common length, perhaps using masked arrays or NaN to signal that some indices are invalid in some rows. E.g. here's b as a masked array:

>>> ma.array(np.resize(b, a.shape[0]), mask=[False, False, True]) masked_array(data = [1.0 1.0 --],              mask = [False False  True],        fill_value = 1e+20) 

This can be stacked with a as follows:

>>> ma.vstack([a, ma.array(np.resize(b, a.shape[0]), mask=[False, False, True])]) masked_array(data =  [[1.0 1.0 1.0]  [1.0 1.0 --]],              mask =  [[False False False]  [False False  True]],        fill_value = 1e+20) 

(For some purposes, scipy.sparse may also be interesting.)

like image 72
Fred Foo Avatar answered Oct 05 '22 17:10

Fred Foo


In general, there is an ambiguity in putting together arrays of different length because alignment of data might matter. Pandas has different advanced solutions to deal with that, e.g. to merge series into dataFrames.

If you just want to populate columns starting from first element, what I usually do is build a matrix and populate columns. Of course you need to fill the empty spaces in the matrix with a null value (in this case np.nan)

a = ones((3,)) b = ones((2,)) arraylist=[a,b]  outarr=np.ones((np.max([len(ps) for ps in arraylist]),len(arraylist)))*np.nan #define empty array for i,c in enumerate(arraylist):  #populate columns     outarr[:len(c),i]=c  In [108]: outarr Out[108]:  array([[  1.,   1.],        [  1.,   1.],        [  1.,  nan]]) 
like image 22
Vincenzooo Avatar answered Oct 05 '22 19:10

Vincenzooo