Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering numpy matrix on a column

I have a numpy matrix as follows:

data = np.matrix(
      "5 3 1;"
      "4 4 1;"
      "6 4 1;"
      "8 2 1;"
      "3 5 1;"
      "1 7 1;"
      "5 4 1;"
      "0 1 0;"
      "2 0 0")
# Output:
matrix([[5, 3, 1],
    [4, 4, 1],
    [6, 4, 1],
    [8, 2, 1],
    [3, 5, 1],
    [1, 7, 1],
    [5, 4, 1],
    [0, 1, 0],
    [2, 0, 0]])

what I want is to filter the matrix on the third column whose value is 1; that is, I do not want to get the rows whose 3rd value is 0. In short, I want to extract the matrix below:

matrix([[5, 3, 1],
    [4, 4, 1],
    [6, 4, 1],
    [8, 2, 1],
    [3, 5, 1],
    [1, 7, 1],
    [5, 4, 1]])

I tried a few combinations to filter it; but none of them worked for me. For instance, the following code rules out the rows with zero, but it returns only the first column.

data[data[:,2]>0]
#Output: 
matrix([[5, 4, 6, 8, 3, 1, 5]])

Is there a way to filter this matrix without explicitly writing loop statements?

like image 563
Muatik Avatar asked Mar 20 '16 15:03

Muatik


People also ask

How to filter a NumPy matrix?

You can filter a numpy array by creating a list or an array of boolean values indicative of whether or not to keep the element in the corresponding array. This method is called boolean mask slicing.

How do you filter an element from an array in Python?

In NumPy, you filter an array using a boolean index list. A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.


2 Answers

Using np.array instead of np.matrix allows you to do a simple mask indexing like:

a = a[a[:, 2] != 0]

to convert from np.matrix to np.array you can do:

a = np.asarray(a)    
like image 150
Saullo G. P. Castro Avatar answered Sep 18 '22 12:09

Saullo G. P. Castro


Or just use:

 import numpy as np

 a = np.matrix([[5, 3, 1],
     [4, 4, 1],
     [6, 4, 1],
     [8, 2, 1],
     [3, 5, 1],
     [1, 7, 1],
     [5, 4, 1],
     [0, 1, 0],
     [2, 0, 0]])
 ind = np.squeeze(np.asarray(a[:,2]))>0

 print(a[ind,:])

, which results in:

 [[5 3 1]
  [4 4 1]
  [6 4 1]
  [8 2 1]
  [3 5 1]
  [1 7 1]
  [5 4 1]]
like image 33
armatita Avatar answered Sep 20 '22 12:09

armatita