Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy vstack vs. column_stack

Tags:

python

numpy

What exactly is the difference between numpy vstack and column_stack. Reading through the documentation, it looks as if column_stack is an implementation of vstack for 1D arrays. Is it a more efficient implementation? Otherwise, I cannot find a reason for just having vstack.

like image 296
ZenBalance Avatar asked May 09 '13 23:05

ZenBalance


People also ask

What is difference between Vstack and Hstack function?

HStack - which arranges its children(i.e. subviews) in a horizontal line, next to each other. VStack - which arranges its children in a vertical line, i.e above and below each other.

What does NP Column_stack do?

column_stack() function is used to stack 1-D arrays as columns into a 2-D array.It takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack function.

What is Hstack and Vstack in NumPy?

NumPy hstack and NumPy vstack are similar in that they both combine NumPy arrays together. The major difference is that np. hstack combines NumPy arrays horizontally and np. vstack combines arrays vertically.

What is the use of Vstack?

VSTACK returns the array formed by appending each of the array arguments in a row-wise fashion. The resulting array will be the following dimensions: Rows: the combined count of all the rows from each of the array arguments. Columns: The maximum of the column count from each of the array arguments.


2 Answers

I think the following code illustrates the difference nicely:

>>> np.vstack(([1,2,3],[4,5,6])) array([[1, 2, 3],        [4, 5, 6]]) >>> np.column_stack(([1,2,3],[4,5,6])) array([[1, 4],        [2, 5],        [3, 6]]) >>> np.hstack(([1,2,3],[4,5,6])) array([1, 2, 3, 4, 5, 6]) 

I've included hstack for comparison as well. Notice how column_stack stacks along the second dimension whereas vstack stacks along the first dimension. The equivalent to column_stack is the following hstack command:

>>> np.hstack(([[1],[2],[3]],[[4],[5],[6]])) array([[1, 4],        [2, 5],        [3, 6]]) 

I hope we can agree that column_stack is more convenient.

like image 77
mgilson Avatar answered Sep 23 '22 17:09

mgilson


In the Notes section to column_stack, it points out this:

This function is equivalent to np.vstack(tup).T.

There are many functions in numpy that are convenient wrappers of other functions. For example, the Notes section of vstack says:

Equivalent to np.concatenate(tup, axis=0) if tup contains arrays that are at least 2-dimensional.

It looks like column_stack is just a convenience function for vstack.

like image 20
SethMMorton Avatar answered Sep 23 '22 17:09

SethMMorton