carl
carl

Reputation: 4436

downsample big numpy.ndarray on if condition in python

I have a large numpy.ndarray and I need to downsample this array based on the value of one column. My solution works, but is very slow

data_table = data_table[[i for i in range(0, len(data_table)) if data_table[i][7] > 0.2 and data_table[i][7] < 0.75]]

does anybody know what the fastest way is to do this?

Upvotes: 1

Views: 56

Answers (1)

Divakar
Divakar

Reputation: 221754

Use column-slicing to select relevant columns and compare those against the thresholds in a vectorized manner to give us a mask of valid rows and then index into the rows for the rows filtered output -

out = data_table[(data_table[:,7] > 0.2) & (data_table[:,7] < 0.75)]

Upvotes: 2

Related Questions