68

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.

3 Answers 3

119

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.

1
  • 2
    A simpler equivalent for column_stack is np.vstack(([1,2,3],[4,5,6])).T which only differs by the transpose, as in @SethMMorton answer.
    – divenex
    Jul 19, 2021 at 14:11
24

hstack stacks horizontally, vstack stacks vertically:

The problem with hstack is that when you append a column you need convert it from 1d-array to a 2d-column first, because 1d array is normally interpreted as a vector-row in 2d context in numpy:

a = np.ones(2)          # 2d, shape = (2, 2)
b = np.array([0, 0])    # 1d, shape = (2,)

hstack((a, b)) -> dimensions mismatch error

So either hstack((a, b[:, None])) or column_stack((a, b)):

where None serves as a shortcut for np.newaxis.

If you're stacking two vectors, you've got three options:

As for the (undocumented) row_stack, it is just a synonym of vstack, as 1d array is ready to serve as a matrix row without extra work.

The case of 3D and above proved to be too huge to fit in the answer, so I've included it in the article called Numpy Illustrated.

17

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.

8
  • It is rather a convenience function for hstack, not for vstack. Dec 7, 2020 at 2:59
  • @AntonyHatchkins Ah, I see. Numpy has changed their documentation in the 7 years since I posted this answer to say that column_stack is more like hstack than vstack. Interesting. Well, you can always edit this answer to improve it, but I'm guessing based on the downvote you think it should just be deleted? Dec 7, 2020 at 3:28
  • The question was: what's the difference between vstack and column_stack. The proper answer would be "the difference is that they stack along different axes". The approved answer does not say it explicitly (which gives some room for improvement), but provides examples, which is good enough. Your answer is that one function is the wrapper of the other, which is not the case. The documentation has changed, but the behaviour didn't (in particular, both of your quotes hold true). I think this answer should be improved. Or deleted, maybe. Dec 7, 2020 at 6:34
  • @AntonyHatchkins Sounds like you should post a new answer since neither existing ones are satisfactory. Dec 7, 2020 at 6:39
  • 1
    I've written a huge article about this (and numpy in general) on medium, thought you might want to have a look: Numpy Illustrated Dec 27, 2020 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.