Python: Add a column to numpy 2d array

后端 未结 5 1253
醉酒成梦
醉酒成梦 2021-01-02 04:44

I have a 60000 by 200 numpy array. I want to make it 60000 by 201 by adding a column of 1\'s to the right. (so every row is [prev, 1]) Concatenate with axis = 1 doesn\'t wor

5条回答
  •  清酒与你
    2021-01-02 05:25

    Using numpy index trick to append a 1D vector to a 2D array

    a = np.zeros((6,2))
    # array([[ 0.,  0.],
    #        [ 0.,  0.],
    #        [ 0.,  0.],
    #        [ 0.,  0.],
    #        [ 0.,  0.],
    #        [ 0.,  0.]])
    b = np.ones(6) # or np.ones((6,1))
    #array([1., 1., 1., 1., 1., 1.])
    np.c_[a,b]
    # array([[0., 0., 1.],
    #        [0., 0., 1.],
    #        [0., 0., 1.],
    #        [0., 0., 1.],
    #        [0., 0., 1.],
    #        [0., 0., 1.]])
    

提交回复
热议问题