selecting rows in numpy ndarray based on the value of two columns

后端 未结 2 372
别那么骄傲
别那么骄傲 2021-01-31 10:46

I have a big np.ndarray (3600000,3), the HUE, the VALUE, and an associated CLASS number. For each pairs of HUE a

相关标签:
2条回答
  • 2021-01-31 10:53

    Try this code:

     x[x[:, 2] == class_number[:, :2]
    

    where x is np.ndarray

     x[:, 2] == class_number
    

    contains true/false that means whether the last is class_number or not.

    You need to take a look at: Boolean indexing in http://wiki.scipy.org/Cookbook/Indexing

    Moved from comment.

    0 讨论(0)
  • 2021-01-31 10:56

    I assume your array looks like:

           |(HUE)(VALUE)(CLASS)
    row/col|   0     1     2
    -------+-----------------
    0      |   0     1     2
    1      |   3     4     5
    2      |   6     7     8
    .      |   .     .     .
    .      |   .     .     .
    3599999|   .     .     .
    

    And here is the sample code. For simplicity I changed the size 3600000 to 5.

    a = np.array(xrange(5 * 3))
    a.shape = (5, 3)
    

    Now array a look like this:

    array([[ 0,  1,  2],
           [ 3,  4,  5],
           [ 6,  7,  8],
           [ 9, 10, 11],
           [12, 13, 14]])
    

    If you want row with HUE=9, do like this:

    a[np.where(a[:,0] == 9)]
    #array([[ 9, 10, 11]])
    

    If you want row with VALUE=4, do like this:

    a[np.where(a[:,1] == 4)]
    #array([[3, 4, 5]])
    

    If you want row with HUE=0 and VALUE=1, do like this:

    a[np.where((a[:,0] == 0) * (a[:,1] == 1))]
    #array([[0, 1, 2]])
    
    0 讨论(0)
提交回复
热议问题