Will Will - 3 months ago 7
Python Question

What is this called: creating numpy matrix from several vectors

I have 4 individual numpy vectors with the shape (10, ), and I want to join them together to form a matrix (10, 4).

What is this transformation called?

Answer

It's called stack.

> import numpy as np
> a = np.arange(10)
> b = np.stack((a, a, a, a), axis=1)
> np.shape(b)
(10, 4)
> b
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4],
       [5, 5, 5, 5],
       [6, 6, 6, 6],
       [7, 7, 7, 7],
       [8, 8, 8, 8],
       [9, 9, 9, 9]])

You could also (in this case) use e.g. np.array([a, a, a, a]).T.

Comments