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?
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.
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.
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)
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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With