3

I wanna find the indices of the rows that all have entries smaller than 1e-6 or where the number of nonzero values is less than 3. Something like this would be nice:

import numpy as np

prob = np.random.rand(15, 500)
all_zero = np.where(prob.max(1) < 1e-6 | np.nonzero(prob, axis=1) < 3) 
3
  • 1
    So what is your question? You just need to sum the nonzero, i.e. np.nonzero(prob).sum(axis=1). Commented Sep 11, 2019 at 15:45
  • 1
    I don't understand the question either. Commented Sep 11, 2019 at 15:47
  • Oops you’re right. I didn’t think of adding up the number of nonzeros. Commented Sep 11, 2019 at 16:52

3 Answers 3

3

I tried to measure the execution times of the solutions proposed so far:
Benchmark data:

prob = np.random.rand(10000, 500)

@Massifox' solution with list:

%%timeit
[i for i, val in enumerate(prob>1e-6)if val.sum() < 3]
# 39.5 ms ± 1.4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

@Massifox' solution only numpy:

%%timeit
np.where(np.sum(prob>1e-6, axis=1) < 3)
# 9.92 ms ± 199 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

@a_guest's solution:

%%timeit
all_zero = np.logical_or(prob.max(axis=1) < 1e-6, np.sum(prob != 0, axis=1) < 3)
np.where(all_zero)
# 13.9 ms ± 150 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

The most efficient solution seems to be the second one.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use np.logical_or and np.sum the non-zero values to check which row has fewer than 3 non-zero elements:

all_zero = np.logical_or(prob.max(axis=1) < 1e-6, np.sum(prob != 0, axis=1) < 3)

Comments

1

This code returns the list of index of rows with less than 3 values ​​other than 0 (less than 1e-6):

[i for i, val in enumerate(prob>1e-6) if val.sum()<3]

or using only numpy functions:

np.where(np.sum(prob>1e-6, axis=1)<3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.