Python

How to use Python NumPy mean(), min() and max() functions?

Python NumPy library has many aggregate or statistical functions for doing different types of tasks with the one-dimensional or multi-dimensional array. Some of the useful aggregate functions are mean(), min(), max(), average(), sum(), median(), percentile(), etc. The uses of mean(), min(), and max() functions are described in this tutorial. The mean() function is used to return the arithmetic mean value of the array elements. The arithmetic mean is calculated by dividing the sum of all elements of the array by the total number of array elements. If the particular axis is mentioned in the function, then it will calculate the mean value of the particular axis. max() function is used to find out the maximum value from the array elements or the elements of the particular array axis. min() function is used to find out the minimum value from the array elements or the particular array axis.

Use of mean() function

The syntax of the mean() function is given below.

Syntax:

numpy.mean(input_array, axis=None, dtype=None, out=None, keepdims=<no value>)

This function can take five arguments. The purposes of these arguments are described below:

input_array

It is a mandatory argument that takes an array as the value and the average of the array values is calculated by this function.

axis

It is an optional argument, and the value of this argument can be an integer or the tuple of integers. This argument is used for the multi-dimensional array. If the value of the axis is set to 0, then the function will calculate the mean of the column values, and if the value of the axis is set to 1, then the function will calculate the mean of the row values.

dtype

It is an optional argument that is used to define the data type of the mean value.

out

It is an optional argument and is used when the output of the function will need to store in an alternative array. In this case, the dimension of the output array must be the same as the input array. The default value of this argument is None.

keepdims

It is an optional argument, and any Boolean value can be set in this argument. It is used to transmit the output properly based on the input array.

This function returns an array of mean values if the value of the out argument is set to None, otherwise the function returns the reference to the output array.

Example: Using mean() function

The following example shows how the mean value of a one-dimensional and two-dimensional array can be calculated. Here, the first mean() function is used with a one-dimensional array of integer numbers, and the second mean() function is used with a two-dimensional array of integer numbers.

# import NumPy library

import numpy as np

# Create a one-dimensional array

np_array = np.array([6, 4, 9, 3, 1])

# Print array and mean values

print("The values of the one-dimensional NumPy array are:\n ", np_array)

print("The mean value of the one-dimensional array is:\n", np.mean(np_array))

# Create a two-dimensional array

np_array = np.array([[5, 3, 5], [5, 4, 3]])

# Print array and mean values

print("\nThe values of the two-dimensional NumPy array are:\n  ", np_array)

print("The mean values of the two-dimensional array are:\n", np.mean(np_array, axis=0))

Output:

The following output will appear after executing the above script.

Use of max() function

The syntax of the max() function is given below.

Syntax:

numpy.max(input_array, axis=None, out=None, keepdims=None, initial=None, where=None)

This function can take six arguments. The purposes of these arguments are described below:

input_array

It is a mandatory argument that takes an array as the value, and this function finds out the maximum value of the array.

axis

It is an optional argument, and its value can be an integer or the tuple of integers. This argument is used for the multi-dimensional array.

out

It is an optional argument and is used when the output of the function will need to store in an alternative array.

keepdims

It is an optional argument, and any Boolean value can be set in this argument. It is used to transmit the output properly based on the input array.

initial

It is an optional argument that is used to set the minimum value of the output.

where

It is an optional argument that is used to compare the array elements to find out the maximum value. The default value of this argument is None.

This function returns the maximum value for the one-dimensional array or an array of the maximum values for the multi-dimensional array.

Example: Using max() function

The following example shows the use of the max() function to find out the maximum value of a one-dimensional array.

# import NumPy library

import numpy as np

# Create NumPy array of integers

np_array = np.array([21, 5, 34, 12, 30, 6])

# Find the maximum value from the array

max_value = np.max(np_array)

# Print the maximum value

print('The maximum value of the array is: ', max_value)

Output:

The following output will appear after executing the above script.

Use of min() function

The syntax of the min() function is given below.

Syntax:

numpy.min(input_array, axis=None, out=None, keepdims=None, initial=None, where=None)

The purposes of the arguments of this function are the same as the max() function that has been explained in the part of the max() function. This returns the minimum value of the input array.

Example: Using min() function

The following example shows the use of the min() function to find out the minimum value of a one-dimensional array.

# import NumPy library

import numpy as np

# Create NumPy array of integers

np_array = np.array([21, 5, 34, 12, 30, 6])

# Find the maximum value from the array

max_value = np.max(np_array)

# Print the maximum value

print('The maximum value of the array is: ', max_value)

Output:

The following output will appear after executing the above script.

Conclusion

The purposes of three useful aggregate functions (mean(), max(), and min()) have been explained in this tutorial to help the readers to know the ways of using these functions in python script.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.