Kai
Kai

Reputation: 234

Filtering through numpy arrays by one row's information

I am asking for help on filtering through numpy arrays. I currently have a numpy array which contains the following information:

[[x1_1, x1_2, ..., x1_n], [x2_1, x2_2, ..., x2_n], [y1, y2, ..., yn]

ie. the array is essentially a dataset where x1, x2 are features (coordinates), and y is the output (value). Each data point has an appropriate x1, x2, and y, so for example, the info corresponding to data point i is x1_i, x2_i, and yi.

Now, I want to extract all the data points by filtering through y, meaning I want to know all the data points in which y > some value. In my case, I want the info (still with the same numpy structure) for all cases where y > 0. I don't really know how to do that -- I've been playing around with boolean indexing such as d[0:2,y>0] or d[d[2]>0], but haven't gotten anywhere.

A clarifying example:

Given the dataset:

d = [[0.1, 0.2, 0.3], [-0.1,-0.2,-0.3], [1,1,-1]]

I pull all points or instances where y > 0, ie. d[2] > 0, and it should return the values:

[[0.1, 0.2],[-0.1,-0.2],[1,1]]

Any advice or help would be appreciated.

Upvotes: 2

Views: 880

Answers (1)

jezrael
jezrael

Reputation: 862406

You can use:

import numpy as np

d = np.array([[0.1, 0.2, 0.3], [-0.1,-0.2,-0.3], [1,1,-1]])
print (d)
[[ 0.1  0.2  0.3]
 [-0.1 -0.2 -0.3]
 [ 1.   1.  -1. ]]

#select last row by d[-1] 
print (d[-1]>0)
[ True  True False]

print (d[:,d[-1]>0])
[[ 0.1  0.2]
 [-0.1 -0.2]
 [ 1.   1. ]]

Upvotes: 3

Related Questions