**NumPy mean() Function:**

**Python numpy mean: **The arithmetic mean along the provided axis is computed using the mean() function of the NumPy module. By default, the mean is calculated over the flattened array; otherwise, it is calculated over the given axis.

**Syntax:**

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

**Parameters**

**a:** This is required. It is an array of numbers whose mean is to be computed. A conversion is attempted if “a” is not an array.

**axis:** This is optional. Indicate which axis or axes will be used to determine the mean. The default, axis=None, computes the flattened array’s mean.

**dtype:** This is optional. Indicate the data type that will be used to calculate the mean. The default for integer inputs is float64. It is the same as the input dtype for floating point inputs.

**out:** This is optional. It Indicates the output array for the result. None is the default value. It must have the same shape as output if it is provided.

**keepdims:** This is optional. The reduced axes are left in the output as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option. The keepdims will not be passed through to the mean function of ndarray sub-classes if the default value is used, but any non-default value will. Exceptions will be thrown if the sub-class method does not implement keepdims.

## NumPy mean() Function in Python

**Example1**

**Approach:**

- Import numpy module using the import keyword
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the given array as an argument to the mean() function of numpy module to calculate the mean of all values in the given array
- Store it in another variable.
- Print the mean of all values in the given array.
- The Exit of the Program.

**Below is the implementation:**

# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[5,20],[35, 40]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array as an argument to the mean() function of numpy module # to calculate the mean of all values in the given array # Store it in another variable. rslt = np.mean(gvn_arry) # Print the mean of all values in the given array print("The mean of all values in the given array =", rslt)

**Output:**

The above given array is: [[ 5 20] [35 40]] The mean of all values in the given array = 25.0

**Example2**

The mean is calculated over the specified axes when the axis parameter is set.

**Approach:**

- Import numpy module using the import keyword
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the given array, axis=0 as the arguments to the mean() function of numpy module to calculate the mean of all values in the given array along axis=0.
- Store it in another variable.
- Print the mean of all values the given array along axis=0
- Pass the given array, axis=0 as the arguments to the mean() function of numpy module to calculate the mean of all values in the given array along axis=1.
- Store it in another variable.
- Print the mean of all values the given array along axis=1
- The Exit of the Program.

**Below is the implementation:**

# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[5, 25, 50],[35, 40, 60]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array, axis=0 as the arguments to the mean() function of numpy module # to calculate the mean of all values in the given array along axis=0 # Store it in another variable. rslt_1 = np.mean(gvn_arry, axis=0) # Print the mean of all values the given array along axis=0 print("The mean of all values in the given array along axis=0: ", rslt_1) # Pass the given array, axis=1 as the arguments to the mean() function of numpy module # to calculate the mean of all values in the given array along axis=1 # Store it in another variable. rslt_2 = np.mean(gvn_arry, axis=1) # Print the mean of all values the given array along axis=1 print("The mean of all values in the given array along axis=1: ", rslt_2)

**Output:**

The above given array is: [[ 5 25 50] [35 40 60]] The mean of all values in the given array along axis=0: [20. 32.5 55. ] The mean of all values in the given array along axis=1: [26.66666667 45. ]

**Example3**

The dtype argument can be used to retrieve the specific data type for the output.

# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[5, 25, 50],[35, 40, 60]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array, axis=0 and dtype=complex as the argument to the mean() function # of numpy module to calculate the mean of all values in the given array # along axis=0 and datatype as complex and print the result. print("The mean of all values in the given array along axis=0 and datatype as complex =") print(np.mean(gvn_arry, axis=0, dtype=complex))

**Output:**

The above given array is: [[ 5 25 50] [35 40 60]] The mean of all values in the given array along axis=0 and datatype as complex = [20. +0.j 32.5+0.j 55. +0.j]