scipy.ndimage.

maximum#

scipy.ndimage.maximum(input, labels=None, index=None)[source]#

Calculate the maximum of the values of an array over labeled regions.

Parameters:
inputarray_like

Array_like of values. For each region specified by labels, the maximal values of input over the region is computed.

labelsarray_like, optional

An array of integers marking different regions over which the maximum value of input is to be computed. labels must have the same shape as input. If labels is not specified, the maximum over the whole array is returned.

indexarray_like, optional

A list of region labels that are taken into account for computing the maxima. If index is None, the maximum over all elements where labels is non-zero is returned.

Returns:
outputa scalar or list of integers or floats based on input type.

List of maxima of input over the regions determined by labels and whose index is in index. If index or labels are not specified, a float is returned: the maximal value of input if labels is None, and the maximal value of elements where labels is greater than zero if index is None.

Notes

The function returns a Python list and not a NumPy array, use np.array to convert the list to an array.

maximum 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 Support for the array API standard for more information.

Examples

>>> import numpy as np
>>> a = np.arange(16).reshape((4,4))
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> labels = np.zeros_like(a)
>>> labels[:2,:2] = 1
>>> labels[2:, 1:3] = 2
>>> labels
array([[1, 1, 0, 0],
       [1, 1, 0, 0],
       [0, 2, 2, 0],
       [0, 2, 2, 0]])
>>> from scipy import ndimage
>>> ndimage.maximum(a)
15
>>> ndimage.maximum(a, labels=labels, index=[1,2])
[5, 14]
>>> ndimage.maximum(a, labels=labels)
14
>>> b = np.array([[1, 2, 0, 0],
...               [5, 3, 0, 4],
...               [0, 0, 0, 7],
...               [9, 3, 0, 0]])
>>> labels, labels_nb = ndimage.label(b)
>>> labels
array([[1, 1, 0, 0],
       [1, 1, 0, 2],
       [0, 0, 0, 2],
       [3, 3, 0, 0]], dtype=int32)
>>> ndimage.maximum(b, labels=labels, index=np.arange(1, labels_nb + 1))
[5, 7, 9]