How to add an extra column to a NumPy array

前端 未结 17 1907
一个人的身影
一个人的身影 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:11

    Assuming M is a (100,3) ndarray and y is a (100,) ndarray append can be used as follows:

    M=numpy.append(M,y[:,None],1)
    

    The trick is to use

    y[:, None]
    

    This converts y to a (100, 1) 2D array.

    M.shape
    

    now gives

    (100, 4)
    

提交回复
热议问题