bundles / scipy 1.17.1 / scipy / ndimage / _measurements / value_indices
function
scipy.ndimage._measurements:value_indices
Signature
def value_indices ( arr , * , ignore_value = None ) Summary
Find indices of each distinct value in given array.
Parameters
arr: ndarray of intsArray containing integer values.
ignore_value: int, optionalThis value will be ignored in searching the
arrarray. If not given, all values found will be included in output. Default is None.
Returns
indices: dictionaryA Python dictionary of array indices for each distinct value. The dictionary is keyed by the distinct values, the entries are array index tuples covering all occurrences of the value within the array.
This dictionary can occupy significant memory, usually several times the size of the input array.
Notes
For a small array with few distinct values, one might use numpy.unique() to find all possible values, and (arr == val) to locate each value within that array. However, for large arrays, with many distinct values, this can become extremely inefficient, as locating each value would require a new search through the entire array. Using this function, there is essentially one search, with the indices saved for all distinct values.
This is useful when matching a categorical image (e.g. a segmentation or classification) to an associated image of other data, allowing any per-class statistic(s) to then be calculated. Provides a more flexible alternative to functions like scipy.ndimage.mean() and scipy.ndimage.variance().
Some other closely related functionality, with different strengths and weaknesses, can also be found in scipy.stats.binned_statistic() and the scikit-image function skimage.measure.regionprops().
Note for IDL users: this provides functionality equivalent to IDL's REVERSE_INDICES option (as per the IDL documentation for the HISTOGRAM function).
Array API Standard Support
value_indices has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
import numpy as np from scipy import ndimage a = np.zeros((6, 6), dtype=int) a[2:4, 2:4] = 1 a[4, 4] = 1 a[:2, :3] = 2 a[0, 5] = 3 a val_indices = ndimage.value_indices(a)✓
val_indices.keys()
✓ndx1 = val_indices[1] ndx1✓
a[ndx1]
✓val_indices = ndimage.value_indices(a, ignore_value=0) val_indices.keys()✓
See also
- extrema
- label
- maximum
- mean
- median
- minimum_position
- numpy.unique
- numpy.where
- standard_deviation
- sum
- variance
Aliases
-
scipy.ndimage.value_indices