Reduction Functions¶
Contrarily to lazy functions, reduction functions are evaluated eagerly, and the result is always a NumPy array (although this can be converted internally into an NDArray if you pass any blosc2.empty()
arguments in kwargs
).
Reduction operations can be used with any of NDArray, C2Array, NDField and LazyExpr. Again, although these can be part of a LazyExpr, you must be aware that they are not lazy, but will be evaluated eagerly during the construction of a LazyExpr instance (this might change in the future).
|
Test whether all array elements along a given axis evaluate to True. |
|
Test whether any array element along a given axis evaluates to True. |
|
Return the sum of array elements over a given axis. |
|
Return the product of array elements over a given axis. |
|
Return the arithmetic mean along the specified axis. |
|
Return the standard deviation along the specified axis. |
|
Return the variance along the specified axis. |
|
Return the minimum along a given axis. |
|
Return the maximum along a given axis. |
- blosc2.all(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | bool [source]¶
Test whether all array elements along a given axis evaluate to True.
The parameters are documented in the
min
.- Returns:
all_along_axis – The result of the evaluation along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> data = np.array([True, True, False, True, True, True]) >>> ndarray = blosc2.asarray(data) >>> # Test if all elements are True along the default axis (flattened array) >>> result_flat = blosc2.all(ndarray) >>> print("All elements are True (flattened):", result_flat) All elements are True (flattened): False
- blosc2.any(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | bool [source]¶
Test whether any array element along a given axis evaluates to True.
The parameters are documented in the
min
.- Returns:
any_along_axis – The result of the evaluation along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import blosc2 >>> import numpy as np >>> data = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]]) >>> # Convert the NumPy array to a Blosc2 NDArray >>> ndarray = blosc2.asarray(data) >>> print("NDArray data:", ndarray[:]) NDArray data: [[1 0 0] [0 1 0] [0 0 0]] >>> any_along_axis_0 = blosc2.any(ndarray, axis=0) >>> print("Any along axis 0:", any_along_axis_0) Any along axis 0: [True True False] >>> any_flattened = blosc2.any(ndarray) >>> print("Any in the flattened array:", any_flattened) Any in the flattened array: True
- blosc2.sum(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, dtype: dtype | str = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | complex | bool [source]¶
Return the sum of array elements over a given axis.
- Parameters:
ndarr¶ (NDArray or NDField or C2Array or LazyExpr) – The input array or expression.
axis¶ (int or tuple of ints, optional) – Axis or axes along which a sum is performed. By default, axis=None, sums all the elements of the input array. If axis is negative, it counts from the last to the first axis.
dtype¶ (np.dtype or list str, optional) – The type of the returned array and of the accumulator in which the elements are summed. The dtype of
ndarr
is used by default unless it has an integer dtype of less precision than the default platform integer.keepdims¶ (bool, optional) – If set to True, the reduced axes are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
kwargs¶ (dict, optional) – Additional keyword arguments supported by the
empty()
constructor.
- Returns:
sum_along_axis – The sum of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> # Example array >>> array = np.array([[1, 2, 3], [4, 5, 6]]) >>> nd_array = blosc2.asarray(array) >>> # Sum all elements in the array (axis=None) >>> total_sum = blosc2.sum(nd_array) >>> print("Sum of all elements:", total_sum) 21 >>> # Sum along axis 0 (columns) >>> sum_axis_0 = blosc2.sum(nd_array, axis=0) >>> print("Sum along axis 0 (columns):", sum_axis_0) Sum along axis 0 (columns): [5 7 9]
- blosc2.prod(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, dtype: dtype | str = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | complex | bool [source]¶
Return the product of array elements over a given axis.
The parameters are documented in the
sum
.- Returns:
product_along_axis – The product of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> # Create an instance of NDArray with some data >>> array = np.array([[11, 22, 33], [4, 15, 36]]) >>> nd_array = blosc2.asarray(array) >>> # Compute the product of all elements in the array >>> prod_all = blosc2.prod(nd_array) >>> print("Product of all elements in the array:", prod_all) Product of all elements in the array: 17249760 >>> # Compute the product along axis 1 (rows) >>> prod_axis1 = blosc2.prod(nd_array, axis=1) >>> print("Product along axis 1:", prod_axis1) Product along axis 1: [7986 2160]
- blosc2.mean(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, dtype: dtype | str = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | complex | bool [source]¶
Return the arithmetic mean along the specified axis.
The parameters are documented in the
sum
.- Returns:
mean_along_axis – The mean of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> # Example array >>> array = np.array([[1, 2, 3], [4, 5, 6]] >>> nd_array = blosc2.asarray(array) >>> # Compute the mean of all elements in the array (axis=None) >>> overall_mean = blosc2.mean(nd_array) >>> print("Mean of all elements:", overall_mean) Mean of all elements: 3.5
- blosc2.std(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, dtype: dtype | str = None, ddof: int = 0, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | bool [source]¶
Return the standard deviation along the specified axis.
- Parameters:
ndarr¶ (NDArray or NDField or C2Array or LazyExpr) – The input array or expression.
axis¶ (int or tuple of ints, optional) – Axis or axes along which the standard deviation is computed. By default, axis=None computes the standard deviation of the flattened array.
dtype¶ (np.dtype or list str, optional) – Type to use in computing the standard deviation. For integer inputs, the default is float32; for floating point inputs, it is the same as the input dtype.
ddof¶ (int, optional) – Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. By default, ddof is zero.
keepdims¶ (bool, optional) – If set to True, the reduced axes are left in the result as dimensions with size one. This ensures that the result will broadcast correctly against the input array.
kwargs¶ (dict, optional) – Additional keyword arguments that are supported by the
empty()
constructor.
- Returns:
std_along_axis – The standard deviation of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> # Create an instance of NDArray with some data >>> array = np.array([[1, 2, 3], [4, 5, 6]]) >>> nd_array = blosc2.asarray(array) >>> # Compute the standard deviation of the entire array >>> std_all = blosc2.std(nd_array) >>> print("Standard deviation of the entire array:", std_all) Standard deviation of the entire array: 1.707825127659933 >>> # Compute the standard deviation along axis 0 (columns) >>> std_axis0 = blosc2.std(nd_array, axis=0) >>> print("Standard deviation along axis 0:", std_axis0) Standard deviation along axis 0: [1.5 1.5 1.5]
- blosc2.var(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, dtype: dtype | str = None, ddof: int = 0, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | bool [source]¶
Return the variance along the specified axis.
The parameters are documented in the
std
.- Returns:
var_along_axis – The variance of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> # Create an instance of NDArray with some data >>> array = np.array([[1, 2, 3], [4, 5, 6]]) >>> nd_array = blosc2.asarray(array) >>> # Compute the variance of the entire array >>> var_all = blosc2.var(nd_array) >>> print("Variance of the entire array:", var_all) Variance of the entire array: 2.9166666666666665 >>> # Compute the variance along axis 0 (columns) >>> var_axis0 = blosc2.var(nd_array, axis=0) >>> print("Variance along axis 0:", var_axis0) Variance along axis 0: [2.25 2.25 2.25]
- blosc2.min(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | complex | bool [source]¶
Return the minimum along a given axis.
- Parameters:
ndarr¶ (NDArray or NDField or C2Array or LazyExpr) – The input array or expression.
axis¶ (int or tuple of ints, optional) – Axis or axes along which to operate. By default, flattened input is used.
keepdims¶ (bool, optional) – If set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
kwargs¶ (dict, optional) – Keyword arguments that are supported by the
empty()
constructor.
- Returns:
min_along_axis – The minimum of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import numpy as np >>> import blosc2 >>> array = np.array([1, 3, 7, 8, 9, 31]) >>> nd_array = blosc2.asarray(array) >>> min_all = blosc2.min(nd_array) >>> print("Minimum of all elements in the array:", min_all) Minimum of all elements in the array: 1 >>> # Compute the minimum along axis 0 with keepdims=True >>> min_keepdims = blosc2.min(nd_array, axis=0, keepdims=True) >>> print("Minimum along axis 0 with keepdims=True:", min_keepdims) Minimum along axis 0 with keepdims=True: [1]
- blosc2.max(ndarr: NDArray | NDField | C2Array | LazyExpr, axis: int | tuple[int] | None = None, keepdims: bool = False, **kwargs: Any) ndarray | NDArray | int | float | complex | bool [source]¶
Return the maximum along a given axis.
The parameters are documented in the
min
.- Returns:
max_along_axis – The maximum of the elements along the axis.
- Return type:
np.ndarray or NDArray or scalar
References
Examples
>>> import blosc2 >>> import numpy as np >>> data = np.array([[11, 2, 36, 24, 5, 69], [73, 81, 49, 6, 73, 0]]) >>> ndarray = blosc2.asarray(data) >>> print("NDArray data:", ndarray[:]) NDArray data: [[11 2 36 24 5 69] [73 81 49 6 73 0]] >>> # Compute the maximum along axis 0 and 1 >>> max_along_axis_0 = blosc2.max(ndarray, axis=0) >>> print("Maximum along axis 0:", max_along_axis_0) Maximum along axis 0: [73 81 49 24 73 69] >>> max_along_axis_1 = blosc2.max(ndarray, axis=1) >>> print("Maximum along axis 1:", max_along_axis_1) Maximum along axis 1: [69 81] >>> max_flattened = blosc2.max(ndarray) >>> print("Maximum of the flattened array:", max_flattened) Maximum of the flattened array: 81