How to add an extra column to a NumPy array

前端 未结 17 2076
一个人的身影
一个人的身影 2020-11-22 14:37

Let’s say I have a NumPy array, a:

a = np.array([
    [1, 2, 3],
    [2, 3, 4]
    ])

And I would like to add a column of ze

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 15:19

    In my case, I had to add a column of ones to a NumPy array

    X = array([ 6.1101, 5.5277, ... ])
    X.shape => (97,)
    X = np.concatenate((np.ones((m,1), dtype=np.int), X.reshape(m,1)), axis=1)
    

    After X.shape => (97, 2)

    array([[ 1. , 6.1101],
           [ 1. , 5.5277],
    ...
    

提交回复
热议问题