Hello and welcome to this tutorial on **Numpy fmax**. In this tutorial, we will be learning about the **NumPy fmax() **method and also seeing a lot of examples regarding the same. So let us begin!

*Also read: NumPy ones_like – A Complete Guide*

## What is NumPy fmax?

`fmax()`

is a function in NumPy that compares two arrays and returns an array that contains the element-wise maximum of these two arrays.

## Syntax of NumPy fmax

Let us have a look at the syntax of the `fmax()`

function.

```
numpy.fmax(x1, x2, out=None)
```

Parameter | Description | Required/Optional |

x1 | Input array 1. | Required |

x2 | Input array 2. | Required |

out | An alternative output array in which to place the result. It must have the same shape as the expected output. | Optional |

**Returns:**

A new array containing the element-wise maximum of *x1* and *x2*.

- If
*x1*and*x2*are both scalars, then the output is also a scalar. - If any of
*x1*or*x2*contains a NaN value, then the output for that element-wise comparison is the non-NaN value. - If both elements in the comparison are NaNs, then the first NaN is returned.

## Examples of using NumPy fmax

Let’s now look at a few examples to understand the `fmax()`

function better.

### Using NumPy fmax when both inputs are scalars

```
import numpy as np
a = 15
b = 8
# using fmax function to calculate the element-wise maximum
ans = np.fmax(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)
```

**Output:**

```
a = 15
b = 8
Result = 15
```

Since 15>8, the answer is 15.

### Element-wise maximum of 1-d arrays

```
import numpy as np
a = [2, 36, 1, 5, 10]
b = [6, 3 ,48, 2, 18]
# using fmax function to calculate the element-wise maximum
ans = np.fmax(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)
```

**Output:**

```
a = [2, 36, 1, 5, 10]
b = [6, 3, 48, 2, 18]
Result = [ 6 36 48 5 18]
```

The resulting array is calculated as:

```
ans[0] = max(a[0], b[0]) = max(2, 6) = 6
ans[1] = max(a[1], b[1]) = max(36, 3) = 36
ans[2] = max(a[2], b[2]) = max(1, 48) = 48
ans[3] = max(a[3], b[3]) = max(5, 2) = 5
ans[4] = max(a[4], b[4]) = max(10, 18) = 18
```

### Element-wise maximum of 2-d arrays

```
import numpy as np
a = [[6, -8, 4], [2, 21, 16]]
b = [[-5, -12, 1], [0, 10, 27]]
# using fmax function to calculate the element-wise maximum
ans = np.fmax(a, b)
print("a =", a, "\nb =", b)
print("Result =\n", ans)
```

**Output:**

```
a = [[6, -8, 4], [2, 21, 16]]
b = [[-5, -12, 1], [0, 10, 27]]
Result =
[[ 6 -8 4]
[ 2 21 27]]
```

Here, both the input arrays are 2×3 arrays, hence the resulting array is also a 2×3 computed as:

```
ans[0][0] = max(a[0][0], b[0][0]) = max(6, -5) = 6
ans[0][1] = max(a[0][1], b[0][1]) = max(-8, -12) = -8
ans[0][2] = max(a[0][2], b[0][2]) = max(4, 1) = 4
ans[1][0] = max(a[1][0], b[1][0]) = max(2, 0) = 2
ans[1][1] = max(a[1][1], b[1][1]) = max(21, 10) = 21
ans[1][2] = max(a[1][2], b[1][2]) = max(16, 27) = 27
```

### Element-wise maximum of arrays containing NaNs

```
import numpy as np
a = [8, np.nan, 5, 3]
b = [0, np.nan, np.nan, -6]
# using fmax function to calculate the element-wise maximum
ans = np.fmax(a, b)
print("a =", a, "\nb =", b)
print("Result =", ans)
```

**Output:**

```
a = [8, nan, 5, 3]
b = [0, nan, nan, -6]
Result = [ 8. nan 5. 3.]
```

Here,

```
ans[0] = max(a[0], b[0]) = max(8, 0) = 8
```

Now, a[1] and b[1] are both NaNs, so the maximum of these is also returned as NaN.

```
ans[1] = NaN
```

a[2] = 5 and b[2] = NaN, so the maximum is the non-NaN value i.e. 5.

```
ans[2] = 5
ans[3] = max(a[3], b[3]) = max(3, -6) = 3
```

## Conclusion

That’s all! In this tutorial, we learned about the **Numpy fmax **method and practiced different types of examples using the same.

If you want to learn more about NumPy, feel free to go through our NumPy tutorials.