Muatik
Muatik

Reputation: 4171

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?

Upvotes: 7

Views: 17914

Answers (4)

István Orosz
István Orosz

Reputation: 1

I've just learnt about the np.where function and applied on this task. This solution preserves the type of the original data.

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]])
row_ndx,_ = np.where(a[:,2]>0)
a[row_ndx]

or a oneliner:

a[np.where(a[:,2]>0)[0]]

Upvotes: 0

armatita
armatita

Reputation: 13465

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]]

Upvotes: 1

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58895

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)    

Upvotes: 14

Ilja
Ilja

Reputation: 2114

Somehow, the np.matrix behaves strange. My solution which is valid for an array does not work with a matrix, since the rows of the matrix are 1-row-matrices. Why do you want to use explicitly matrix?

Well, this works:

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")

data = np.array(data)
print data[data[:, 2] > 0]

I also tried to use filter(lambda x: x[0, 2] > 0, data), which works, but returns a list of 1x3-matrices, which I cannot convert back to a good shape.

Upvotes: 0

Related Questions