findinds#
- findinds(arr=None, val=None, *args, eps=1e-06, first=False, last=False, ind=None, die=True, **kwargs)[source]#
Find matches even if two things aren’t eactly equal (e.g. floats vs. ints).
If one argument, find nonzero values. With two arguments, check for equality using eps (by default 1e-6, to handle single-precision floating point). Returns a tuple of arrays if val1 is multidimensional, else returns an array. Similar to calling
np.nonzero(np.isclose(arr, val))[0]
.- Parameters:
arr (array) – the array to find values in
val (float) – if provided, the value to match
args (list) – if provided, additional boolean arrays
eps (float) – the precision for matching (default 1e-6, equivalent to
numpy.isclose()
’s atol)first (bool) – whether to return the first matching value (equivalent to ind=0)
last (bool) – whether to return the last matching value (equivalent to ind=-1)
ind (int) – index of match to retrieve
die (bool) – whether to raise an exception if first or last is true and no matches were found
kwargs (dict) – passed to
numpy.isclose()
Examples:
data = np.random.rand(10) sc.findinds(data<0.5) # Standard usage; returns e.g. array([2, 4, 5, 9]) sc.findinds(data>0.1, data<0.5) # Multiple arguments sc.findinds([2,3,6,3], 3) # Returs array([1,3]) sc.findinds([2,3,6,3], 3, first=True) # Returns 1
New in version 1.2.3: “die” argumentNew in version 2.0.0: fix string matching; allow multiple argumentsNew in version 3.0.0: multidimensional arrays now return a list of tuples