How to split a numpy array based on a column?

后端 未结 3 1517
小鲜肉
小鲜肉 2021-01-24 18:45

I have an array of the form :

[[ 1. ,    2.,     3.,     1.,     3.,     3.,     4.   ],
 [ 1.3,    2.3,    3.3,    3.,     3.3,    3.3,    4.3  ],
 [ 1.2,    2.         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-24 19:12

    Look at the docs for splitting an array into multiple sub-arrays.

    numpy.hsplit(ary, indices_or_sections)

    Split an array into multiple sub-arrays horizontally (column-wise).

    So say you have a 4x4 array A:

    array([[  0.,   1.,   2.,   3.],
       [  4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.],
       [ 12.,  13.,  14.,  15.]])
    
    split = numpy.hsplit(A,4) = 
    
    [array([[  0.],
       [  4.],
       [  8.],
       [ 12.]]), array([[  1.],
       [  5.],
       [  9.],
       [ 13.]]), array([[  2.],
       [  6.],
       [ 10.],
       [ 14.]]), array([[  3.],
       [  7.],
       [ 11.],
       [ 15.]])]
    

提交回复
热议问题