How do you extract a column from a multi-dimensional array?

前端 未结 20 2892
感情败类
感情败类 2020-12-02 03:56

Does anybody know how to extract a column from a multi-dimensional array in Python?

20条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 04:31

    I think you want to extract a column from an array such as an array below

    import numpy as np
    A = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
    

    Now if you want to get the third column in the format

    D=array[[3],
    [7],
    [11]]
    

    Then you need to first make the array a matrix

    B=np.asmatrix(A)
    C=B[:,2]
    D=asarray(C)
    

    And now you can do element wise calculations much like you would do in excel.

提交回复
热议问题