numpy vstack vs. column_stack

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)