Good ways to “expand” a numpy ndarray?

后端 未结 6 658
一个人的身影
一个人的身影 2020-12-04 16:43

Are there good ways to \"expand\" a numpy ndarray? Say I have an ndarray like this:

[[1 2]
 [3 4]]

And I want each row to contains more ele

6条回答
  •  离开以前
    2020-12-04 17:03

    there are also similar methods like np.vstack, np.hstack, np.dstack. I like these over np.concatente as it makes it clear what dimension is being "expanded".

    temp = np.array([[1, 2], [3, 4]])
    np.hstack((temp, np.zeros((2,3))))
    

    it's easy to remember becase numpy's first axis is vertical so vstack expands the first axis and 2nd axis is horizontal so hstack.

提交回复
热议问题