InterviewSolution
| 1. |
How Do I Find The Indices Of An Array Where Some Condition Is True? |
|
Answer» The prefered idiom for doing this is to use the function numpy.NONZERO() , or the nonzero() method of an array. Given an array a, the condition a > 3 RETURNS a boolean array and since False is INTERPRETED as 0 in Python and NumPy, NP.nonzero(a > 3)yields the indices of a where the condition is true. >>> IMPORT numpy as np The prefered idiom for doing this is to use the function numpy.nonzero() , or the nonzero() method of an array. Given an array a, the condition a > 3 returns a boolean array and since False is interpreted as 0 in Python and NumPy, np.nonzero(a > 3)yields the indices of a where the condition is true. >>> import numpy as np |
|