diff --git a/content/numpy/concepts/ndarray/terms/max/max.md b/content/numpy/concepts/ndarray/terms/max/max.md new file mode 100644 index 00000000000..e913925c6bd --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/max/max.md @@ -0,0 +1,6203 @@ +--- +Title: 'max()' +Description: 'Returns the maximum value of elements along a specified axis or the largest element in the entire NumPy array.' +Subjects: + - 'Python' + - 'Data Science' +Tags: + - 'Arrays' + - 'Functions' + - 'NumPy' + - 'Methods' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`max()`** function in NumPy returns the maximum value of elements along a specified axis in an array. If no axis is provided, it returns a single scalar value representing the most significant element in the entire array. NumPy arrays support efficient element-wise operations, and max() quickly identifies the maximum value across dimensions or within specific rows and columns. + +When `axis=0`, the function returns a **one-dimensional array containing the maximum values from each column**. +When `axis=1`, it returns a **one-dimensional array containing the maximum values from each row**. + +## Syntax + +```pseudo +numpy.ndarray.max(axis=None, out=None, keepdims=False, initial=, where=True) +``` + +**Parameters:** + +- `axis` *(int or None)* — Axis or axes along which to operate. + - `0` **:** Operates vertically, comparing elements column by column. The result is a one-dimensional array containing the maximum values from each column. + - `1` **:** Operates horizontally, comparing elements row by row. The result is a one-dimensional array containing the maximum values from each row. + - `None` **:** Computes the maximum value over the entire array, regardless of dimensions, and returns a single scalar value representing the largest element. + +- `out` *(ndarray or None, optional)* — Output array where results will be stored. + - If an array (`ndarray`) is provided, the result of the `max()` operation is written directly into that array instead of creating and returning a new one. + - The provided array must have a shape and data type compatible with the result of the operation. + - If the shapes do not match or the array is not writable, NumPy raises a `ValueError`. + - Using `out` is memory-efficient, as it prevents allocating new arrays when performing large computations. + - If `None` (default), NumPy creates and returns a new array containing the result. + +- `keepdims` *(bool, optional)* — Determines whether the reduced dimensions are retained as dimensions of size one in the result. + - `True` **:** Keeps the reduced dimensions as size one, preserving the original array’s dimensionality. This is useful for maintaining compatibility during broadcasting or further operations. + - `False` *(default)* **:** Removes the reduced dimensions from the result, producing a smaller array. + +- `initial` *(scalar, optional)* **:** The minimum value used to start comparisons when computing the maximum. + - Acts as a baseline comparison value for the reduction operation. + - Ensures that even if all array elements are smaller than the `initial` value, the result will still be at least equal to `initial`. + - If `initial` is smaller than the largest element in the array, the actual maximum value is returned. + +- `where` *(array_like of bool, optional)* **:** Condition that selects which elements to include in the comparison. + - Only elements corresponding to `True` values in the `where` mask are considered when computing the maximum. + - Elements where the condition is `False` are ignored in the comparison. + - If `where` is not provided, all elements are included by default. + +**Returns:** + +- *(ndarray or scalar)*, The return type depends on the input array and the value of the `axis` parameter. + - If an `axis` is specified (`0` or `1`), the method returns a **NumPy array (`ndarray`)** containing the maximum values computed along the specified axis. + - For `axis=0`, the result is a one-dimensional array of maximum values from each column. + - For `axis=1`, the result is a one-dimensional array of maximum values from each row. + - When `keepdims=True`, the reduced dimension is retained with size one, so the output maintains the same number of dimensions as the original array. + - If `axis=None`, the method flattens the array and returns a **single scalar value** representing the largest element in the entire array. + - If an `out` array is provided, the result is also stored directly in that array, and a reference to it is returned. + +> **Note:** When no axis is specified, the `max()` method returns a scalar value representing the overall maximum element in the array. +> **Note:** The `max()` functionality can also be accessed using the top-level NumPy function `np.max(array, axis=None)`. Both approaches produce the same result, but `ndarray.max()` is the object-oriented method version, while `np.max()` is the functional interface that can operate on array-like objects. + + +## Examples + +Below are focused examples that demonstrate how each parameter of `ndarray.max()` works, followed by combinations and common pitfalls. + +### Single-Parameter Examples + +#### A) `axis` +###### a. Overall maximum (no axis). +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +# Computes the maximum value over the entire array, +# regardless of dimensions, and returns a single scalar value +# representing the largest element. +print(arr.max()) + +# or + +print(arr.max(axis=None)) +``` +Both produces same results and are equivalent. +This example results in the following output: + +```shell +9 +``` +
+ +###### b. Column-wise maxima (axis=0). +```py +# Operates vertically, comparing elements column by column. +# The result is a one-dimensional array containing the maximum values +# from each column. +print(arr.max(axis=0)) +``` +This example results in the following output: + +```shell +[ 4 8 9 ] +``` + +
+ +###### c. Row-wise maxima (axis=1). + +```py +# Computes the maximum value over the entire array, +# regardless of dimensions, and returns a +# single scalar value representing the largest element. +print(arr.max(axis=1)) +``` + +This example results in the following output: + +```shell +[ 8 9 ] +``` + +
+ +--- + +#### B) `out` +###### a. Preallocate output buffer for column-wise maxima. +```py + +import numpy as np + +arr = np.array([[2, 5, 8], + [3, 7, 1]]) + +out_arr = np.empty(3, dtype=arr.dtype) +arr.max(axis=0, out=out_arr) + +print(out_arr) +``` +This example results in the following output: + +```shell +[ 3 7 8 ] +``` + +
+ +###### b. Reuse the same buffer for multiple arrays +```py +import numpy as np + +arr1 = np.array([[2, 8, 5], + [4, 1, 9]]) +arr2 = np.array([[3, 7, 6], + [2, 10, 4]]) + +out_buf = np.empty(1, dtype=arr1.dtype) + +arr1.max(out=out_buf) +print("Max arr1:", out_buf) + +arr2.max(out=out_buf) +print("Max arr2:", out_buf) +``` + +This example results in the following output: + +```shell +Max arr1: [9] +Max arr2: [10] +``` + +
+ +###### c. Safe dtype choice: store integer max into a float buffer +```py +import numpy as np + +arr = np.array([[1, 4, 3], + [6, 2, 5]], dtype=np.int32) + +# Float buffer is safe for storing an integer maximum. +out_float = np.empty(1, dtype=np.float64) +arr.max(out=out_float) + +print(out_float) +``` +This example results in the following output: + +```shell +[6.] +``` + +
+ +###### d. In-place overwrite: prefill buffer, then write the result +```py +import numpy as np + +arr = np.array([[5, 3, 9], + [2, 7, 1]]) + +out_arr = np.array([-999], dtype=arr.dtype) # Prefilled sentinel. + +arr.max(out=out_arr) # Overwrites in place. +print(out_arr) +``` + +This example results in the following output: + +```shell +[9] +``` + +
+ +###### e. Functional form: np.max with out (equivalent to ndarray.max) +```py +import numpy as np + +arr = np.array([[2, 11, 4], + [3, 7, 6]]) + +out_arr = np.empty(1, dtype=arr.dtype) + +# Top-level function also supports 'out'. +np.max(arr, out=out_arr) + +print(out_arr) +``` + +This example results in the following output: + +```shell +[11] +``` + +
+ +--- + +#### C) `keepdims` +###### a. Default behavior — `keepdims=False` +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +# Default: keepdims=False +result = arr.max(keepdims=False) + +print(result) +print("Shape:", np.shape(result)) +``` + +This example results in the following output: + +```shell +9 +Shape: () +``` +By default, `keepdims=False` removes reduced dimensions, returning a scalar since the array was flattened. + +
+ +###### b. Preserve dimensionality: keepdims=True +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +# Retains reduced dimensions as size-one dimensions. +result = arr.max(keepdims=True) + +print(result) +print("Shape:", result.shape) +``` + +This example results in the following output: + +```shell +[[9]] +Shape: (1, 1) +``` + +Setting `keepdims=True` preserves the array’s dimensionality, +the output keeps two dimensions with size 1 for each reduced axis + +
+ +###### c. Compare shapes with and without keepdims +```py +import numpy as np + +arr = np.array([[3, 5, 7], + [2, 9, 1]]) + +res_default = arr.max() # keepdims=False (default) +res_keepdims = arr.max(keepdims=True) + +print("Without keepdims:", res_default, "Shape:", np.shape(res_default)) +print("With keepdims: ", res_keepdims, "Shape:", res_keepdims.shape) +``` + +This example results in the following output: + +```shell +Without keepdims: 9 Shape: () +With keepdims: [[9]] Shape: (1, 1) +``` +The result value is the same, but `keepdims=True` retains an extra dimension, making it compatible for broadcasting with the original array. + +
+ +--- + +#### D) `where` +###### a. Basic use: include only elements satisfying a condition +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +# Consider only elements greater than 5 +mask = arr > 5 + +result = arr.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +9 +``` +Only elements satisfying `arr > 5` are compared. +The global maximum among these values is 9. + +
+ +###### b. Using 'where' with a threshold condition +```py +import numpy as np + +arr = np.array([[3, 7, 2], + [5, 1, 9]]) + +# Include only elements that are odd numbers +mask = arr % 2 == 1 + +result = arr.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +9 +``` + +The method ignores even numbers and computes the maximum only among odd numbers. + +
+ +###### c. Boolean mask from another array +```py +import numpy as np + +data = np.array([[10, 20, 30], + [40, 50, 60]]) + +mask = np.array([[True, False, True], + [False, True, False]]) + +# Compare only where mask is True +result = data.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +50 +``` +`where` can take a separate Boolean array of the same shape, +only positions marked `True` are used in the comparison. + +
+ +###### d. Chained condition in where +```py +import numpy as np + +arr = np.arange(1, 11) + +# Include elements that are multiples of 3 or 5 +mask = (arr % 3 == 0) | (arr % 5 == 0) + +result = arr.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +# 10 +``` +The mask selects elements {3, 5, 6, 9, 10} and returns the maximum (10). + +
+ +###### e. Using a full True mask +```py +import numpy as np + +arr = np.array([4, 7, 2]) + +mask = np.array([True, True, True]) + +result = arr.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +7 +``` +When all mask elements are True, the result is identical to arr.max(). + +
+ +###### f. All False mask +```py +import numpy as np + +arr = np.array([[3, 5, 7], + [2, 4, 6]]) + +mask = np.zeros_like(arr, dtype=bool) # all False + +# Since all are excluded, NumPy returns the smallest possible value of dtype +result = arr.max(where=mask) + +print(result) +``` + +This example results in the following output: + +```shell +6 +``` +NumPy ignores all elements when mask is all False, +it still returns the overall dtype’s lowest initialized value (for ints, the last seen value or minimum). +To control this safely, initial should be used, but that’s beyond this scope. + +
+ + +###### g. Using a runtime-generated condition +```py +import numpy as np + +arr = np.array([10, 5, 3, 7, 9]) + +# Mask depends on a variable threshold +threshold = 6 +mask = arr > threshold + +result = arr.max(where=mask) + +print("Threshold:", threshold) +print("Mask:", mask) +print("Max where condition met:", result) +``` + +This example results in the following output: + +```shell +Threshold: 6 +Mask: [ True False False True True] +Max where condition met: 10 +``` + +The mask can be created dynamically at runtime using variables or computed expressions. + +
+ +###### h. Mask with computed comparison +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 10, 1]]) + +# Compare using another array as a threshold +thresholds = np.array([[3, 5, 0], + [2, 7, 1]]) + +mask = arr > thresholds +result = arr.max(where=mask) + +print("Mask:\n", mask) +print("Max where condition met:", result) +``` + +This example results in the following output: + +```shell +Mask: + [[False True True] + [ True True False]] +Max where condition met: 10 +``` + +The mask can come from elementwise comparisons between arrays of the same shape. + +
+ + +###### i. Masked selection on flattened array +```py +import numpy as np + +arr = np.arange(12).reshape(3, 4) + +# Only include values greater than 7 +mask = arr > 7 + +result = arr.max(where=mask) + +print("Flattened mask result:", result) +``` + +This example results in the following output: + +```shell +11 +``` + +When no axis is provided, NumPy flattens both the array and mask before applying the condition. + +
+ +--- + +#### E) `initial` +###### a. Default behavior — no `initial` provided +```py +import numpy as np + +arr = np.array([4, 7, 2]) + +# Default max operation +result = arr.max() + +print(result) +``` + +This example results in the following output: + +```shell +7 +``` + +Without an `initial` value, `max()` compares only the array’s elements and returns the true maximum. + +
+ +###### b. Providing an "initial" value larger than all elements +```py +import numpy as np + +arr = np.array([4, 7, 2]) + +# Baseline value is larger than all elements +result = arr.max(initial=10) + +print(result) +``` + +This example results in the following output: + +```shell +10 +``` + +When `initial` is greater than every element, it becomes the maximum itself, because comparisons start from `10`. + +
+ +###### c. Providing an initial value smaller than all elements +```py +import numpy as np + +arr = np.array([4, 7, 2]) + +# Baseline value is smaller than all elements +result = arr.max(initial=0) + +print(result) +``` + +This example results in the following output: + +```shell +7 +``` +If the `initial` value is smaller, it doesn’t affect the outcome, +the true maximum from the array is still returned. + +
+ +###### d. initial with negative values +```py +import numpy as np + +arr = np.array([-8, -4, -6]) + +# Use an initial value smaller than all elements +result = arr.max(initial=-10) + +print(result) +``` + +This example results in the following output: + +```shell +-4 +``` +The initial value acts as a baseline for comparison. Since all array values are greater than `-10`, the maximum value `-4` is returned. + +
+ +###### e. Empty array with initial +```py +import numpy as np + +arr = np.array([]) + +# Works only when initial is provided +result = arr.max(initial=5) + +print(result) +``` + +This example results in the following output: + +```shell +5 +``` +When the array is empty, the `initial` value is returned. +Without `initial`, an empty array would cause a `ValueError`. + +
+ +###### f. Using initial to define a safe fallback +```py +import numpy as np + +arr = np.array([-3, -9, -1]) + +# Use initial to ensure at least 0 is returned, even if all are negative +result = arr.max(initial=0) + +print(result) +``` + +This example results in the following output: + +```shell +0 +``` +Even though all elements are negative, `initial=0` ensures a non-negative fallback result + +
+ +--- + +### Multi-Parameter Examples +#### A) `axis` + `out` Together +###### a. 2D array — `axis=0` (column-wise) into a 1-D buffer +```py +import numpy as np + +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +# Result shape for axis=0 is (3,) +out_cols = np.empty(3, dtype=arr.dtype) + +arr.max(axis=0, out=out_cols) +print(out_cols) +``` + +This example results in the following output: + +```shell +[4 8 9] +``` +Computes column-wise maxima and writes them directly into a 1-D buffer `out_cols`. + +
+ +###### b. 2D array — `axis=1` (row-wise) into a 1-D buffer +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +out_rows = np.empty(2, dtype=arr.dtype) +arr.max(axis=1, out=out_rows) +print(out_rows) +``` + +This example results in the following output: + +```shell +[8 9] +``` +Finds the maximum of each row and stores results in `out_rows`. + +
+ +###### c. 2D array — `axis=None` into a 1-element buffer +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +out_scalar = np.empty(1, dtype=arr.dtype) +arr.max(axis=None, out=out_scalar) +print(out_scalar) +``` + +This example results in the following output: + +```shell +[9] +``` +Flattens the array and writes the global maximum into a single-element buffer. + +
+ +###### d. 2D array — negative axis (`axis=-1` equals last axis) into a 1-D buffer +```py +import numpy as np +arr = np.array([[3, 1, 7], + [2, 9, 4]]) +out_last = np.empty(2, dtype=arr.dtype) +arr.max(axis=-1, out=out_last) +print(out_last) +``` + +This example results in the following output: + +```shell +[7 9] +``` +Uses `axis=-1` (last axis) to compute per-row maxima, identical to `axis=1`. + +
+ +###### e. 3D array — `axis=0` into a 2-D buffer +```py +import numpy as np + +arr = np.arange(24).reshape(2, 3, 4) # Shape (2, 3, 4) + +# Reducing axis=0 on (2,3,4) -> result shape is (3,4) +out_0 = np.empty((3, 4), dtype=arr.dtype) + +arr.max(axis=0, out=out_0) +print("Shape:", out_0.shape) +print(out_0) +``` + +This example results in the following output: + +```shell +Shape: (3, 4) +[[12 13 14 15] + [16 17 18 19] + [20 21 22 23]] +``` + +Reduces the first dimension; maxima are taken across layers and written to a 2-D buffer. + +
+ +###### f. 3D array — `axis=1` into a 2-D buffer +```py +import numpy as np + +arr = np.arange(24).reshape(2, 3, 4) # Shape (2, 3, 4) + +# Reducing axis=1 -> result shape is (2,4) +out_1 = np.empty((2, 4), dtype=arr.dtype) + +arr.max(axis=1, out=out_1) +print("Shape:", out_1.shape) +print(out_1) +``` + +This example results in the following output: + +```shell +Shape: (2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` +Finds maxima along the middle axis for each 3-D block and stores them in out_1. + +
+ +###### g. 3D array — `axis=2` (or `axis=-1`) into a 2-D buffer +```py +import numpy as np + +arr = np.arange(24).reshape(2, 3, 4) # Shape (2, 3, 4) + +# Reducing axis=2 -> result shape is (2,3) +out_2 = np.empty((2, 3), dtype=arr.dtype) + +arr.max(axis=2, out=out_2) +print("Shape:", out_2.shape) +print(out_2) +``` + +This example results in the following output: + +```shell +Shape: (2, 3) +[[ 3 7 11] + [15 19 23]] +``` + +Reduces the last dimension; each row of `out_2` contains maxima from one 2-D slice. + +
+ +###### h. Reusing the same buffer across calls with the same result shape +```py +import numpy as np + +arr1 = np.array([[2, 8, 5], + [4, 1, 9]]) +arr2 = np.array([[3, 7, 6], + [2, 10, 4]]) + +# For axis=0 on a 2x3 array, result shape is (3,) +buf = np.empty(3, dtype=arr1.dtype) + +arr1.max(axis=0, out=buf) +print("arr1 col max:", buf) + +arr2.max(axis=0, out=buf) +print("arr2 col max:", buf) +``` + +This example results in the following output: + +```shell +arr1 col max: [ 4 8 9] +arr2 col max: [ 3 10 6] +``` + +Reuses one output buffer for multiple arrays with identical result shapes. + +
+ +###### i. Using a reshaped view as out (same memory, matching shape) +```py +import numpy as np + +arr = np.arange(24).reshape(2, 3, 4) + +# Prepare a flat buffer and reshape it to match the expected result shape (3,4) for axis=0 +flat = np.empty(12, dtype=arr.dtype) +out_view = flat.reshape(3, 4) + +arr.max(axis=0, out=out_view) +print("Flat buffer (reshaped) received result:\n", out_view) +``` + +This example results in the following output: + +```shell +Flat buffer (reshaped) received result: + [[12 13 14 15] + [16 17 18 19] + [20 21 22 23]] +``` + +Writes results into a reshaped view of a flat buffer, efficiently reusing memory. + +
+ +--- + +#### B) `axis` and `keepdims` Together +###### a. 2D — `axis=None`, `keepdims=False` (default) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(keepdims=False) +print(res, np.shape(res)) +``` + +This example results in the following output: + +```shell +9 () +``` + +Computes the global maximum and returns a scalar (shape `()`). + +
+ +###### b. 2D — `axis=None`, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(keepdims=True) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[[9]] (1, 1) +``` +Reduces over all axes but preserves dimensionality, returning a `(1, 1)` result. + +
+ +###### c. 2D — `axis=0`, `keepdims=False` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(axis=0, keepdims=False) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[4 8 9] (3,) +``` + +Takes column-wise maxima; reduced axis is removed, returning a 1-D array. + +
+ +###### d. `axis=0`, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(axis=0, keepdims=True) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[[4 8 9]] (1, 3) +``` +Column-wise maxima with shape preserved; reduced axis kept as size one. + +
+ +###### f. 2D — `axis=1`, `keepdims=False` +```py +import numpy as np +arr = np.array([[10, 3, 7], + [4, 8, 2]]) + +res = arr.max(axis=1, keepdims=False) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[10 8] (2,) +``` +Computes row-wise maxima; result is a 1-D array of length equal to rows. + +
+ +###### g. 2D — `axis=1`, `keepdims=True` +```py +import numpy as np +arr = np.array([[10, 3, 7], + [4, 8, 2]]) + +res = arr.max(axis=1, keepdims=True) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[[10] + [ 8]] (2, 1) +``` + +Row-wise maxima keeping the reduced axis as size one for broadcasting. + +
+ +###### h. 2D — `axis=-1` (last axis), `keepdims=True` +```py +import numpy as np +arr = np.array([[3, 1, 7], + [2, 9, 4]]) + +res = arr.max(axis=-1, keepdims=True) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +[[7] + [9]] (2, 1) +``` + +Negative axis targets the last dimension; equivalent to axis=1 here. + +
+ +###### i. `axis=0`, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=0, keepdims=False) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(3, 4) +[[12 13 14 15] + [16 17 18 19] + [20 21 22 23]] +``` + +Reduces the first dimension; result shape is the remaining `(3, 4)`. + +
+ +###### j. 3D — axis=0, keepdims=True +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=0, keepdims=True) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(1, 3, 4) +[[[12 13 14 15] + [16 17 18 19] + [20 21 22 23]]] +``` + +Same reduction but preserves the reduced axis as size one. + +
+ +###### k. `3D — axis=1`, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=1, keepdims=False) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` + +Reduces the middle axis; keeps outer and last dimensions. + +
+ +###### l. 3D — `axis=1`, `keepdims=True` + +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=1, keepdims=True) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Preserves the reduced middle axis as size one for easier broadcasting. + +
+ +###### m. 3D — `axis=2`, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=2, keepdims=False) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 3) +[[ 3 7 11] + [15 19 23]] +``` + +Reduces the last axis; result holds per-row maxima for each slice. + +
+ +###### n. 3D — `axis=2`, `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(axis=2, keepdims=True) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 3, 1) +[[[ 3] + [ 7] + [11]] + + [[15] + [19] + [23]]] +``` + +Keeps the last axis as size one, maintaining 3-D shape for broadcasting. + +
+ +###### o. 1D array — `axis=None`, `keepdims=False` +```py +import numpy as np +arr = np.array([3, 8, 5]) +res = arr.max(axis=None, keepdims=False) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +8 () +``` + +Computes the global maximum over the 1-D array; returns a scalar. + +
+ +--- + +#### C) `axis` and `where` Together + +###### a. 2D — `axis=0` with value mask +```py +import numpy as np +arr = np.array([[6, 2, 5], + [4, 9, 7]]) +mask = arr > 5 # Ensure at least one True per column. +res = arr.max(axis=0, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[6 9 7] +``` + +Compares column-wise but only where values exceed 5; each column has at least one candidate. + +
+ +###### b. 2D — axis=1 with odd-number mask +```py +import numpy as np +arr = np.array([[3, 8, 5], + [4, 7, 2]]) +mask = (arr % 2) == 1 # Odds only. +res = arr.max(axis=1, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[5 7] +``` + +Reduces row-wise considering only odd entries; each row retains at least one odd. + +
+ +###### c. 2D — `axis=-1` (last axis) with multiples-of-n mask +```py +import numpy as np +arr = np.array([[3, 6, 1], + [5, 9, 7]]) +n = 3 # here n can be randomized +mask = (arr % n) == 0 +res = arr.max(axis=-1, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[6 9] +``` + +Uses the last axis; only entries divisible by three are considered per row. + +
+ +###### d. 2D — `axis=0` with broadcast row mask `(2,1)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +row_mask = np.array([[True], + [False]]) # Select only first row across all columns. +res = arr.max(axis=0, where=row_mask) +print(res) +``` + +This example results in the following output: + +```shell +[2 8 5] +``` + +Broadcasts a row mask; column-wise reduction considers only the first row. + +
+ +###### e. 2D — `axis=1` with broadcast column mask `(1,3)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +col_mask = np.array([[True, False, True], + [False, True, True]]) # Ensure at least one True per row. +res = arr.max(axis=1, where=col_mask) +print(res) +``` + +This example results in the following output: + +```shell +[5 9] +``` + +Broadcasts a column mask; row-wise reduction compares only permitted columns. + +
+ +###### f. 2D — `axis=None` (global) with condition +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr > 5 +res = arr.max(axis=None, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +9 +``` + +Flattens array and finds the maximum among elements greater than five. + +
+ +###### g. 2D — `axis=-1` with comparison-based mask +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr +res = arr.max(axis=-1, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[5 9] +``` + +Creates a mask from pairwise comparison; reduces along the last axis per row. + +
+ +###### h. 3D — `axis=0` with layer-select mask +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # Values 0..23 +mask = np.array([True, False])[:, None, None] # Use only first layer. +res = arr.max(axis=0, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[[ 0 1 2 3] + [ 4 5 6 7] + [ 8 9 10 11]] +``` + +Reduces across the first dimension while selecting only layer 0. + +
+ +###### i. 3D — `axis=1` with guaranteed-one-True mask + +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 0, :] = True # Ensure one True along axis=1. +res = arr.max(axis=1, where=mask) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 4) +[[ 0 1 2 3] + [12 13 14 15]] +``` + +Row-of-slices mask ensures each (batch, col) position has a candidate; result equals the first slice. + +
+ +###### j. 3D — `axis=2` with periodic mask on last axis +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = np.array([True, False, True, False])[None, None, :] # Pick indices 0 and 2. +res = arr.max(axis=2, where=mask) +print(res.shape) +print(res) +``` + +This example results in the following output: + +```shell +(2, 3) +[[ 2 6 10] + [14 18 22]] +``` + +Reduces over the last axis considering positions 0 and 2 only; values increase with index, so index 2 dominates. + +
+ +###### k. 1D — axis=0 with threshold mask +```py +import numpy as np +arr = np.array([3, 9, 1, 7]) +mask = arr > 5 +res = arr.max(axis=0, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +9 +``` + +Reduces a 1-D array with a same-shape mask; picks the max among elements greater than five. + +
+ +###### l. 1D — `axis=None` (global) with even-number mask +```py +import numpy as np +arr = np.array([2, 11, 4, 7, 10]) +mask = (arr % 2) == 0 +res = arr.max(axis=None, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +10 +``` + +Flattens (trivial for 1-D) and selects only even elements before computing the maximum. + +
+ +###### m. 2D — `axis=0` with broadcast column mask `(1, n)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +col_mask = np.array([[True, False, True]]) # Broadcast over rows. +res = arr.max(axis=0, where=col_mask) +print(res) +``` + +This example results in the following output: + +```shell +[4 1 9] +``` + +Reduces by column but only considers selected columns across all rows via a (1, n) mask. + +
+ +###### n. 2D — `axis=1` with scalar comparison mask (broadcast) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +mask = arr >= 6 # Broadcasted scalar threshold. +res = arr.max(axis=1, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +[8 9] +``` + +Row-wise maxima considering only values ≥ 6 in each row. + +
+ +###### o. 3D — `axis=1` with mask shape `(b, 1, c)` (broadcast middle axis) +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # (2,3,4) +mask = np.array([[True, False, True, False], + [False, True, True, False]])[:, None, :] # (2,1,4) +res = arr.max(axis=1, where=mask) +print(res.shape); print(res) +``` + +This example results in the following output: + +```shell +(2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` +Broadcasts mask over the middle axis; per batch/column, at least one True ensures a valid candidate. + +
+ +###### p. 3D — `axis=2` with mask shape `(1, m, 1)` (pick rows across depth) +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # (2,3,4) +row_pick = np.array([[True],[False],[True]])[None, :, :] # (1,3,1) +res = arr.max(axis=2, where=row_pick) +print(res.shape); print(res) +``` + +This example results in the following output: + +```shell +(2, 3) +[[ 3 7 11] + [15 19 23]] +``` + +Selects rows 0 and 2 across the last axis, then reduces over depth for those rows only. + +
+ +###### q. 3D — `axis=None` (global) with composite condition +```py +import numpy as np +arr = np.arange(1, 13).reshape(3, 4) +mask = (arr % 2 == 0) | (arr > 9) +res = arr.max(axis=None, where=mask) +print(res) +``` + +This example results in the following output: + +```shell +12 +``` + +Flattens and compares only elements that are even or greater than nine, returning the global maximum. + +
+ +--- + +#### D) `axis` and `initial` Together + +###### a. 1D — `axis=0`, `initial` larger than all +```py +import numpy as np +arr = np.array([4, 7, 2]) +res = arr.max(axis=0, initial=10) +print(res) +``` +This example results in the following output: +```shell +10 +``` +`initial` is compared with all elements; since 10 exceeds the array max, it becomes the result. + +
+ +###### b. 1D — `axis=0`, `initial` smaller than max +```py +import numpy as np +arr = np.array([4, 7, 2]) +res = arr.max(axis=0, initial=3) +print(res) +``` +This example results in the following output: +```shell +7 +``` +initial is a baseline; the true maximum (7) dominates when greater than initial. + +
+ +###### c. 1D (empty) — `axis=0` with `initial` +```py +import numpy as np +arr = np.array([], dtype=int) +res = arr.max(axis=0, initial=5) +print(res) +``` +This example results in the following output: +```shell +5 +``` +For an empty array, the reduction returns the provided initial as a safe fallback. + +
+ +###### d. 2D — `axis=0`, column-wise with baseline +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +res = arr.max(axis=0, initial=5) +print(res) +``` +This example results in the following output: +```shell +[7 5 6] +``` +Per column, the result is max(column_max, initial); only the second column is lifted to 5. + +
+ +###### e. 2D — `axis=1`, row-wise with higher baseline +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +res = arr.max(axis=1, initial=8) +print(res) +``` +This example results in the following output: +```shell +[8 8] +``` +Each row’s maximum is compared with 8; both rows are raised to the baseline. + +
+ +###### f. 2D — `axis=-1` (last axis), moderate baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +res = arr.max(axis=-1, initial=6) +print(res) +``` +This example results in the following output: +```shell +[9 6] +``` +Reduces across the last axis; each row’s result is max(row_max, 6). + +
+ +###### g. 2D — `axis=None`, global reduction with high baseline +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +res = arr.max(axis=None, initial=12) +print(res) +``` +This example results in the following output: +```shell +12 +``` +Flattens the array; the global maximum is compared with `initial`, so 12 wins. + +
+ +###### h. 3D — `axis=0`, baseline affecting smaller positions +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +res = arr.max(axis=0, initial=15) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(3, 4) +[[15 15 15 15] + [16 17 18 19] + [20 21 22 23]] +``` +Across layers, each (row, col) takes `max(layer_max, 15)`; low positions are lifted to 15. + +
+ +###### i. 3D — `axis=2` (last), per-row baseline +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +res = arr.max(axis=2, initial=5) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3) +[[ 5 7 11] + [15 19 23]] +``` +For each row across depth, the result is `max(row_max, 5)`; only smaller rows are raised. + +
+ +--- + +#### E) `out` and `keepdims` Together + +###### a. 2D — Default shape match (`keepdims=False`) +```py +import numpy as np +arr = np.array([[2, 5, 8], + [3, 7, 1]]) + +out_arr = np.empty(3, dtype=int) +arr.max(axis=0, out=out_arr, keepdims=False) + +print(out_arr) +``` +This example results in the following output: +```shell +[3 7 8] +``` +Writes column-wise maxima directly into `out_arr`. Since `keepdims=False`, the output shape `(3,)` matches the column count. + +
+ +###### b. 2D — Preserve dimensions (keepdims=True) +```py +import numpy as np +arr = np.array([[2, 5, 8], + [3, 7, 1]]) + +out_arr = np.empty((1, 3), dtype=int) +arr.max(axis=0, out=out_arr, keepdims=True) + +print(out_arr) +``` +This example results in the following output: +```shell +[[3 7 8]] +``` +Keeps reduced dimension as size one; result fits into a `(1, 3)` output array. + +
+ +###### c. 2D — Row-wise reduction (`axis=1`, `keepdims=False`) +```py +import numpy as np +arr = np.array([[4, 1, 9], + [3, 7, 5]]) + +out_arr = np.empty(2, dtype=int) +arr.max(axis=1, out=out_arr, keepdims=False) + +print(out_arr) +``` +This example results in the following output: +```shell +[9 7] +``` +Stores the maximum of each row directly in a 1D array `(2,)`. + +
+ +###### d. 2D — Row-wise with dimension retained (`keepdims=True`) +```py +import numpy as np +arr = np.array([[4, 1, 9], + [3, 7, 5]]) + +out_arr = np.empty((2, 1), dtype=int) +arr.max(axis=1, out=out_arr, keepdims=True) + +print(out_arr) +``` +This example results in the following output: +```shell +[[9] + [7]] +``` +Keeps the reduced axis as size one, so the output shape `(2, 1)` aligns with broadcasting rules. + +
+ +###### e. 3D — Layer reduction (`axis=0`, `keepdims=False`) +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +out_arr = np.empty((3, 4), dtype=int) + +arr.max(axis=0, out=out_arr, keepdims=False) +print(out_arr) +``` +This example results in the following output: +```shell +[[12 13 14 15] + [16 17 18 19] + [20 21 22 23]] +``` +Reduces across the first axis; results stored in `(3, 4)` array matching the reduced shape. + +
+ +###### f. 3D — Layer reduction with dimension retained (`keepdims=True`) +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +out_arr = np.empty((1, 3, 4), dtype=int) + +arr.max(axis=0, out=out_arr, keepdims=True) +print(out_arr.shape) +``` +This example results in the following output: +```shell +(1, 3, 4) +``` +Preserves the first axis as a singleton; output retains full dimensionality for broadcasting. + +
+ +###### g. 2D — Global maximum (`axis=None`, `keepdims=False`) +```py +import numpy as np +arr = np.array([[2, 5, 8], + [3, 7, 1]]) + +out_arr = np.empty((), dtype=int) +arr.max(axis=None, out=out_arr, keepdims=False) + +print(out_arr) +``` +This example results in the following output: +```shell +8 +``` +Stores a single scalar maximum (global) in a zero-dimensional array container. + +
+ +###### h. 2D — Global maximum with dimension retained (`keepdims=True`) +```py +import numpy as np +arr = np.array([[2, 5, 8], + [3, 7, 1]]) + +out_arr = np.empty((1, 1), dtype=int) +arr.max(axis=None, out=out_arr, keepdims=True) + +print(out_arr) +``` +This example results in the following output: +```shell +[[8]] +``` +Retains 2D shape `(1, 1)` while computing the overall maximum; useful for dimensional consistency. + +
+ +--- + +#### F) `out` and `where` Together + +###### a. Basic mask to select values + +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr > 5 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, out=out0) +print(out0) +``` + +This example results in the following output: + +```shell +9 +``` +Compares only elements greater than 5 and writes the global maximum into a 0-D output. + +
+ +###### b. Full-True mask (equivalent to normal max) +```py +import numpy as np +arr = np.array([[1, 3], + [2, 4]]) +mask = np.ones_like(arr, dtype=bool) +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +4 +``` +A full-True mask behaves like no mask; the overall maximum is stored into `out0`. + +
+ +###### c. Buffer reuse across arrays +```py +import numpy as np +arr1 = np.array([[2, 8, 5], + [4, 1, 9]]) +arr2 = np.array([[3, 7, 6], + [2,10, 4]]) +mask1 = arr1 > 5 +mask2 = arr2 % 2 == 0 +out0 = np.empty((), dtype=int) +arr1.max(where=mask1, out=out0); print("arr1:", out0) +arr2.max(where=mask2, out=out0); print("arr2:", out0) +``` +This example results in the following output: +```shell +arr1: 9 +arr2: 10 +``` +Reuses the same 0-D buffer for different arrays and masks. + +
+ +###### d. Broadcast row mask on 2×3, global reduction +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +row_mask = np.array([[True],[False]]) # Use only first row. +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=row_mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +8 +``` +Broadcasts a (2,1) mask; the global maximum is taken from the first row only. + +
+ +###### e. Mask from pairwise comparison with another array +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[1, 9, 0], + [3, 5,10]]) +mask = arr > thr +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +6 +``` +Builds a mask by comparing two arrays elementwise; writes the maximum of permitted positions. + +
+ +###### f. Sparse mask (pick specific coordinates) +```py +import numpy as np +arr = np.array([[10, 20, 30], + [40, 50, 60]]) +mask = np.zeros_like(arr, dtype=bool) +mask[0,2] = True; mask[1,1] = True +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +50 +``` +Selects only positions (0,2) and (1,1); the maximum among those is written to out0. + +
+ +###### g. 1-D array with even-number mask; 1-element out +```py +import numpy as np +arr = np.array([2, 11, 4, 7, 10]) +mask = (arr % 2) == 0 +out1 = np.empty(1, dtype=arr.dtype) +arr.max(where=mask, out=out1) +print(out1) +``` +This example results in the following output: +```shell +[10] +``` +Stores the masked global maximum into a 1-element output vector. + +
+ +###### h. 3-D array; mask selects large values only +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = arr >= 20 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +23 +``` +Flattens under the hood and compares only elements ≥20; writes the overall maximum. + +
+ +###### i. Safe dtype upcast in out +```py +import numpy as np +arr = np.array([[1, 4, 3], + [6, 2, 5]], dtype=np.int32) +mask = arr >= 4 +outf = np.empty((), dtype=np.float64) +arr.max(where=mask, out=outf) +print(outf) +``` +This example results in the following output: +```shell +6.0 +``` +Stores an integer result into a float output using safe upcasting. + +
+ +###### j. Overwriting a prefilled 0-D buffer +```py +import numpy as np +arr = np.array([[5, 3, 9], + [2, 7, 1]]) +mask = arr > 4 +out0 = np.array(-999, dtype=arr.dtype) # Prefilled sentinel. +arr.max(where=mask, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +The masked maximum overwrites the prefilled scalar buffer in place. + +
+ +--- + +#### G) `out` and `initial` Together +###### a. Basic: `initial` smaller than the true max +```py +import numpy as np +arr = np.array([4, 7, 2]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(initial=3, out=out0) +print(out0) +``` +This example results in the following output: +```shell +7 +``` +`initial` is a baseline; the array’s real maximum (7) wins and is written into `out0`. + +
+ +###### b. Baseline dominates: `initial` larger than all elements +```py +import numpy as np +arr = np.array([4, 7, 2]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(initial=10, out=out0) +print(out0) +``` +This example results in the following output: +```shell +10 +``` +Because `initial` exceeds every element, it becomes the result stored in `out0`. + +
+ +###### c. Empty array with `initial` +```py +import numpy as np +arr = np.array([], dtype=int) +out0 = np.empty((), dtype=arr.dtype) +arr.max(initial=5, out=out0) +print(out0) +``` +This example results in the following output: +```shell +5 +``` +For empty input, the reduction returns the provided `initial`, safely written to `out0`. + +
+ +###### d. Reusing the same buffer across calls +```py +import numpy as np +a = np.array([2, 9, 1]) +b = np.array([3, 5, 7]) +out0 = np.empty((), dtype=int) + +a.max(initial=0, out=out0); print("a:", out0) +b.max(initial=10, out=out0); print("b:", out0) +``` +This example results in the following output: +```shell +a: 9 +b: 10 +``` +One 0-D buffer is reused: first returns the true max of `a`, then `initial` dominates for `b`. + +
+ +###### e. Safe upcast: int array → float `out`, float `initial` +```py +import numpy as np +arr = np.array([1, 6, 3], dtype=np.int32) +outf = np.empty((), dtype=np.float64) +arr.max(initial=4.5, out=outf) +print(outf) +``` +This example results in the following output: +```shell +6.0 +``` +Result is safely stored as float; `initial=4.5` doesn’t exceed the array maximum. + +
+ +###### f. Float array, float `out`, high `initial` +```py +import numpy as np +arr = np.array([1.2, 3.4, 2.8]) +outf = np.empty((), dtype=arr.dtype) +arr.max(initial=5.0, out=outf) +print(outf) +``` +This example results in the following output: +```shell +5.0 +``` +`initial` defines a higher floor than all elements, so the result is the baseline. + +
+ +###### g. Prefilled buffer overwritten in place +```py +import numpy as np +arr = np.array([5, 3, 9]) +out0 = np.array(-999, dtype=arr.dtype) # Sentinel value. +arr.max(initial=0, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +The computed maximum overwrites the prefilled scalar buffer. + +
+ +###### h. Larger baseline for nonnegative guarantee +```py +import numpy as np +arr = np.array([-4, -2, -7]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(initial=0, out=out0) +print(out0) +``` +This example results in the following output: +```shell +0 +``` +Even though all values are negative, `initial=0` guarantees a nonnegative result. + +
+ +###### i. Functional form: np.max with out and initial +```py +import numpy as np +arr = np.array([2, 11, 4]) +out0 = np.empty((), dtype=arr.dtype) +np.max(arr, initial=8, out=out0) +print(out0) +``` +This example results in the following output: +```shell +11 +``` +`np.max` mirrors `ndarray.max`; with `initial=8`, the true max (11) still wins. + +
+ +--- + +#### H) `keepdims` and `where` Together +###### a. Basic 2D array with conditional mask, `keepdims=False` (default) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr > 5 +res = arr.max(where=mask, keepdims=False) +print(res, res.shape) +``` + +This example results in the following output: +```shell +9 () +``` +The global maximum is computed only where `arr > 5`; reduced to a scalar because `keepdims=False`. + +
+ +###### b. Same mask but `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr > 5 +res = arr.max(where=mask, keepdims=True) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Keeps the original 2D dimensionality as (1,1) while still computing the same masked maximum. + +
+ +###### c. Full-True mask, `keepdims=False` +```py +import numpy as np +arr = np.array([[1, 3, 5], + [2, 6, 4]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, keepdims=False) +print(res, res.shape) +``` +This example results in the following output: +```shell +6 () +``` +All elements are valid; behave the same as a normal global `max()`. + +
+ +###### d. Full-True mask, `keepdims=True` +```py +import numpy as np +arr = np.array([[1, 3, 5], + [2, 6, 4]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, keepdims=True) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[6]] (1, 1) +``` +Keeps reduced dimensions of size one, producing a 2D array that mirrors the original layout. + +
+ +###### e. Sparse mask — pick only top-right elements, `keepdims=False` +```py +import numpy as np +arr = np.array([[10, 2, 30], + [5, 8, 9]]) +mask = np.zeros_like(arr, dtype=bool) +mask[0, 2] = True +mask[1, 1] = True +res = arr.max(where=mask, keepdims=False) +print(res, res.shape) +``` +This example results in the following output: +```shell +30 () +``` +Only considers positions `(0,2)` and `(1,1)`; returns the largest of those as a scalar. + +
+ +###### f. Sparse mask with same positions, `keepdims=True` +```py +import numpy as np +arr = np.array([[10, 2, 30], + [5, 8, 9]]) +mask = np.zeros_like(arr, dtype=bool) +mask[0, 2] = True +mask[1, 1] = True +res = arr.max(where=mask, keepdims=True) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[30]] (1, 1) +``` +Same elements are considered, but dimensionality is preserved for broadcasting. + +
+ +###### g. 3D array, selective mask, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = arr >= 20 +res = arr.max(where=mask, keepdims=False) +print(res, res.shape) +``` +This example results in the following output: +```shell +23 () +``` +Flattens the 3D array and computes the maximum only where `arr >= 20`; result is scalar. + +
+ +###### h. 3D array, selective mask, `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = arr >= 20 +res = arr.max(where=mask, keepdims=True) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[[23]]] (1, 1, 1) +``` +Keeps the 3D structure (size-one dims), useful for aligning shapes during later broadcasting. + +
+ +###### i. 1D array, partial mask, `keepdims=False` +```py +import numpy as np +arr = np.array([3, 8, 2, 9, 1]) +mask = arr > 5 +res = arr.max(where=mask, keepdims=False) +print(res, res.shape) +``` + +This example results in the following output: + +```shell +9 () +``` +Only elements greater than 5 are compared; result is scalar since `keepdims=False`. + +
+ +###### j. 1D array, same mask, `keepdims=True` +```py +import numpy as np +arr = np.array([3, 8, 2, 9, 1]) +mask = arr > 5 +res = arr.max(where=mask, keepdims=True) +print(res, res.shape) +``` +This example results in the following output: +```shell +[9] (1,) +``` +Keeps one dimension as size one (`(1,)`) while performing the same masked reduction. + +
+ +--- + +#### I) `keepdims` and `initial` Together +###### a. 2D — `keepdims=False` (default), baseline below true max +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(keepdims=False, initial=3) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +9 () +``` +Compares all elements against `initial=3`; true maximum (9) wins and returns a scalar. + +
+ +###### b. 2D — keepdims=True, baseline below true max +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) + +res = arr.max(keepdims=True, initial=0) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Global reduction keeps dimensions as `(1, 1)` while returning the same maximum value. + +
+ +###### c. 2D — `keepdims=False`, baseline above all elements +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) + +res = arr.max(keepdims=False, initial=10) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +10 () +``` +Since `initial=10` exceeds every element, it becomes the result (scalar). + +
+ +###### d. 2D — `keepdims=True`, baseline above all elements +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) + +res = arr.max(keepdims=True, initial=10) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[10]] (1, 1) +``` +Returns the baseline while preserving dimensionality for broadcasting. + +
+ +###### e. 1D — `keepdims=False`, baseline smaller than max +```py +import numpy as np +arr = np.array([4, 7, 2]) + +res = arr.max(keepdims=False, initial=5) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +7 () +``` +True maximum (7) beats the baseline and returns a scalar. + +
+ +###### f. 1D — `keepdims=True`, baseline larger than all +```py +import numpy as np +arr = np.array([4, 7, 2]) + +res = arr.max(keepdims=True, initial=10) +print(res, res.shape) +``` +This example results in the following output: +```shell +[10] (1,) +``` +Keeps one dimension of size one; baseline dominates. + +
+ +###### g. 3D — `keepdims=False`, moderate baseline +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(keepdims=False, initial=5) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +23 () +``` +Flattens under the hood; the array’s global maximum (23) exceeds the baseline. + +
+ +###### h. 3D — `keepdims=True`, high baseline +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) + +res = arr.max(keepdims=True, initial=30) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[[30]]] (1, 1, 1) +``` +All elements are below 30, so baseline is returned with dimensions preserved. + +
+ +###### i. Empty array — `keepdims=False` with baseline +```py +import numpy as np +arr = np.array([]) + +res = arr.max(keepdims=False, initial=5) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +5 () +``` +For empty input, the baseline provides a safe scalar result. + +
+ +###### j. Float array — `keepdims=True`, baseline below true max +```py +import numpy as np +arr = np.array([1.2, 3.4, 2.8]) + +res = arr.max(keepdims=True, initial=2.5) +print(res, res.shape) +``` +This example results in the following output: +```shell +[3.4] (1,) +``` +Preserves a `(1,)` shape while selecting the larger of the array max and the baseline. + +
+ +--- + +#### J) `where` and `initial` Together +###### a. Basic mask (no fallback needed) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr > 5 +res = arr.max(where=mask) +print(res) +``` +This example results in the following output: +```shell +9 +``` +Considers only elements greater than 5; at least one match exists, so no baseline is needed. + +
+ +###### b. Full-True mask with low baseline (ignored) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, initial=3) +print(res) +``` +This example results in the following output: +```shell +9 +``` +All elements are valid; the array’s true maximum beats the baseline 3. + +
+ +###### c. Full-True mask with high baseline (dominates) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, initial=12) +print(res) +``` +This example results in the following output: +```shell +12 +``` +Because the baseline exceeds every element, the result is the baseline. + +
+ +###### d. Sparse mask (select specific positions) +```py +import numpy as np +arr = np.array([[10, 20, 30], + [40, 50, 60]]) +mask = np.zeros_like(arr, dtype=bool) +mask[0, 2] = True; mask[1, 1] = True +res = arr.max(where=mask) +print(res) +``` +This example results in the following output: +```shell +50 +``` +Only positions (0,2) and (1,1) are compared; 50 is the maximum among selected entries. + +
+ +###### e. All-False mask with safe fallback +```py +import numpy as np +arr = np.array([[3, 5], + [2, 4]]) +mask = np.zeros_like(arr, dtype=bool) # No positions selected +res = arr.max(where=mask, initial=-1) +print(res) +``` +This example results in the following output: +```shell +-1 +``` +No elements are eligible; the baseline supplies a safe, explicit fallback result. + +
+ +###### f. 1D array with condition and baseline +```py +import numpy as np +arr = np.array([2, 11, 4, 7, 10]) +mask = (arr % 2) == 0 # Evens only +res = arr.max(where=mask, initial=0) +print(res) +``` +This example results in the following output: +```shell +10 +``` +Compares only even numbers; 10 wins over the baseline 0. + +
+ +###### g. Comparison-based mask from another array +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[3, 7, 1], + [5, 0, 8]]) +mask = arr > thr +res = arr.max(where=mask, initial=0) +print(res) +``` +This example results in the following output: +```shell +9 +``` +Mask is built by `arr > thr`; the maximum over permitted positions is 9, exceeding the baseline. + +
+ +###### h. 3D array with thresholded mask +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # 0..23 +mask = arr >= 20 +res = arr.max(where=mask, initial=0) +print(res) +``` +This example results in the following output: +```shell +23 +``` +Only values ≥ 20 are considered; the maximum among them is 23. + +
+ +###### i. Negative inputs with nonnegative floor +```py +import numpy as np +arr = np.array([-8, -4, -6]) +mask = arr > -7 # Select -4 and -6 +res = arr.max(where=mask, initial=0) +print(res) +``` +This example results in the following output: +```shell +0 +``` +The baseline 0 exceeds all selected negatives, ensuring a nonnegative result. + +
+ +###### j. Float array with float baseline +```py +import numpy as np +arr = np.array([1.2, 3.4, 2.8]) +mask = arr > 2.0 +res = arr.max(where=mask, initial=2.5) +print(res) +``` +This example results in the following output: +```shell +3.4 +``` +Considers values greater than 2.0; 3.4 beats the baseline 2.5. + +
+ +###### k. Runtime threshold baseline pattern +```py +import numpy as np +arr = np.array([3, 5, 9, 1, 7]) +threshold = 6 +mask = arr > threshold +res = arr.max(where=mask, initial=threshold) +print(res) +``` +This example results in the following output: +```shell +9 +``` +A common pattern: use the same threshold for both masking and fallback. + +
+ +###### l. Broadcast row mask (2×3) with baseline +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +row_mask = np.array([[True], + [False]]) # Use only first row +res = arr.max(where=row_mask, initial=-1) +print(res) +``` +This example results in the following output: +```shell +8 +``` +Broadcasts a (2,1) mask across columns; the result is the maximum from the first row, compared against the baseline. + +
+ +###### m. Empty array with `initial` +```py +import numpy as np +arr = np.array([], dtype=int) +res = arr.max(where=True, initial=7) +print(res) +``` +This example results in the following output: +```shell +7 +``` +For empty arrays, `initial` provides the only valid result, avoiding errors. + +
+ +###### n. Baseline equal to the true maximum +```py +import numpy as np +arr = np.array([3, 7, 2]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, initial=7) +print(res) +``` +This example results in the following output: +```shell +7 +``` +If `initial` equals the actual maximum, the result remains unchanged but still validated. + +
+ +###### o. Scalar broadcast mask (`where=True`/`where=False`) +```py +import numpy as np +arr = np.array([2, 4, 6]) +res_true = arr.max(where=True, initial=0) +res_false = arr.max(where=False, initial=-5) +print(res_true, res_false) +``` +This example results in the following output: +```shell +6 -5 +``` +A scalar `True` includes all elements; scalar `False` excludes all, so baseline alone is returned. + +
+ +--- + +#### K) `axis` + `out` + `keepdims` (No Errors) +###### a. 1D — `axis=0`, keep dimension, write to 1-D buffer +```py +import numpy as np +arr = np.array([3, 9, 2]) +out_keep = np.empty((1,), dtype=arr.dtype) +arr.max(axis=0, keepdims=True, out=out_keep) +print(out_keep, out_keep.shape) +``` +This example results in the following output: +```shell +[9] (1,) +``` +Row is reduced but dimension is preserved; result goes into a `(1,)` buffer. + +
+ +###### b. 1D — axis=None, keep dimension, write to 1-D buffer +```py +import numpy as np +arr = np.array([4, 1, 7]) +out_keep = np.empty((1,), dtype=arr.dtype) +arr.max(axis=None, keepdims=True, out=out_keep) +print(out_keep, out_keep.shape) +``` +This example results in the following output: +```shell +[7] (1,) +``` +Global maximum with `keepdims=True`; NumPy returns a `(1,)` result that fits the buffer. + +
+ +###### c. 2D — Global max with dims kept; write to `(1,1)` buffer +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +out_11 = np.empty((1, 1), dtype=arr.dtype) +arr.max(axis=None, keepdims=True, out=out_11) +print(out_11, out_11.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Flattens conceptually, but returns a `(1,1)` array because `keepdims=True`. + +
+ +###### d. 2D — `axis=-1` (last axis), keepdims; write to `(2,1)` buffer +```py +import numpy as np +arr = np.array([[3, 1, 7], + [2, 9, 4]]) +out_21 = np.empty((2, 1), dtype=arr.dtype) +arr.max(axis=-1, keepdims=True, out=out_21) +print(out_21, out_21.shape) +``` +This example results in the following output: +```shell +[[7] + [9]] (2, 1) +``` +Per-row maxima using negative axis; reduced axis kept as size one. + +
+ +###### e. 3D — `axis=1`, no keepdims; write to `(2,4)` buffer +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # (2,3,4) +out_24 = np.empty((2, 4), dtype=arr.dtype) +arr.max(axis=1, keepdims=False, out=out_24) +print(out_24.shape); print(out_24) +``` +This example results in the following output: +```shell +(2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` +Reduces the middle axis; results stored directly in a `(2,4)` buffer. + +
+ +###### f. 3D — `axis=2`, keepdims; write to `(2,3,1)` buffer +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +out_231 = np.empty((2, 3, 1), dtype=arr.dtype) +arr.max(axis=2, keepdims=True, out=out_231) +print(out_231.shape); print(out_231) +``` +This example results in the following output: +```shell +(2, 3, 1) +[[[ 3] + [ 7] + [11]] + + [[15] + [19] + [23]]] +``` +Reduces the last axis and preserves it as length-1 for easy broadcasting. + +
+ +###### g. 3D — `axis=1`, keepdims; write to `(2,1,4)` buffer +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +out_214 = np.empty((2, 1, 4), dtype=arr.dtype) +arr.max(axis=1, keepdims=True, out=out_214) +print(out_214.shape); print(out_214) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Keeps the reduced middle axis as size one; buffer matches the preserved shape. + +
+ +###### h. 2D — `axis=0`, keepdims; write into a reshaped view buffer +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +flat = np.empty(3, dtype=arr.dtype) +out_view = flat.reshape(1, 3) # view with kept dim +arr.max(axis=0, keepdims=True, out=out_view) +print(out_view, out_view.shape) +``` +This example results in the following output: +```shell +[[4 9 7]] (1, 3) +``` +Stores column maxima into a reshaped view; demonstrates memory reuse with a kept dimension. + +
+ +###### i. 2D — Global max, `keepdims=False`, float out from int array +```py +import numpy as np +arr = np.array([[2, 11, 4], + [3, 7, 6]], dtype=np.int32) +out_scalar = np.empty((), dtype=np.float64) +arr.max(axis=None, keepdims=False, out=out_scalar) +print(out_scalar, out_scalar.shape) +``` +This example results in the following output: +```shell +11.0 () +``` +Global max written to a 0-D float buffer; safe upcast from int to float. + + +--- + +###### Quick pointers +- For `axis=None` with `keepdims=True`, match `out` to `(1,)*arr.ndim` (e.g., `(1,)`, `(1,1)`, `(1,1,1)`). +- For `keepdims=False`, match `out` to the reduced shape (e.g., `(n,)`, `(m,n)`, or `()` for scalars). + +--- + +
+ +#### L) `axis` + `out` + `where` (No Errors) +###### a. 1D — `axis=0` with even mask → scalar in 0-D `out` +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 +out0 = np.empty((), dtype=arr.dtype) # 0-D buffer for scalar result +arr.max(axis=0, where=mask, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +12 () +``` +Reduces a 1-D array along axis 0, comparing only even entries, and writes the scalar maximum to a 0-D buffer. + +
+ +###### b. 2D — `axis=0` with row broadcast mask `(2,1)`; result into `(ncols,)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +row_mask = np.array([[True],[False]]) # Use only first row across all columns +out_cols = np.empty(3, dtype=arr.dtype) # shape matches (ncols,) +arr.max(axis=0, where=row_mask, out=out_cols) +print(out_cols) +``` +This example results in the following output: +```shell +[2 8 5] +``` +Column-wise reduction that considers only the first row via broadcasting; results go directly into a 1-D buffer. + +
+ +###### c. 2D — `axis=1` with column mask `(1,n)`; result into `(nrows,)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +col_mask = np.array([[True, False, True]]) # choose columns 0 and 2 +out_rows = np.empty(2, dtype=arr.dtype) # shape matches (nrows,) +arr.max(axis=1, where=col_mask, out=out_rows) +print(out_rows) +``` +This example results in the following output: +```shell +[5 9] +``` +Row-wise reduction that ignores masked-off columns; one maximum per row written to `out_rows`. + +
+ +###### d. 2D — `axis=-1` (last axis) with pairwise comparison mask; result into `(nrows,)` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr +out_rows = np.empty(arr.shape[0], dtype=arr.dtype) +arr.max(axis=-1, where=mask, out=out_rows) +print(out_rows) +``` +This example results in the following output: +```shell +[5 9] +``` +Compares each row only where `arr > thr`, then writes each row’s maximum to a 1-D output buffer. + +
+ +###### e. 3D — `axis=0` with layer-select mask `(2,1,1)`; result into `(3,4)` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] # take only first layer +out_034 = np.empty((3,4), dtype=arr.dtype) +arr.max(axis=0, where=mask, out=out_034) +print(out_034.shape); print(out_034) +``` +This example results in the following output: +```shell +(3, 4) +[[ 0 1 2 3] + [ 4 5 6 7] + [ 8 9 10 11]] +``` +Reduces across layers using a broadcast mask that selects only layer 0; writes the 2-D result into `(3,4)`. + +
+ +###### f. 3D — `axis=1` with “at least one True per (batch,col)” mask; result into `(2,4)` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 0, :] = True # ensure a valid candidate along axis=1 +out_24 = np.empty((2,4), dtype=arr.dtype) +arr.max(axis=1, where=mask, out=out_24) +print(out_24.shape); print(out_24) +``` +This example results in the following output: +```shell +(2, 4) +[[ 0 1 2 3] + [12 13 14 15]] +``` +For each batch/column, at least one True ensures a defined maximum; results saved into `(2,4)`. + +
+ +###### g. 3D — `axis=2` with periodic mask `(1,1,4)`; result into `(2,3)` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] # select indices 0 and 2 +out_23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, where=mask, out=out_23) +print(out_23.shape); print(out_23) +``` +This example results in the following output: +```shell +(2, 3) +[[ 2 6 10] + [14 18 22]] +``` +Reduces over the last axis considering only positions 0 and 2; writes per-row maxima to `(2,3)`. + +
+ +###### h. 2D — `axis=0` with reshaped view as `out` and broadcast row mask +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +row_mask = np.array([[True],[False]]) # first row only +flat = np.empty(3, dtype=arr.dtype) # flat storage +out_view = flat.reshape(3) # view shaped for (ncols,) +arr.max(axis=0, where=row_mask, out=out_view) +print(out_view) # backed by the same memory as 'flat' +``` +This example results in the following output: +```shell +[4 6 1] +``` +Stores column-wise masked maxima into a reshaped view, demonstrating safe memory reuse. + +
+ +###### i. 1D — `axis=0` with float `out` upcast and threshold mask +```py +import numpy as np +arr = np.array([1, 6, 3], dtype=np.int32) +mask = arr >= 3 +out0f = np.empty((), dtype=np.float64) # 0-D float buffer +arr.max(axis=0, where=mask, out=out0f) +print(out0f, out0f.shape) +``` +This example results in the following output: +```shell +6.0 () +``` +Computes the masked scalar max and writes it to a 0-D float buffer, exercising safe dtype upcasting. + +
+ +--- + + +**Tip:** For scalar results (1-D with `axis=0`, or any shape with `axis=None`), use a **0-D** `out` buffer (`np.empty((), ...)`). For non-scalar results, match `out.shape` to the **reduced** shape. + +--- + +
+ +#### M) `axis` + `out` + `initial` (No Errors) +###### a. 1D — `axis=0`, scalar `out`, baseline dominates +```py +import numpy as np +arr = np.array([3, 5, 1]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=0, initial=10, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +10 () +``` +Reduces along the only axis; since `initial=10` > array max, the baseline is written to a 0-D buffer. + +
+ +###### b. 1D — `axis=0`, scalar `out`, baseline below true max +```py +import numpy as np +arr = np.array([3, 5, 1]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=0, initial=2, out=out0) +print(out0) +``` +This example results in the following output: +```shell +5 +``` +The true maximum (5) beats the baseline and is stored in-place. + +
+ +###### c. 2D — `axis=0` (columns), 1-D `out`, per-column baseline +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +# Column maxima are [7,4,6]; baseline lifts only columns below 5 +out_cols = np.empty(3, dtype=arr.dtype) +arr.max(axis=0, initial=5, out=out_cols) +print(out_cols) +``` +This example results in the following output: +```shell +[7 5 6] +``` +Writes column-wise results; each entry is `max(column_max, 5)`. + +
+ +###### d. 2D — `axis=1` (rows), 1-D `out`, high baseline +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +out_rows = np.empty(2, dtype=arr.dtype) +arr.max(axis=1, initial=8, out=out_rows) +print(out_rows) +``` +This example results in the following output: +```shell +[8 8] +``` +Each row’s maximum is compared with 8; both rows are raised to the baseline. + +
+ +###### e. 2D — `axis=-1` (last axis), 1-D `out`, moderate baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +out_rows = np.empty(2, dtype=arr.dtype) +arr.max(axis=-1, initial=6, out=out_rows) +print(out_rows) +``` +This example results in the following output: +```shell +[9 6] +``` +Row maxima are compared with 6; the second row is lifted to 6. + +
+ +###### f. 3D — `axis=0` (across layers), 2-D `out`, baseline fills low cells +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +out_2d = np.empty((3,4), dtype=arr.dtype) +arr.max(axis=0, initial=15, out=out_2d) +print(out_2d) +``` +This example results in the following output: +```shell +[[15 15 15 15] + [16 17 18 19] + [20 21 22 23]] +``` +For each (row,col), result is `max(max_over_layers, 15)`; only early positions are lifted + +
+ +###### g. 3D — `axis=1` (middle), 2-D `out`, baseline below slice max +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (2,3,4) +out_24 = np.empty((2,4), dtype=arr.dtype) +arr.max(axis=1, initial=5, out=out_24) +print(out_24) +``` +This example results in the following output: +```shell +[[ 8 9 10 11] + [20 21 22 23]] +``` +Reduces the middle axis; true maxima exceed the baseline. + +
+ +###### h. 3D — `axis=2` (last), 2-D `out`, baseline partially dominates +```py +import numpy as np +arr = np.array([[[0,1,2,3], + [4,5,6,7], + [8,9,10,11]], + [[12,13,14,15], + [16,17,18,19], + [20,21,22,23]]]) +out_23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, initial=9, out=out_23) +print(out_23) +``` +This example results in the following output: +```shell +[[ 9 9 11] + [15 19 23]] +``` +Per row, result is `max(row_max_along_depth, 9)`; early rows lift to 9 where needed. + +
+ +###### i. Float baseline & safe upcast — int array → float `out` +```py +import numpy as np +arr = np.array([[1, 6, 3], + [2, 5, 4]], dtype=np.int32) +out_rows = np.empty(2, dtype=np.float64) +arr.max(axis=1, initial=4.5, out=out_rows) +print(out_rows, out_rows.dtype) +``` +This example results in the following output: +```shell +[6. 5.] float64 +``` +Row maxima are stored into a float buffer; NumPy safely upcasts the integer results. + +
+ +###### j. Writing into a reshaped view buffer (memory reuse) +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +flat = np.empty(3, dtype=arr.dtype) +out_view = flat.reshape(3) # matches reduced shape for axis=0 +arr.max(axis=0, initial=5, out=out_view) +print(out_view, "(backed by 'flat')") +``` +This example results in the following output: +```shell +[5 9 7] (backed by 'flat') +``` +Computes column-wise results with a baseline and writes into a reshaped view for efficient reuse. + +
+ +###### k. Reusing the same `out` buffer with different `initial` values +```py +import numpy as np +A = np.array([[2, 8, 5], + [4, 1, 9]]) +B = np.array([[3, 7, 6], + [2,10, 4]]) +buf = np.empty(3, dtype=A.dtype) + +A.max(axis=0, initial=0, out=buf); print("A:", buf) +B.max(axis=0, initial=8, out=buf); print("B:", buf) +``` +This example results in the following output: +```shell +A: [4 8 9] +B: [8 10 8] +``` +The same 1-D buffer is reused; the baseline affects each column independently per call. + +
+ +--- + +#### N) `axis` + `keepdims` + `where` (No Errors) +###### a. 2D — `axis=0`, `keepdims=True`, broadcast **row mask** +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # use only first row +res = arr.max(axis=0, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[2 8 5]] (1, 3) +``` +Column-wise maxima are computed only from the first row; `keepdims=True` preserves shape `(1, 3)`. + +
+ +###### b. 2D — `axis=1`, `keepdims=True`, broadcast column mask +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True, False, True]]) # choose cols 0 and 2 +res = arr.max(axis=1, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[5] + [9]] (2, 1) +``` +Row-wise maxima consider only the masked columns; the reduced axis is kept as size one. + +
+ +###### c. 2D — `axis=-1`, `keepdims=True`, pairwise comparison mask +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr # [[T,F,T],[F,T,T]] +res = arr.max(axis=-1, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[5] + [9]] (2, 1) +``` +Compares each row only where `arr > thr`; `axis=-1` reduces per row and keeps the last axis. + +
+ +###### d. 1D — `axis=0`, `keepdims=True`, even-only mask +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 +res = arr.max(axis=0, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[12] (1,) +``` +Reduction along the only axis with a mask; dimension is preserved as `(1,)`. + +
+ +###### e. 3D — `axis=0`, `keepdims=True`, layer-select mask +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] # select layer 0 only +res = arr.max(axis=0, keepdims=True, where=mask) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(1, 3, 4) +[[[ 0 1 2 3] + [ 4 5 6 7] + [ 8 9 10 11]]] +``` +Reduces across layers but only considers the first layer; `keepdims=True` retains a `(1,3,4)` shape. + +
+ +###### f. 3D — `axis=1`, `keepdims=True`, mask guarantees one True per slice +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 0, :] = True # ensure at least one True along axis=1 +res = arr.max(axis=1, keepdims=True, where=mask) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[ 0 1 2 3]] + + [[12 13 14 15]]] +``` +Row-of-slices mask ensures a defined maximum per (batch, col); the middle axis is kept as size one. + +
+ +###### g. 3D — `axis=2`, `keepdims=True`, periodic last-axis mask +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = np.array([True, False, True, False])[None, None, :] # pick indices 0 and 2 +res = arr.max(axis=2, keepdims=True, where=mask) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3, 1) +[[[ 2] + [ 6] + [10]] + + [[14] + [18] + [22]]] +``` +Reduces over the last axis while only considering positions 0 and 2; preserves the last axis as length one. + +
+ +###### h. 3D — `axis=2`, `keepdims=True`, threshold mask +```py +import numpy as np +arr = np.arange(24).reshape(2, 3, 4) +mask = arr >= 2 +res = arr.max(axis=2, keepdims=True, where=mask) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3, 1) +[[[ 3] + [ 7] + [11]] + + [[15] + [19] + [23]]] +``` +For each row across depth, only elements ≥2 are considered; keeping dimensions yields `(2,3,1)`. + +
+ +###### i. 2D — `axis=0`, `keepdims=True`, full-True mask +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(axis=0, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[4 8 9]] (1, 3) +``` +Equivalent to a normal column-wise max but retains the reduced dimension for downstream broadcasting. + +
+ +###### j. 2D — `axis=1`, `keepdims=True`, row-specific mixed mask +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +mask = np.array([[False, True, False], + [True, False, True]]) # each row has at least one True +res = arr.max(axis=1, keepdims=True, where=mask) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[8] + [9]] (2, 1) +``` +Each row uses a different mask pattern; `keepdims=True` returns a column vector suitable for broadcasting. + +
+ + +### Coverage notes +- **Axes:** `0`, `1`, `2`, and `-1` +- **Dims:** 1-D, 2-D, 3-D +- **Masks:** full-True, threshold, pairwise comparison, periodic selection, and broadcast row/column/layer masks +- **Validity:** Every reduced slice has at least one `True`, so no errors are raised. + +--- + +#### O) `axis` + `keepdims` + `initial` (No Errors) +###### a. 2D — `axis=0`, `keepdims=True`, baseline dominates +```py +import numpy as np +arr = np.array([3, 5, 1]) +res = arr.max(axis=0, keepdims=False, initial=10) +print(res, res.shape) +``` +This example results in the following output: +```shell +10 () +``` +Baseline exceeds all elements, so the scalar result is `10`. + +
+ +###### b. 1D — `axis=0`, `keepdims=True`, baseline below true max +```py +import numpy as np +arr = np.array([3, 5, 1]) +res = arr.max(axis=0, keepdims=True, initial=2) +print(res, res.shape) +``` +This example results in the following output: +```shell +[5] (1,) +``` +Keeps one dimension; result is the true max `[5]`. + +
+ +###### c. 2D — `axis=0`, `keepdims=True`, per-column baseline lift +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +res = arr.max(axis=0, keepdims=True, initial=5) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[7 5 6]] (1, 3) +``` +Column maxima are compared with `5`; only the middle column is lifted. + +
+ +###### d. 2D — `axis=1`, `keepdims=True`, high baseline dominates rows +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +res = arr.max(axis=1, keepdims=True, initial=8) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[8] + [8]] (2, 1) +``` +Row maxima are overridden by the higher baseline `8`. + +
+ +###### e. 2D — `axis=-1`, `keepdims=True`, mixed baseline effect +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +res = arr.max(axis=-1, keepdims=True, initial=6) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9] + [6]] (2, 1) +``` +Per row: take `max(row_max, 6)`; second row lifts to `6`. + +
+ +###### f. 3D — `axis=0`, `keepdims=True`, early cells filled by baseline +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +res = arr.max(axis=0, keepdims=True, initial=15) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(1, 3, 4) +[[[15 15 15 15] + [16 17 18 19] + [20 21 22 23]]] +``` +Across layers: each (row,col) is `max(layer_max, 15)`. + +
+ +###### g. 3D — `axis=1`, `keepdims=False`, baseline below slice maxima +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +res = arr.max(axis=1, keepdims=False, initial=5) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` +Middle-axis reduction; true maxima exceed the baseline. + +
+ +###### h. 3D — `axis=2`, `keepdims=True`, baseline partially dominates +```py +import numpy as np +arr = np.array([[[0,1,2,3], + [4,5,6,7], + [8,9,10,11]], + [[12,13,14,15], + [16,17,18,19], + [20,21,22,23]]]) +res = arr.max(axis=2, keepdims=True, initial=9) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3, 1) +[[[ 9] + [ 9] + [11]] + + [[15] + [19] + [23]]] +``` +Per row across depth: `max(depth_max, 9)`; early rows lift to `9`. + +
+ +###### i. Empty along reduced axis — shape `(0,3)`, `axis=0`, `keepdims=False` +```py +import numpy as np +arr = np.empty((0,3), dtype=int) +res = arr.max(axis=0, keepdims=False, initial=5) +print(res, res.shape) +``` +This example results in the following output: +```shell +[5 5 5] (3,) +``` +Empty reduction along columns returns the baseline repeated per column. + +
+ +###### j. Empty 1D —`axis=0`, `keepdims=True` +```py +import numpy as np +arr = np.array([], dtype=int) +res = arr.max(axis=0, keepdims=True, initial=7) +print(res, res.shape) +``` +This example results in the following output: +```shell +[7] (1,) +``` +Empty 1D with `keepdims=True` returns a `(1,)` array containing the baseline. + +
+ +###### k. Empty 3D in reduced axis — shape `(2,0,4)`, `axis=1`, `keepdims=True` +```py +import numpy as np +arr = np.empty((2,0,4), dtype=int) +res = arr.max(axis=1, keepdims=True, initial=9) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[9 9 9 9]] + + [[9 9 9 9]]] +``` +For each (batch,col), no elements exist, so the retained axis is filled with the baseline. + +
+ +###### l. Baseline equals true max — shape & value preserved +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +res = arr.max(axis=0, keepdims=True, initial=9) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9 9 9]] (1, 3) +``` +Column maxima `[4,8,9]` become `max(col_max, 9)`, yielding `[9,9,9]` with kept dimension. + +
+ + +--- + +### Coverage notes +- **Axes:** `0`, `1`, `2`, `-1` across **1D–3D** arrays +- **`keepdims`:** `True` and `False` +- **`initial`:** lower than max, greater than all, **equal to max**, and **empty-axis** reductions (valid due to baseline) + +All examples are non-error, runnable, and focused on this specific parameter trio. + +--- + +#### P) `axis` + `where` + `initial` (No Errors) +###### a. 1D — `axis=0`, even-only mask, baseline as floor +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # keep 10, 12 +res = arr.max(axis=0, where=mask, initial=5) +print(res) +``` +This example results in the following output: +```shell +12 +``` +Compares only even entries; `initial=5` is ignored because the masked max (12) is larger. + +
+ +###### b. 1D — `axis=0`, all-False mask, result is baseline +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) # no candidates +res = arr.max(axis=0, where=mask, initial=9) +print(res) +``` +This example results in the following output: +```shell +9 +``` +No elements pass the mask; `initial` supplies a safe, defined result. + +
+ +###### c. 2D — `axis=0`, broadcast row mask, baseline fills weak columns +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 3]]) +row_m = np.array([[True],[False]]) # use only first row +res = arr.max(axis=0, where=row_m, initial=6) +print(res) +``` +This example results in the following output: +```shell +[6 8 6] +``` +Column-wise reduction considers only the first row; columns below 6 are lifted by the baseline. + +
+ +###### d. 2D — `axis=0`, value mask can empty slices; baseline protects +```py +import numpy as np +arr = np.array([[2, 9, 5], + [3, 4, 1]]) +mask = arr > 6 # [[F,T,F],[F,F,F]] +res = arr.max(axis=0, where=mask, initial=7) +print(res) +``` +This example results in the following output: +```shell +[7 9 7] +``` +Only column 1 has a candidate; other columns take the baseline. + +
+ +###### e. 2D — `axis=1`, column mask, baseline overrides some rows +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 3]]) +col_m = np.array([[True, False, True]]) # keep cols 0 and 2 +res = arr.max(axis=1, where=col_m, initial=6) +print(res) +``` +This example results in the following output: +```shell +[6 6] +``` +Per row, only the selected columns are considered; both row maxima are below 6, so baseline dominates. + +
+ +###### f. 2D — `axis=-1`, pairwise comparison mask, baseline as floor +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr # [[T,F,T],[F,T,T]] +res = arr.max(axis=-1, where=mask, initial=6) +print(res) +``` +This example results in the following output: +```shell +[6 9] +``` +Row-wise, masked maxima are compared against 6; first row lifts to 6, second remains 9. + +
+ +###### g. 3D — `axis=0`, layer-select mask, baseline fills early cells +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] # use layer 0 only +res = arr.max(axis=0, where=mask, initial=5) +print(res.shape); print(res) +``` +This example results in the following output: +``` +(3, 4) +[[ 5 5 5 5] + [ 5 5 6 7] + [ 8 9 10 11]] +``` +Reduces across layers but only reads layer 0; positions below 5 are lifted to baseline. + +
+ +###### h. 3D — `axis=1`, ensure at least one True per (batch,col) via baseline +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) # all False would be invalid +mask[:, 1, 2] = True # single True per (batch,col) at depth index 2 +res = arr.max(axis=1, where=mask, initial=10) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 4) +[[10 10 14 10] + [10 10 22 10]] +``` +For each (batch,col), only one depth element is considered; `initial=10` fills other columns if that element is <10. + +
+ +###### i. 3D — `axis=2`, periodic mask, baseline partially dominates +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] # keep indices 0 and 2 +res = arr.max(axis=2, where=mask, initial=9) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3) +[[ 9 9 11] + [13 17 21]] +``` +For each row across depth, compare masked values with baseline 9; early rows lift to 9 where needed. + +
+ +###### j. 3D — `axis=2`, mask all False for one row; baseline saves it +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +mask[0, 0, :] = True # only row (0,0, :) has candidates +res = arr.max(axis=2, where=mask, initial=100) +print(res.shape); print(res) +``` +This example results in the following output: +```shell +(2, 3) +[[ 100 100 100] + [ 3 100 100]] +``` +Only the first row/first slice has candidates; all other positions take the baseline, avoiding errors cleanly. + +
+ + +--- + +### Coverage notes +- **Axes:** `0`, `1`, `2`, `-1` across **1D–3D** arrays +- **Masks:** same-shape, broadcast row/column, pairwise comparison, periodic, and even **all-False** (made valid via `initial`) +- **`initial`:** used as a per-slice fallback/floor to guarantee defined results in every case + +--- + +
+ +#### Q) `out` + `keepdims` + `where` (No Errors) +###### a. 1D — Scalar result in 0-D `out` (keepdims=False) +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # pick 10,12 + +out0 = np.empty((), dtype=arr.dtype) # 0-D buffer +arr.max(where=mask, keepdims=False, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +12 () +``` +Masked global maximum written to a scalar (0-D) buffer. + +
+ +###### b. 1D — Keep dimension; write to shape (1,) `out` +```py +import numpy as np +arr = np.array([2, 11, 4, 7, 10]) +mask = arr > 5 + +out1 = np.empty((1,), dtype=arr.dtype) # (1,) for keepdims on 1-D +arr.max(where=mask, keepdims=True, out=out1) +print(out1, out1.shape) +``` +This example results in the following output: +```shell +[11] (1,) +``` +Global masked max with `keepdims=True` preserves one dimension. + +
+ +###### c. 2D — Scalar result; threshold mask; 0-D `out` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr >= 6 + +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, keepdims=False, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +9 () +``` +Masks values ≥6 across the whole array; writes the single maximum into a scalar buffer. + +###### d. 2D — Keep dims as (1,1); full-True mask; `(1,1)` `out` +```py +import numpy as np +arr = np.array([[1, 3, 5], + [2, 6, 4]]) +mask = np.ones_like(arr, dtype=bool) + +out11 = np.empty((1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, out=out11) +print(out11, out11.shape) +``` +This example results in the following output: +```shell +[[6]] (1, 1) +``` +Equivalent to normal global max, but returns a `(1,1)` array for easy broadcasting. + +
+ +###### e. 2D — Keep dims; row-broadcast mask selects first row only +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # shape (2,1), broadcasts over columns + +out11 = np.empty((1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, out=out11) +print(out11, out11.shape) +``` +This example results in the following output: +```shell +[[8]] (1, 1) +``` +Global reduction considers only the first row via broadcasting; result keeps `(1,1)` shape. + +
+ +###### f. 3D — Scalar result; comparison-derived mask; 0-D float `out` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # 0..23 +thr = 15 +mask = arr > thr + +out0f = np.empty((), dtype=np.float64) # upcast target +arr.max(where=mask, keepdims=False, out=out0f) +print(out0f, out0f.shape) +``` +This example results in the following output: +```shell +23.0 () +``` +Compares against a scalar threshold; writes the masked maximum to a 0-D float buffer (safe upcast). + +
+ +###### g. 3D — Keep dims as `(1,1,1)`; periodic last-axis mask +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] # pick indices 0 and 2 + +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, out=out111) +print(out111, out111.shape) +``` +This example results in the following output: +```shell +[[[22]]] (1, 1, 1) +``` +Global reduction but restricted to positions 0 and 2 across the last axis; keeps all dims as size one. + +
+ +###### h. 2D — Keep dims; write into a reshaped view buffer +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +mask = arr % 2 == 1 # odd elements only + +flat = np.empty(1, dtype=arr.dtype) # storage of size 1 +out11v = flat.reshape(1,1) # view with shape (1,1) +arr.max(where=mask, keepdims=True, out=out11v) +print(out11v, out11v.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Writes masked global max into a `(1,1)` view backed by a flat buffer. + +
+ +###### i. 3D — Keep dims; layer-broadcast mask selects first layer +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False])[:, None, None] # use only layer 0 + +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, out=out111) +print(out111, out111.shape) +``` +This example results in the following output: +```shell +[[[11]]] (1, 1, 1) +``` +Global reduction limited to the first layer; dimensionality preserved as `(1,1,1)`. + +
+ +###### j. Overwriting a prefilled scalar buffer (0-D `out`) +```py +import numpy as np +arr = np.array([[5, 3, 9], + [2, 7, 1]]) +mask = arr >= 5 + +out0 = np.array(-999, dtype=arr.dtype) # sentinel +arr.max(where=mask, keepdims=False, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +The masked maximum overwrites the prefilled 0-D buffer in place. + +
+ +--- + +### Notes & Tips +- With **no `axis`**, the reduction is **global**. +- If `keepdims=False`, use a **0-D** `out`; if `keepdims=True`, make `out.shape == (1,)*arr.ndim`. +- Masks may **broadcast** (e.g., `(2,1)` on a `(2,3)` array) but must allow **at least one True overall**. + +--- + +
+ +#### R) `out` + `keepdims` + `initial` (No Errors) +###### a. 1D — Scalar result; `keepdims=False`; baseline below true max +```py +import numpy as np +arr = np.array([3, 5, 1]) +out0 = np.empty((), dtype=arr.dtype) # 0-D buffer +arr.max(keepdims=False, initial=2, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +5 () +``` +Global maximum beats the baseline; written into a scalar buffer. + +
+ +###### b. 1D — Keep dimension; out shape (1,); baseline dominates +```py +import numpy as np +arr = np.array([3, 5, 1]) +out1 = np.empty((1,), dtype=arr.dtype) +arr.max(keepdims=True, initial=10, out=out1) +print(out1, out1.shape) +``` +This example results in the following output: +```shell +[10] (1,) +``` +Preserves one dimension; baseline exceeds all elements. + +
+ +###### c. 2D — `keepdims=True`; `(1,1)` `out`; moderate baseline +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +out11 = np.empty((1,1), dtype=arr.dtype) +arr.max(keepdims=True, initial=6, out=out11) +print(out11, out11.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Global max (9) wins over baseline 6; dims retained for broadcasting. + +
+ +###### d. 2D — `keepdims=False`; 0-D `out`; high baseline dominates +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(keepdims=False, initial=10, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +10 () +``` +Returns the baseline since it’s greater than every element. + +
+ +###### e. 3D — `keepdims=True`; `(1,1,1)` `out`; baseline below true max +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # 0..23 +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(keepdims=True, initial=5, out=out111) +print(out111, out111.shape) +``` +This example results in the following output: +```shell +[[[23]]] (1, 1, 1) +``` +Global maximum (23) exceeds the baseline; all dims kept as size one. + +
+ +###### f. 3D — `keepdims=True`; float `out`; float baseline +```py +import numpy as np +arr = np.arange(24, dtype=np.int32).reshape(2,3,4) +out111 = np.empty((1,1,1), dtype=np.float64) +arr.max(keepdims=True, initial=22.5, out=out111) +print(out111, out111.dtype) +``` +This example results in the following output: +```shell +[[[23.]]] float64 +``` +Safe upcast: int array reduced into a float buffer with a float baseline. + +
+ +###### g. Overwrite a prefilled 0-D buffer in place +```py +import numpy as np +arr = np.array([5, 3, 9]) +out0 = np.array(-999, dtype=arr.dtype) # sentinel +arr.max(keepdims=False, initial=0, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +The computed maximum overwrites the existing scalar value. + +
+ +###### h. Empty 1D input — `keepdims=False`; baseline supplies result +```py +import numpy as np +arr = np.array([], dtype=int) +out0 = np.empty((), dtype=int) +arr.max(keepdims=False, initial=7, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +7 () +``` +No elements; reduction returns the baseline in a scalar buffer. + +
+ +###### i. Empty 2D input — `keepdims=True`; `(1,1)` `out` +```py +import numpy as np +arr = np.empty((0,0), dtype=int) +out11 = np.empty((1,1), dtype=int) +arr.max(keepdims=True, initial=5, out=out11) +print(out11, out11.shape) +``` +This example results in the following output: +```shell +[[5]] (1, 1) +``` +With no data, baseline fills the kept-dims result. + +
+ +###### j. Write into a reshaped view buffer (memory reuse) +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +store = np.empty(1, dtype=arr.dtype) # single cell +out11 = store.reshape(1,1) # view -> (1,1) +arr.max(keepdims=True, initial=3, out=out11) +print(out11, out11.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Masked global max not used; still demonstrates writing to a reshaped single-cell view. + +
+ +###### k. Reuse the same `out` with different arrays/baselines +```py +import numpy as np +A = np.array([2, 9, 1]) +B = np.array([3, 5, 7]) +out0 = np.empty((), dtype=int) + +A.max(keepdims=False, initial=0, out=out0); print("A:", out0) +B.max(keepdims=False, initial=10, out=out0); print("B:", out0) +``` +This example results in the following output: +```shell +A: 9 +B: 10 +``` +One scalar buffer reused; first call returns true max, second returns the dominating baseline. + +
+ + +--- + +### Notes & Tips +- With **no `axis`**, reduction is **global**. +- Use **0-D `out`** for `keepdims=False`; use **`(1,)*arr.ndim`** for `keepdims=True`. +- `initial` guarantees a defined result for empty inputs (or when enforcing a floor). + +--- + +
+ +#### S) `out` + `where` + `initial` (No Errors) +###### a. 1D — Even-only mask; baseline lower than true masked max +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # 10, 12 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=5, out=out0) +print(out0) +``` +This example results in the following output: +```shell +12 +``` +Compares only even entries; 12 beats the baseline and is written to the scalar buffer. + +
+ +###### b. 1D — All-False mask; result is baseline +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=9, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +No candidate passes the mask, so the baseline is returned. + +
+ +###### c. 2D — Row-broadcast mask (first row only); baseline as floor +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # (2,1) → first row only +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=6, out=out0) +print(out0) +``` +This example results in the following output: +```shell +8 +``` +Global masked reduction sees only the first row; 8 exceeds the baseline. + +
+ +###### d. 2D — Pairwise comparison mask; float `out` and float baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [3, 4, 1]], dtype=np.int32) +thr = np.array([[3, 7, 6], + [2, 5, 0]]) +mask = arr > thr # [[F,T,F],[T,F,T]] +out0f = np.empty((), dtype=np.float64) +arr.max(where=mask, initial=4.5, out=out0f) +print(out0f) +``` +This example results in the following output: +```shell +9.0 +``` +Only permitted positions are compared; result is safely upcast to float in the output. + +
+ +###### e. 2D — Sparse mask (specific cells); baseline between candidates +```py +import numpy as np +arr = np.array([[10, 20, 30], + [40, 50, 60]]) +mask = np.zeros_like(arr, dtype=bool) +mask[0,2] = True; mask[1,1] = True # pick 30 and 50 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=45, out=out0) +print(out0) +``` +This example results in the following output: +```shell +50 +``` +Chooses between 30, 50, and baseline 45; 50 wins. + +
+ +###### f. 3D — Threshold mask; baseline dominates +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # 0..23 +mask = arr >= 20 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=24, out=out0) +print(out0) +``` +This example results in the following output: +```shell +24 +``` +Masked candidates’ maximum is 23; baseline 24 is larger and becomes the result. + +
+ +###### g. 3D — Layer-select mask (first layer only); baseline below candidate +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False])[:, None, None] # layer 0 only +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=5, out=out0) +print(out0) +``` +This example results in the following output: +```shell +11 +``` +Reduces over only layer 0 (values 0–11); the largest allowed is 11, beating the baseline. + +
+ +###### h. 3D — All-False mask; baseline provides safe fallback +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, initial=100, out=out0) +print(out0) +``` +This example results in the following output: +```shell +100 +``` +With no eligible elements, the specified baseline is returned. + +
+ +###### i. Reuse the same scalar `out` across calls with different masks/baselines +```py +import numpy as np +a = np.array([2, 9, 1]) +b = np.array([3, 5, 7]) + +out0 = np.empty((), dtype=int) + +a_mask = a > 3 +a.max(where=a_mask, initial=0, out=out0); print("a:", out0) + +b_mask = b % 2 == 0 +b.max(where=b_mask, initial=10, out=out0); print("b:", out0) +``` +This example results in the following output: +```shell +a: 9 +b: 10 +``` +The same 0-D buffer is overwritten in place across different arrays and masks. + +
+ +###### j. Overwrite a prefilled 0-D buffer +```py +import numpy as np +arr = np.array([[5, 3, 9], + [2, 7, 1]]) +mask = arr >= 5 +out0 = np.array(-999, dtype=arr.dtype) # sentinel +arr.max(where=mask, initial=0, out=out0) +print(out0) +``` +This example results in the following output: +```shell +9 +``` +Masked maximum replaces the prefilled sentinel in the scalar output. + +
+ +###### k. Functional form `np.max` with `out`, `where`, `initial` +```py +import numpy as np +arr = np.array([2, 11, 4]) +mask = arr > 3 +out0 = np.empty((), dtype=arr.dtype) +np.max(arr, where=mask, initial=8, out=out0) +print(out0) +``` +This example results in the following output: +```shell +11 +``` +`np.max` mirrors `ndarray.max`; with masking and a baseline, the largest permitted value is returned into `out0`. + +
+ + +--- + +### Tips +- With **no `axis`**, this is a **global** reduction. +- Use a **0-D scalar `out`** (`np.empty((), dtype=...)`) for the result container. +- `initial` guarantees a defined result when the mask is **all False** or very restrictive. + +--- + +
+ +#### T) `keepdims` + `where` + `initial` (No Errors) +###### a. 1D — Partial mask; `keepdims=False`; baseline ignored +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # 10, 12 +res = arr.max(where=mask, keepdims=False, initial=5) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +12 () +``` +Only even elements are compared; masked max 12 exceeds the baseline. + +
+ +###### b. 1D — Same mask; `keepdims=True`; baseline dominates +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 +res = arr.max(where=mask, keepdims=True, initial=15) +print(res, res.shape) +``` +This example results in the following output: +```shell +[15] (1,) +``` +Keeps one dimension; baseline 15 exceeds masked candidates. + +
+ +###### c. 1D — All-False mask; `keepdims=True` +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) +res = arr.max(where=mask, keepdims=True, initial=9) +print(res, res.shape) +``` +This example results in the following output: +```shell +[9] (1,) +``` +No elements selected; baseline supplies a defined result with dimension preserved. + +
+ +###### d. 2D — Row-broadcast mask (first row only); `keepdims=False` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # (2,1) → first row only +res = arr.max(where=mask, keepdims=False, initial=6) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +8 () +``` +Global masked reduction considers only the first row; 8 beats the baseline. + +
+ +###### e. 2D — Full-True mask; `keepdims=True`; baseline equals global max +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.ones_like(arr, dtype=bool) +res = arr.max(where=mask, keepdims=True, initial=9) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +All elements considered; with baseline equal to the true max, value and kept shape are maintained. + +
+ +###### f. 2D — Pairwise comparison mask; `keepdims=True`; mid baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [3, 4, 1]]) +thr = np.array([[3, 7, 6], + [2, 5, 0]]) +mask = arr > thr # [[F,T,F],[T,F,T]] +res = arr.max(where=mask, keepdims=True, initial=6) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Only permitted positions are compared; masked max 9 wins over baseline 6. + +
+ +###### g. 3D — Threshold mask; `keepdims=False`; high baseline dominates +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # 0..23 +mask = arr >= 20 +res = arr.max(where=mask, keepdims=False, initial=25) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +25 () +``` +Masked candidates’ maximum is 23; baseline 25 becomes the result. + +
+ +###### h. 3D — Periodic last-axis mask; `keepdims=True`; baseline below masked max +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] +res = arr.max(where=mask, keepdims=True, initial=9) +print(res, res.shape) +``` +This example results in the following output: +```shell +[[[22]]] (1, 1, 1) +``` +Global reduction considers only indices 0 and 2 along the last axis; masked max 22 exceeds baseline. + +
+ +###### i. Negative values; partial mask; nonnegative floor; `keepdims=False` +```py +import numpy as np +arr = np.array([-8, -4, -6]) +mask = arr > -7 # selects -4 and -6 +res = arr.max(where=mask, keepdims=False, initial=0) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +0 () +``` +Baseline 0 exceeds selected negatives, ensuring a nonnegative result. + +
+ +###### j. Empty array; `keepdims=True` with baseline +```py +import numpy as np +arr = np.array([], dtype=int) +mask = np.array([], dtype=bool) +res = arr.max(where=mask, keepdims=True, initial=7) +print(res, res.shape) +``` +This example results in the following output: +```shell +[7] (1,) +``` +Empty input with empty mask; baseline supplies a safe value while keeping one dimension. + +
+ +###### k. Data-driven mask (relative to mean); `keepdims=True` +```py +import numpy as np +arr = np.array([1.2, 3.4, 2.8, 0.9]) +mask = arr > arr.mean() # select above-average values +res = arr.max(where=mask, keepdims=True, initial=2.5) +print(res, res.shape) +``` +This example results in the following output: +```shell +[3.4] (1,) +``` +Only above-mean elements are considered; masked max exceeds the baseline. + +
+ +###### l. Float array; float baseline; `keepdims=False` +```py +import numpy as np +arr = np.array([[1.2, 3.4], + [2.8, 0.5]]) +mask = arr >= 1.0 +res = arr.max(where=mask, keepdims=False, initial=3.0) +print(res, np.shape(res)) +``` +This example results in the following output: +```shell +3.4 () +``` +Masked global maximum 3.4 is greater than the baseline 3.0. + +
+ +--- + +### Tips +- With **no `axis`**, this performs a **global masked reduction**. +- `keepdims=True` preserves the array’s dimensionality, returning shape `(1,)*arr.ndim` for consistent broadcasting. +- `initial` ensures a valid result when the `where` mask is **all False** or the array is **empty**. + +--- + +
+ +#### U) `axis` + `out` + `keepdims` + `where` (No Errors, Non-Duplicates) +###### a. 2D — `axis=-1`, keepdims=False, column mask → `(nrows,)` out +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 6, 9]]) +mask = np.array([[True, False, True]]) # choose cols 0 and 2 +outR = np.empty(arr.shape[0], dtype=arr.dtype) +arr.max(axis=-1, where=mask, keepdims=False, out=outR) +print(outR, outR.shape) +``` +This example results in the following output: +```shell +[5 9] (2,) +``` +Reduces per row over selected columns; writes one value per row. + +
+ +###### b. 2D — `axis=1`, `keepdims=True`, non-contiguous `out` view (transpose) +```py +import numpy as np +arr = np.array([[4, 1, 9], + [2, 7, 3]]) +mask = arr >= 2 +buf = np.empty((3,2), dtype=arr.dtype) +outV = buf.T # shape (2,3), non-contiguous view +arr.max(axis=1, where=mask, keepdims=True, out=outV) +print(outV, outV.flags['C_CONTIGUOUS'], outV.shape) +``` +This example results in the following output: +```shell +[[9 7 9] + [9 7 9]] False (2, 3) +``` +Row-wise masked max kept as `(2,1)` but broadcast into a non-contiguous view with matching shape. + +
+ +###### c. 3D — `axis=-2` (middle), keepdims=True, row-broadcast mask `(1,3,1)` → `(2,1,4)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (batch, rows, cols) +mask = np.array([[True, False, True]])[:, :, None] # (1,3,1) +out214 = np.empty((2,1,4), dtype=arr.dtype) +arr.max(axis=-2, where=mask, keepdims=True, out=out214) +print(out214.shape); print(out214) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Reduces over rows using a broadcast mask; reduced axis kept as size one. + +
+ +###### d. 3D — `axis=0`, keepdims=False, layer-select mask, dtype upcast to float +```py +import numpy as np +arr = np.arange(24, dtype=np.int32).reshape(2,3,4) +mask = np.array([True, False])[:, None, None] # first layer only +out34 = np.empty((3,4), dtype=np.float64) +arr.max(axis=0, where=mask, keepdims=False, out=out34) +print(out34.dtype, out34.shape); print(out34) +``` +This example results in the following output: +```shell +float64 (3, 4) +[[ 0. 1. 2. 3.] + [ 4. 5. 6. 7.] + [ 8. 9. 10. 11.]] +``` +Reduces across layers restricted to layer 0; results are safely upcast when written. + +
+ +###### e. 3D — `axis=2`, keepdims=False, periodic last-axis mask → `(batch,rows)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] +out23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, where=mask, keepdims=False, out=out23) +print(out23.shape); print(out23) +``` +This example results in the following output: +```shell +(2, 3) +[[ 2 6 10] + [14 18 22]] +``` +Reduces along the last axis considering only indices 0 and 2; one value per row. + +
+ +###### f. 4D — `axis=0` (batch), keepdims=True, scalar-True mask → `(1,3,4,2)` out +```py +import numpy as np +arr = np.arange(48).reshape(2,3,4,2) # (batch, x, y, z) +mask = True # scalar mask (all allowed) +out1342 = np.empty((1,3,4,2), dtype=arr.dtype) +arr.max(axis=0, where=mask, keepdims=True, out=out1342) +print(out1342.shape); print(out1342[0, :2, :2, :]) +``` +This example results in the following output: +```shell +(1, 3, 4, 2) +[[[ 2 3] + [ 6 7]] + + [[10 11] + [14 15]]] +``` +Reduces across the batch dimension; scalar True mask behaves like no mask, keeping the reduced axis. + +
+ +###### g. 4D — `axis=-1`, keepdims=True, threshold mask → `(2,3,4,1)` out +```py +import numpy as np +arr = np.arange(48).reshape(2,3,4,2) +mask = arr % 5 != 0 # exclude multiples of 5 +out2341 = np.empty((2,3,4,1), dtype=arr.dtype) +arr.max(axis=-1, where=mask, keepdims=True, out=out2341) +print(out2341.shape); print(out2341[..., :2, :]) +``` +This example results in the following output: +```shell +(2, 3, 4, 1) +[[[[ 1] + [ 3]] + + [[ 7] + [ 9]] + + [[11] + [13]]] + + + [[[21] + [23]] + + [[27] + [29]] + + [[31] + [33]]]] +``` +Reduces the last axis with a condition; keeps the last axis for broadcasting. + +
+ +###### h. 2D — `axis=None` (global), keepdims=True, scalar-True mask, write to `(1,1)` view +```py +import numpy as np +arr = np.array([[5, 3, 9], + [2, 7, 1]]) +mask = True +slot = np.empty(1, dtype=arr.dtype) +out11v = slot.reshape(1,1) # (1,1) view +arr.max(axis=None, where=mask, keepdims=True, out=out11v) +print(out11v, out11v.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Global masked max (all elements allowed) returned into a reshaped one-cell view. + +
+ +###### i. 3D — `axis=1`, keepdims=True, column-broadcast mask `(1,1,4)` → `(2,1,4)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, True])[None, None, :] # broadcast across rows +out214 = np.empty((2,1,4), dtype=arr.dtype) +arr.max(axis=1, where=mask, keepdims=True, out=out214) +print(out214.shape); print(out214) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Reduces across rows; at least one permitted element per (batch,col) due to broadcast mask; reduced axis kept. + +
+ +###### j. 2D — `axis=0`, keepdims=True, write into sliced `out` (non-contiguous slice) +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = arr >= 2 +buf = np.empty((1,6), dtype=arr.dtype) +outS = buf[:, ::2] # slice view with shape (1,3) +arr.max(axis=0, where=mask, keepdims=True, out=outS) +print(outS, outS.shape) +``` +This example results in the following output: +```shell +[[4 8 9]] (1, 3) +``` +Column-wise masked maxima are written into a **strided** slice view; reduced axis preserved. + +
+ +--- + + +### Quick tips +- Match `out.shape` to the **post-reduction** shape: reduced axes dropped or size-one (if `keepdims=True`). +- Masks may be **scalar** (`True`) or **broadcasted**; ensure **≥1 True per reduced slice** since no `initial` is used. +- Non-contiguous or sliced `out` views work if shape/dtype align; NumPy writes correctly into strided buffers. + +--- + +
+ +#### V) `axis` + `out` + `keepdims` + `initial` (No Errors) +###### a. 1D — `axis=0`, `keepdims=True`, baseline dominates → `(1,)` out +```py +import numpy as np +arr = np.array([3, 5, 1]) +out1 = np.empty((1,), dtype=arr.dtype) +arr.max(axis=0, keepdims=True, initial=10, out=out1) +print(out1, out1.shape) +``` +This example results in the following output: +```shell +[10] (1,) +``` +Row is reduced with the dimension preserved; baseline 10 exceeds the true max. + +
+ +###### b. 1D — `axis=None`, `keepdims=False`, scalar out; baseline below max +```py +import numpy as np +arr = np.array([4, 1, 7]) +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=None, keepdims=False, initial=2, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +7 () +``` +Global reduction with a lower baseline; scalar buffer receives the true maximum. + +
+ +###### c. 2D — `axis=0` (columns), `keepdims=True` → `(1,n)` out; baseline as floor +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +out13 = np.empty((1,3), dtype=arr.dtype) +arr.max(axis=0, keepdims=True, initial=5, out=out13) +print(out13, out13.shape) +``` +This example results in the following output: +```shell +[[7 5 6]] (1, 3) +``` +Per-column maxima are compared with baseline 5; only the middle column is lifted. + +
+ +###### d. 2D — `axis=1` (rows), `keepdims=False` → `(nrows,)` out; high baseline +```py +import numpy as np +arr = np.array([[1, 4, 6], + [7, 2, 3]]) +outr2 = np.empty((2,), dtype=arr.dtype) +arr.max(axis=1, keepdims=False, initial=8, out=outr2) +print(outr2, outr2.shape) +``` +This example results in the following output: +```shell +[8 8] (2,) +``` +Row-wise maxima are overridden by the higher baseline. + +
+ +###### e. 2D — `axis=-1` (last), `keepdims=True` → `(nrows,1)` out; mixed baseline effect +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +out21 = np.empty((2,1), dtype=arr.dtype) +arr.max(axis=-1, keepdims=True, initial=6, out=out21) +print(out21, out21.shape) +``` +This example results in the following output: +```shell +[[9] + [6]] (2, 1) +``` +Per row, results are `max(row_max, 6)`; second row lifts to 6 with the reduced axis kept. + +
+ +###### f. 3D — `axis=0` (layers), `keepdims=False` → `(rows,cols)` out; baseline fills early cells +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +out34 = np.empty((3,4), dtype=arr.dtype) +arr.max(axis=0, keepdims=False, initial=15, out=out34) +print(out34.shape); print(out34) +``` +This example results in the following output: +```shell +(3, 4) +[[15 15 15 15] + [16 17 18 19] + [20 21 22 23]] +``` +Across layers, each cell takes `max(layer_max, 15)`; early positions are lifted to 15. + +
+ +###### g. 3D — `axis=1` (middle), `keepdims=True` → `(batch,1,cols)` out; baseline below maxima +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +out214 = np.empty((2,1,4), dtype=arr.dtype) +arr.max(axis=1, keepdims=True, initial=5, out=out214) +print(out214.shape); print(out214) +``` +This example results in the following output: +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Reduces the middle axis; true maxima exceed the baseline; reduced axis retained as size one. + +
+ +###### h. 3D — `axis=2` (last), `keepdims=False` → `(batch,rows)` out; partial baseline dominance +```py +import numpy as np +arr = np.array([[[0,1,2,3], + [4,5,6,7], + [8,9,10,11]], + [[12,13,14,15], + [16,17,18,19], + [20,21,22,23]]]) +out23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, keepdims=False, initial=9, out=out23) +print(out23.shape); print(out23) +``` +This example results in the following output: +```shell +(2, 3) +[[ 9 9 11] + [15 19 23]] +``` +Per row along depth, results take `max(depth_max, 9)`; early rows are lifted to 9 in places. + +
+ +###### i. Negative axis — `axis=-2` (middle), `keepdims=True` → `(2,1,4)` out; dtype upcast +```py +import numpy as np +arr = np.arange(24, dtype=np.int32).reshape(2,3,4) +outf = np.empty((2,1,4), dtype=np.float64) +arr.max(axis=-2, keepdims=True, initial=7.5, out=outf) +print(outf.dtype, outf.shape); print(outf) +``` +This example results in the following output: +```shell +float64 (2, 1, 4) +[[[ 8. 9. 10. 11.]] + + [[20. 21. 22. 23.]]] +``` +Middle-axis reduction with a float baseline written to a float buffer; NumPy safely upcasts results. + +
+ +###### j. Global — `axis=None`, `keepdims=True` → `(1,1)` out; reshaped view buffer +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +slot = np.empty(1, dtype=arr.dtype) +out11v = slot.reshape(1,1) # (1,1) view +arr.max(axis=None, keepdims=True, initial=3, out=out11v) +print(out11v, out11v.shape) +``` +This example results in the following output: +```shell +[[9]] (1, 1) +``` +Global reduction with a low baseline; writes into a reshaped single-cell view and preserves all dimensions. + +
+ +###### k. Empty along reduced axis — shape `(0,3)`, `axis=0`, `keepdims=True` → `(1,3)` out +```py +import numpy as np +arr = np.empty((0,3), dtype=int) +out13 = np.empty((1,3), dtype=int) +arr.max(axis=0, keepdims=True, initial=5, out=out13) +print(out13, out13.shape) +``` +This example results in the following output: +```shell +[[5 5 5]] (1, 3) +``` +Column reduction over an empty axis returns the baseline per column, with the reduced axis kept. + +
+ +###### l. Non-contiguous `out` slice — 2D, `axis=0`, `keepdims=True` → write into strided view +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +buf = np.empty((1,6), dtype=arr.dtype) +outS = buf[:, ::2] # (1,3) strided slice +arr.max(axis=0, keepdims=True, initial=0, out=outS) +print(outS, outS.shape) +``` +This example results in the following output: +```shell +[[4 8 9]] (1, 3) +``` +Masked behavior not used; demonstrates valid writes into a strided slice while keeping the reduced axis. + +
+ + +--- + +### Quick tips +- Match `out.shape` to the **post-reduction** shape: reduced axes dropped or size-one if `keepdims=True`. +- Use `initial` to provide a **floor** and to handle **empty reductions** safely. +- Negative axes (e.g., `-1`, `-2`) behave identically to their positive counterparts. + +--- + +
+ +#### W) `axis` + `out` + `where` + `initial` (No Errors) +###### a. 1D — `axis=0`, even-only mask, scalar `out`; baseline ignored +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=5, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +12 () +``` +Only even elements are considered; 12 exceeds the baseline and is written to a 0-D buffer. + +
+ +###### b. 1D — `axis=0`, all-False mask; scalar `out` gets baseline +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=9, out=out0) +print(out0, out0.shape) +``` +This example results in the following output: +```shell +9 () +``` +No candidates pass the mask; `initial` safely supplies the result. + +
+ +###### c. 2D — `axis=0` (columns), row-broadcast mask; `(ncols,)` out +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +rowmask = np.array([[True],[False]]) # first row only +outc = np.empty(arr.shape[1], dtype=arr.dtype) +arr.max(axis=0, where=rowmask, initial=6, out=outc) +print(outc, outc.shape) +``` +This example results in the following output: +```shell +[6 8 6] (3,) +``` +Column maxima computed using only the first row; baseline lifts columns below 6. + +
+ +###### d. 2D — `axis=1` (rows), column mask; `(nrows,)` out; high baseline +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 3]]) +colmask = np.array([[True, False, True]]) # cols 0 and 2 +outr = np.empty(arr.shape[0], dtype=arr.dtype) +arr.max(axis=1, where=colmask, initial=6, out=outr) +print(outr, outr.shape) +``` +This example results in the following output: +```shell +[6 6] (2,) +``` +Row-wise masked maxima are each raised to the baseline 6. + +
+ +###### e. 2D — `axis=-1` (last), pairwise mask; float `out`, mid baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [3, 4, 1]], dtype=np.int32) +thr = np.array([[3, 7, 6], + [2, 5, 0]]) +mask = arr > thr # [[F,T,F],[T,F,T]] +outf = np.empty((2,), dtype=np.float64) +arr.max(axis=-1, where=mask, initial=4.5, out=outf) +print(outf, outf.dtype) +``` +This example results in the following output: +```shell +[9. 4.5] float64 +``` +Row 0 uses 9; row 1’s masked candidates are below 4.5, so the baseline prevails; results are safely upcast. + +
+ +###### f. 3D — `axis=0` (layers), layer-select mask; `(rows,cols)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] # layer 0 only +out34 = np.empty((3,4), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=5, out=out34) +print(out34.shape); print(out34) +``` +This example results in the following output: +```shell +(3, 4) +[[ 5 5 5 5] + [ 5 5 6 7] + [ 8 9 10 11]] +``` +Reduces across layers but only reads layer 0; baseline fills low positions. + +
+ +###### g. 3D — `axis=1` (middle), ensure one True per (batch,col); (batch,cols) out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 1, 2] = True # exactly one candidate per (batch,col) +out24 = np.empty((2,4), dtype=arr.dtype) +arr.max(axis=1, where=mask, initial=10, out=out24) +print(out24.shape); print(out24) +``` +This example results in the following output: +```shell +(2, 4) +[[10 10 14 10] + [10 10 22 10]] +``` +Each (batch,col) slice compares a single element with baseline 10; maximum is chosen per column. +
+ +###### h. 3D — `axis=2` (last), periodic mask; `(batch,rows)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] +out23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, where=mask, initial=9, out=out23) +print(out23.shape); print(out23) +``` +This example results in the following output +```shell +(2, 3) +[[ 9 9 11] + [13 17 21]] +``` +For each row across depth, consider indices 0 and 2; baseline 9 lifts earlier rows. + +
+ +###### i. 2D — `axis=0`, reshaped view as `out`; broadcast mask +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +rowmsk = np.array([[True],[False]]) # first row only +flat = np.empty(3, dtype=arr.dtype) +outv = flat.reshape(3) # view for (ncols,) +arr.max(axis=0, where=rowmsk, initial=3, out=outv) +print(outv, "(backed by 'flat')") +``` +This example results in the following output +```shell +[4 6 3] (backed by 'flat') +``` +Writes masked column maxima into a reshaped view; third column is lifted by the baseline. + +
+ +###### j. 1D — `axis=0`, float baseline and float `out` from int array +```py +import numpy as np +arr = np.array([1, 6, 3], dtype=np.int32) +mask = arr >= 3 +out0 = np.empty((), dtype=np.float64) +arr.max(axis=0, where=mask, initial=4.5, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +6.0 () +``` +Masked scalar maximum is stored in a float buffer; NumPy performs safe upcasting. + +
+ +###### k. Global — `axis=None`, data-driven mask, scalar `out`; baseline floor +```py +import numpy as np +arr = np.array([1.2, 3.4, 2.8, 0.9]) +mask = arr > arr.mean() +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=None, where=mask, initial=2.5, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +3.4 () +``` +Global masked reduction considers only above-mean elements; 3.4 beats the baseline. + +
+ +###### l. 3D — `axis=-2` (middle), column-broadcast mask `(1,1,4)`; `(batch,cols)` out +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, True])[None, None, :] # broadcast across rows +out24 = np.empty((2,4), dtype=arr.dtype) +arr.max(axis=-2, where=mask, initial=7, out=out24) +print(out24.shape); print(out24) +``` +This example results in the following output +```shell +(2, 4) +[[ 8 9 10 11] + [20 21 22 23]] +``` +Reduces over rows with a broadcast mask; true maxima exceed the baseline across columns. + +
+ +--- + +### Quick tips +- Match `out.shape` to the **reduced** shape (`axis=None` → scalar; `axis=k` → drop that axis). +- Use `initial` to guarantee results when a masked slice has **no True** or low candidates. +- Masks can **broadcast** (e.g., `(2,1)` on `(2,3)`); ensure at least one True per reduced slice. + +--- + +
+ +#### X) `out` + `keepdims` + `where` + `initial` (No Errors) +###### a. 1D — Even-only mask; `keepdims=False`; scalar `out`; baseline lower than masked max +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # 10, 12 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, keepdims=False, initial=5, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +12 () +``` +Only even entries participate; 12 beats the baseline and is written into the scalar buffer. + +
+ +###### b. 1D — All-False mask; `keepdims=True`; `(1,)` `out`; baseline supplies result +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) +out1 = np.empty((1,), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, initial=9, out=out1) +print(out1, out1.shape) +``` +This example results in the following output +```shell +[9] (1,) +``` +No candidates pass the mask; baseline fills the kept-dims result. + +
+ +###### c. 2D — Row-broadcast mask (first row only); `keepdims=False`; scalar `out`; baseline as floor +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # (2,1) broadcast across columns +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, keepdims=False, initial=6, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +8 () +``` +Global masked reduction uses only the first row; result exceeds the baseline. + +
+ +###### d. 2D — Column mask; `keepdims=True`; `(1,1)` `out`; baseline equals masked max +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[False, True, False]]) # pick only column 1 everywhere +out11 = np.empty((1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, initial=8, out=out11) +print(out11, out11.shape) +``` +This example results in the following output +```shell +[[8]] (1, 1) +``` +Only column 1 participates; baseline equals the masked maximum, preserving value and kept shape. + +
+ +###### e. 2D — Pairwise mask; `keepdims=True`; float `out` and float baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [3, 4, 1]], dtype=np.int32) +thr = np.array([[3, 7, 6], + [2, 5, 0]]) +mask = arr > thr # [[F,T,F],[T,F,T]] +out11f= np.empty((1,1), dtype=np.float64) +arr.max(where=mask, keepdims=True, initial=6.5, out=out11f) +print(out11f, out11f.dtype, out11f.shape) +``` +This example results in the following output +```shell +[[9. ]] float64 (1, 1) +``` +Only allowed positions compared; result upcasts safely to float in a `(1,1)` buffer. + +
+ +###### f. 3D — Scalar `True` mask (all allowed); `keepdims=True`; `(1,1,1)` `out`; baseline below max +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # 0..23 +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(where=True, keepdims=True, initial=5, out=out111) +print(out111, out111.shape) +``` +This example results in the following output +```shell +[[[23]]] (1, 1, 1) +``` +Global reduction over all elements; true global max exceeds the baseline. + +
+ +###### g. 3D — Threshold mask; `keepdims=False`; scalar `out`; baseline dominates +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = arr >= 20 +out0 = np.empty((), dtype=arr.dtype) +arr.max(where=mask, keepdims=False, initial=24, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +24 () +``` +Masked maximum is 23; baseline 24 wins and is returned. + +
+ +###### h. 3D — Periodic last-axis mask; `keepdims=True`; `(1,1,1)` `out` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] # keep indices 0,2 +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, initial=9, out=out111) +print(out111, out111.shape) +``` +This example results in the following output +```shell +[[[22]]] (1, 1, 1) +``` +Global masked reduction limited to positions (..,0|2); result exceeds baseline and dims are kept. + +
+ +###### i. 3D — Layer-select mask (first layer only); `keepdims=True`; `(1,1,1)` `out`; baseline as floor +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] +out111 = np.empty((1,1,1), dtype=arr.dtype) +arr.max(where=mask, keepdims=True, initial=11, out=out111) +print(out111, out111.shape) +``` +This example results in the following output +```shell +[[[11]]] (1, 1, 1) +``` +Only layer 0 is considered (max 11); equal-to-baseline case returns the same value. + +
+ +###### j. Empty 1D; `keepdims=True`; `(1,)` `out`; baseline supplies value +```py +import numpy as np +arr = np.array([], dtype=int) +mask = np.array([], dtype=bool) +out1 = np.empty((1,), dtype=int) +arr.max(where=mask, keepdims=True, initial=7, out=out1) +print(out1, out1.shape) +``` +This example results in the following output +```shell +[7] (1,) +``` +Empty input and mask; baseline provides a well-defined kept-dims result. + +
+ +###### k. Reshaped view buffer as `out`; 2D; `keepdims=True`; global mask +```py +import numpy as np +arr = np.array([[5, 3, 9], + [2, 7, 1]]) +slot = np.empty(1, dtype=arr.dtype) +out11v = slot.reshape(1,1) # (1,1) view +arr.max(where=True, keepdims=True, initial=3, out=out11v) +print(out11v, out11v.shape) +``` +This example results in the following output +```shell +[[9]] (1, 1) +``` +Writes the global maximum into a reshaped one-cell view; all dimensions preserved. + +
+ +###### l. Reuse the same scalar `out` with different masks/baselines +```py +import numpy as np +a = np.array([2, 9, 1]) +b = np.array([3, 5, 7]) +out0 = np.empty((), dtype=int) + +a_mask = a > 3 +a.max(where=a_mask, keepdims=False, initial=0, out=out0); print("a:", out0) + +b_mask = b % 2 == 0 +b.max(where=b_mask, keepdims=False, initial=10, out=out0); print("b:", out0) +``` +This example results in the following output +```shell +a: 9 +b: 10 +``` +The same 0-D buffer is overwritten across calls; `initial` provides a fallback when the mask is restrictive. + +
+ + +--- + +### Quick tips +- With **no `axis`**, this is a **global masked reduction**; `keepdims` controls `(1,)*ndim` vs scalar `()`. +- Ensure `out.shape` matches the chosen `keepdims` result. +- `initial` guarantees valid results for **all-False** masks or **empty** inputs. + +--- + +
+ +#### Y) `axis` + `keepdims` + `where` + `initial` (No Errors) +###### a. 1D — `axis=0`, partial mask, `keepdims=False`, baseline ignored +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 # keep 10, 12 +res = arr.max(axis=0, keepdims=False, where=mask, initial=5) +print(res, np.shape(res)) +``` +This example results in the following output +```shell +12 () +``` +Masked max (12) exceeds baseline 5; returns a scalar because `keepdims=False`. + +
+ +###### b. 1D — `axis=0`, all-False mask, `keepdims=True`, baseline supplies result +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) # no candidates +res = arr.max(axis=0, keepdims=True, where=mask, initial=9) +print(res, res.shape) +``` +This example results in the following output +```shell +[9] (1,) +``` +No elements selected; baseline 9 ensures a defined result with one kept dimension. + +
+ +###### c. 2D — `axis=0` (columns), row-broadcast mask, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # use first row only +res = arr.max(axis=0, keepdims=True, where=mask, initial=6) +print(res, res.shape) +``` +This example results in the following output +```shell +[[6 8 6]] (1, 3) +``` +Column maxima come only from the first row; columns below 6 are lifted by the baseline. + +
+ +###### d. 2D — `axis=1` (rows), column mask, `keepdims=False`, high baseline +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 3]]) +mask = np.array([[True, False, True]]) # use cols 0 and 2 +res = arr.max(axis=1, keepdims=False, where=mask, initial=6) +print(res, np.shape(res)) +``` +This example results in the following output +```shell +[6 6] () +``` +Per row, selected columns are compared against baseline 6; both rows return 6. + +
+ +###### e. 2D — `axis=-1` (last), pairwise mask, `keepdims=True`, mid baseline +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr # [[T,F,T],[F,T,F]] +res = arr.max(axis=-1, keepdims=True, where=mask, initial=6) +print(res, res.shape) +``` +This example results in the following output +```shell +[[6] + [6]] (2, 1) +``` +Row-wise, first row’s masked max (5) is lifted to 6; second row’s masked max (6) equals baseline. + +
+ +###### f. 2D — `axis=None` (global), `keepdims=True`, row-broadcast mask +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +mask = np.array([[True],[False]]) # consider only first row +res = arr.max(axis=None, keepdims=True, where=mask, initial=5) +print(res, res.shape) +``` +This example results in the following output +```shell +[[6]] (1, 1) +``` +Global masked reduction over the first row; 6 beats baseline 5, and all dimensions are kept. + +
+ +###### g. 3D — `axis=0` (layers), layer-select mask, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers, rows, cols) +mask = np.array([True, False])[:, None, None] # layer 0 only +res = arr.max(axis=0, keepdims=False, where=mask, initial=5) +print(res.shape); print(res) +``` +This example results in the following output +```shell +(3, 4) +[[ 5 5 5 5] + [ 5 5 6 7] + [ 8 9 10 11]] +``` +Across layers but only reading layer 0; baseline ensures a floor of 5. + +
+ +###### h. 3D — `axis=1` (middle), ensure ≥1 True per (batch,col), `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 1, 2] = True # single True per (batch,col) +res = arr.max(axis=1, keepdims=True, where=mask, initial=10) +print(res.shape); print(res) +``` +This example results in the following output +```shell +(2, 1, 4) +[[[10 10 14 10]] + + [[10 10 22 10]]] +``` +Per (batch,col), compare that single element with baseline 10; reduced axis kept as size one. + +
+ +###### i. 3D — `axis=2` (last), periodic mask, `keepdims=False`, baseline floor +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] +res = arr.max(axis=2, keepdims=False, where=mask, initial=9) +print(res.shape); print(res) +``` +This example results in the following output +```shell +(2, 3) +[[ 9 9 11] + [13 17 21]] +``` +For each row across depth, consider indices 0 and 2; baseline 9 lifts earlier rows. + +
+ +###### j. 3D — Negative axis `-2` (middle), column-broadcast mask, `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, True])[None, None, :] # broadcast across rows +res = arr.max(axis=-2, keepdims=True, where=mask, initial=7) +print(res.shape); print(res) +``` +This example results in the following output +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Reduces over rows; broadcast mask ensures candidates per column; reduced axis retained. + +
+ +###### k. 3D — `axis=None` (global), scalar-True mask, `keepdims=False`, high baseline +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +res = arr.max(axis=None, keepdims=False, where=True, initial=30) +print(res, np.shape(res)) +``` +This example results in the following output +```shell +30 () +``` +Global reduction with all elements allowed; baseline 30 dominates the true max (23). + +
+ +###### l. Empty along reduced axis — shape `(0,3)`, `axis=0`, `keepdims=True` +```py +import numpy as np +arr = np.empty((0,3), dtype=int) +res = arr.max(axis=0, keepdims=True, where=True, initial=5) +print(res, res.shape) +``` +This example results in the following output +```shell +[[5 5 5]] (1, 3) +``` +Empty column reduction returns the baseline per column with the reduced axis kept. + +
+ + +--- + +### Quick tips +- `keepdims=True` returns shapes with **size-one** reduced axes; `False` drops them (scalars for 1D/global). +- Use `initial` to guarantee results for **all-False** masked slices or **empty** reductions. +- Masks may **broadcast**; ensure each reduced slice has **at least one candidate or a suitable baseline**. + +--- + +
+ +#### Z) `axis` + `out` + `keepdims` + `where` + `initial` (No Errors) +###### a. 1D — `axis=0`, scalar out, partial mask, low baseline, `keepdims=False` +```py +import numpy as np +arr = np.array([3, 10, 7, 12]) +mask = (arr % 2) == 0 +out0 = np.empty((), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=5, keepdims=False, out=out0) +print(out0, out0.shape) +``` +This example results in the following output +```shell +12 () +``` +Even-only reduction; true masked max (12) exceeds the baseline. + +
+ +###### b. 1D — `axis=0`, `(1,)` out, all-False mask, baseline supplies value, `keepdims=True` +```py +import numpy as np +arr = np.array([3, 1, 2]) +mask = np.zeros_like(arr, dtype=bool) +out1 = np.empty((1,), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=9, keepdims=True, out=out1) +print(out1, out1.shape) +``` +This example results in the following output +```shell +[9] (1,) +``` +No candidates; baseline 9 is returned with one dimension preserved. + +
+ +###### c. 2D — `axis=0` (columns), `(1,n)` out, row-broadcast mask, floor baseline, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = np.array([[True],[False]]) # first row only +out13 = np.empty((1,3), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=6, keepdims=True, out=out13) +print(out13, out13.shape) +``` +This example results in the following output +```shell +[[6 8 6]] (1, 3) +``` +Column-wise maxima from first row only; columns below 6 are lifted to the baseline. + +
+ +###### d. 2D — `axis=1` (rows), `(n,)` out, column mask, high baseline, `keepdims=False` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 3]]) +mask = np.array([[True, False, True]]) # cols 0 & 2 +outr = np.empty(arr.shape[0], dtype=arr.dtype) +arr.max(axis=1, where=mask, initial=6, keepdims=False, out=outr) +print(outr, outr.shape) +``` +This example results in the following output +```shell +[6 6] (2,) +``` +Row-wise reduction over selected columns; each row raised to baseline 6. + +
+ +###### e. 2D — `axis=-1`, `(n,1)` out, pairwise mask, mid baseline, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 9, 5], + [4, 6, 3]]) +thr = np.array([[1, 9, 0], + [4, 5, 8]]) +mask = arr > thr # [[T,F,T],[F,T,F]] +out21 = np.empty((2,1), dtype=arr.dtype) +arr.max(axis=-1, where=mask, initial=6, keepdims=True, out=out21) +print(out21, out21.shape) +``` +This example results in the following output +```shell +[[6] + [6]] (2, 1) +``` +Per-row masked maxima compared to baseline; both rows return 6 with last axis retained. + +
+ +###### f. 2D — Global (`axis=None`), `(1,1)` out, row-broadcast mask, baseline below max, `keepdims=True` +```py +import numpy as np +arr = np.array([[4, 6, 1], + [2, 9, 7]]) +mask = np.array([[True],[False]]) # first row only +out11 = np.empty((1,1), dtype=arr.dtype) +arr.max(axis=None, where=mask, initial=5, keepdims=True, out=out11) +print(out11, out11.shape) +``` +This example results in the following output +```shell +[[6]] (1, 1) +``` +Global masked reduction over first row; 6 exceeds baseline 5; all dims preserved. + +
+ +###### g. 3D — `axis=0` (layers), `(rows,cols)` out, layer-select mask, floor baseline, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) # (layers,rows,cols) +mask = np.array([True, False])[:, None, None] # use layer 0 only +out34 = np.empty((3,4), dtype=arr.dtype) +arr.max(axis=0, where=mask, initial=5, keepdims=False, out=out34) +print(out34.shape); print(out34) +``` +This example results in the following output +```shell +(3, 4) +[[ 5 5 5 5] + [ 5 5 6 7] + [ 8 9 10 11]] +``` +Across layers but only layer 0 contributes; baseline fills low cells. + +
+ +###### h. 3D — `axis=1` (middle), `(batch,1,cols)` out, single-True per slice, `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.zeros_like(arr, dtype=bool) +mask[:, 1, 2] = True # one candidate per (batch,col) +out214 = np.empty((2,1,4), dtype=arr.dtype) +arr.max(axis=1, where=mask, initial=10, keepdims=True, out=out214) +print(out214.shape); print(out214) +``` +This example results in the following output +```shell +(2, 1, 4) +[[[10 10 14 10]] + + [[10 10 22 10]]] +``` +Each (batch,col) compares the single masked element with baseline 10; reduced axis kept. + +
+ +###### i. 3D — `axis=2` (last), `(batch,rows)` out, periodic mask, baseline floor, `keepdims=False` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, False])[None, None, :] +out23 = np.empty((2,3), dtype=arr.dtype) +arr.max(axis=2, where=mask, initial=9, keepdims=False, out=out23) +print(out23.shape); print(out23) +``` +This example results in the following output +```shell +(2, 3) +[[ 9 9 11] + [13 17 21]] +``` +Depth-wise per row using indices 0 and 2; early rows lifted to baseline 9. + +
+ +###### j. 3D — Negative axis `-2` (middle), `(batch,1,cols)` out, column-broadcast mask, `keepdims=True` +```py +import numpy as np +arr = np.arange(24).reshape(2,3,4) +mask = np.array([True, False, True, True])[None, None, :] # broadcast across rows +out214 = np.empty((2,1,4), dtype=arr.dtype) +arr.max(axis=-2, where=mask, initial=7, keepdims=True, out=out214) +print(out214.shape); print(out214) +``` +This example results in the following output +```shell +(2, 1, 4) +[[[ 8 9 10 11]] + + [[20 21 22 23]]] +``` +Row reduction with broadcast mask; true maxima exceed baseline; reduced axis retained. + +
+ +###### k. Dtype upcast — 2D, `axis=1`, `(n,)` float out, threshold mask, mid baseline, `keepdims=False` +```py +import numpy as np +arr = np.array([[1, 6, 3], + [2, 5, 4]], dtype=np.int32) +mask = arr >= 3 +outf = np.empty((2,), dtype=np.float64) +arr.max(axis=1, where=mask, initial=4.5, keepdims=False, out=outf) +print(outf, outf.dtype) +``` +This example results in the following output +```shell +[6. 5.] float64 +``` +Row-wise masked maxima with a float baseline stored in a float buffer; safe upcast. + +
+ +###### l. Non-contiguous `out` view — 2D, `axis=0`, `(1,n)` strided slice, scalar-True mask, `keepdims=True` +```py +import numpy as np +arr = np.array([[2, 8, 5], + [4, 1, 9]]) +mask = True # all allowed +buf = np.empty((1,6), dtype=arr.dtype) +outS = buf[:, ::2] # (1,3) strided view +arr.max(axis=0, where=mask, initial=0, keepdims=True, out=outS) +print(outS, outS.shape) +``` +This example results in the following output +```shell +[[4 8 9]] (1, 3) +``` +Column maxima written into a strided (1,3) slice; reduced axis kept and baseline unused. + +
+ +--- + +### Quick tips + +- Match `out.shape` to the **post-reduction** shape: drop reduced axes or keep them as size-one if `keepdims=True`. +- Use `initial` to guarantee results for **all-False** masked slices or **empty** reductions. +- Masks may **broadcast**; ensure every reduced slice has at least one candidate or a suitable baseline. + +--- + +
+ +## Codebyte Example + +```codebyte/python +import numpy as np + +# Sample 2D array +arr = np.array([[3, 7, 1], + [8, 2, 5]]) + +# Mask: include only elements >= 5 +mask = arr >= 5 + +# Compute row-wise maximums with baseline and dimension retention +result = arr.max(axis=1, where=mask, initial=4, keepdims=True) + +print("Array:") +print(arr) + +print("\nMask (arr >= 5):") +print(mask) + +print("\nRow-wise masked maximums (keepdims=True):") +print(result) +``` + + +--- + +## Common Pitfalls + +- Providing an `out` array with a shape that does not match the reduction result causes a `ValueError`. +- Using `where` with all-False masks requires an `initial` value; otherwise, the result is undefined. +- Forgetting that `keepdims=True` preserves reduced axes as size-one dimensions may cause shape mismatches in later operations. + +## Tips + +- Use `keepdims=True` when the output must broadcast correctly with the input array. +- `initial` is essential for predictable results when masking excludes all elements along a reduction path. +- Boolean masks in `where` automatically broadcast across array dimensions. +