# ValueError: zero-size array to reduction operation maximum which has no identity

While working with arrays, you may face ValueError: zero-size array to reduction operation maximum which has no identity error. This error occurs when you tried to apply the max reduction operation on an empty array. This error occurs because the max reduction operation cannot be applied on an empty array. In this short article, we will learn different methods to solve ValueError: zero-size array to reduction operation maximum which has no identity error. Moreover, we will discuss the error deeply to understand the error.

## ValueError: zero-size array to reduction operation maximum which has no identity

This error occurs when we tried to apply max reduction on an empty array. The max education cannot be applied to an empty array. Let us take an example and apply the max function on an empty array.

``````# importing the array
import numpy as np

# Create an empty array
arr = np.array([])

# maximize the array
max_val = np.max(arr)``````

Output:

``````---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_168023/2126853683.py in <module>
5
6 # maximize the array
----> 7 max_val = np.max(arr)

<__array_function__ internals> in amax(*args, **kwargs)

/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where)
2752     5
2753     """
-> 2754     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
2755                           keepdims=keepdims, initial=initial, where=where)
2756

/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
84                 return reduction(axis=axis, out=out, **passkwargs)
85
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
87
88

ValueError: zero-size array to reduction operation maximum which has no identity``````

As you can see, we get the error: The simplest way to get rid of the error is to fill the array with elements or us if-else statements:

### Solution-1: Check the length of the array

As we know the reason for the error, so to get rid of ValueError: zero-size array to reduction operation maximum which has no identity error. We can use the len method to check the size of the array and if the array is empty then we will not apply the max reduction function as shown below:

``````# importing the array
import numpy as np

# Create an empty array
arr = np.array([])

# if statement
if len(arr) > 0:
max_val = np.max(arr)
print(max_val)

# else statements
else:
max_val = None
print(max_val)``````

Output:

``None``

As you can, we solved the error without getting an error.

### Solution-2: Using np.nanmax function

numpy. nanmax() function is used to return the maximum value of an array or along any specifically mentioned axis of the array, ignoring any Nan value. Syntax : numpy.nanmax(arr, axis=None, out=None, keepdims = no value) Parameters : arr : Input array.

Let us now use the np.nanmax function to see how we can use it to get rid of ValueError: zero-size array to reduce operation maximum which has no identity.

``````# importing numpy array
import numpy as np

# Create an empty array
arr = np.array([])

# Use the np.nanmax function
max_val = np.nanmax(arr)
print(max_val) ``````

This will help you to get rid of ValueError: zero-size array to reduction operation maximum which has no identity error

### Solution-3: Use try-except block

The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.

Let us now use the try and except block to handle the error:

``````# importing the array
import numpy as np

# Create an empty array
arr = np.array([])

# try block
try:
max_val = np.max(arr)
print(max_val)

# except block
except:
max_val = None
print(max_val)``````

Output:

``None``

As you can see, we have executed the code without getting any errors!

## Understanding ValueError: zero-size array to reduction operation maximum which has no identity error

In Python, the error itself gives a lot of information. The errors in Python have two parts. The first part of the error shows the category of the error. In our case, the problem is from ValueError category. The second part of the error gives more specific information about the error which in this case shows what type of value error we are getting.

What is valueError in Python?

The Python ValueError is an exception that occurs when a function receives an argument of the correct data type but an inappropriate value. This error usually occurs in mathematical operations that require a certain kind of value.

What is NumPy array in Python?

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

What is Max function in NumPy?

The max() function returns the item with the highest value or the item with the highest value in an iterable. If the values are strings, an alphabetical comparison is done.

## Summary

In this short article, we learned how we solve ValueError: zero-size array to reduction operation maximum which has no identity error. We discussed three different methods to solve ValueError: zero-size array to reduction operation maximum which has no identity and explained each method separately. Moreover, we also covered and understand the error deeply.