Numpy var – Python NumPy var() Function

NumPy var() Function:

Numpy var: The variance along the specified axis is calculated using the NumPy var() function. The variance is a measure of the spread of the distribution. By default, the variance is calculated for the flattened array, but it can also be done for the given axis.

Syntax:

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

Parameters

a: This is required. It is an array given as input.

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

dtype: This is optional. Indicate the type that will be used to calculate the variance. The default for integer arrays is float64, but the default for float arrays is the same as the array type.

out: This is optional. Indicate the output array in which the result will be stored. It must be the same shape as the desired result.

keepdims: 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.

Return Value:

np var: If out=None, an array containing the variance is returned; otherwise, a reference to the output array is returned.

NumPy var() 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 var() function of numpy module to calculate the variance of all values in the given array
  • Store it in another variable.
  • Print the variance 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,25],[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 var() function of numpy module 
# to calculate the variance of all values in the given array 
# Store it in another variable.
rslt = np.var(gvn_arry)
# Print the variance of all values in the given array 
print("The variance of all values in the given array =", rslt)

Output:

The above given array is:
[[ 5 25]
[35 40]]
The variance of all values in the given array = 179.6875

Example2

np.var: variance is calculated over the specified axes when the axis parameter is defined.

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 var() function of numpy module to calculate the variance of all values in the given array along axis=0.
  • Store it in another variable.
  • Print the variance of all values the given array along axis=0
  • Pass the given array, axis=0 as the arguments to the var() function of numpy module to calculate the variance of all values in the given array along axis=1.
  • Store it in another variable.
  • Print the variance 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 var() function of numpy module 
# to calculate the variance of all values in the given array along axis=0
# Store it in another variable.
rslt_1 = np.var(gvn_arry, axis=0)
# Print the variance of all values the given array along axis=0
print("The variance of all values in the given array along axis=0: ", rslt_1)
# Pass the given array, axis=1 as the arguments to the var() function of numpy module 
# to calculate the variance of all values in the given array along axis=1
# Store it in another variable.
rslt_2 = np.var(gvn_arry, axis=1)
# Print the variance of all values the given array along axis=1
print("The variance 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 variance of all values in the given array along axis=0: [225. 56.25 25. ]
The variance of all values in the given array along axis=1: [338.88888889 116.66666667]

Example3

The variance can be calculated in float64 for a more accurate result.

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.
  • Pass the given array, datatype float32 as the arguments to the var() function of numpy module to calculate the variance of all values in the given array with float32 and print it.
  • Pass the given array, datatype float64 as the arguments to the var() function of numpy module to calculate the variance of all values in the given array with float64 and print it.
  • 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([10, 5, 55, 200])              
# Pass the given array, datatype float32 as the arguments to the var() function of numpy module 
# to calculate the variance of all values in the given array with float32 and print it
print("The variance of all values in the given array with float32:")
print(np.var(gvn_arry, dtype=np.float32))
# Pass the given array, datatype float64 as the arguments to the var() function of numpy module 
# to calculate the variance of all values in the given array with float64 and print it
print("The variance of all values in the given array with float64:")
print(np.var(gvn_arry, dtype=np.float64))

Output:

The variance of all values in the given array with float32:
6231.25
The variance of all values in the given array with float64:
6231.25