How to calculate variance in Numpy?

Let’s see how to calculate variance in Numpy python module.
numpy variance

Variance calculations

Calculating variance in Python is straightforward, and with NumPy, it becomes even more convenient.

NumPy provides a dedicated function, var, to calculate a variance.

import numpy as np

my_array = np.array([1, 5, 7, 5, 43, 43, 8, 43, 6])

variance = np.var(my_array)
print("Variance equals: " + str(round(variance, 2)))

How to calculate population variance and sample variance

The var function in NumPy can calculate both population and sample variance. By default, it calculates the population variance. To calculate sample variance, set the ddof (Delta Degrees of Freedom) parameter to 1:

# Population variance
population_variance = np.var(my_array)

# Sample variance
sample_variance = np.var(my_array, ddof=1)

Setting ddof to 1 adjusts the denominator in the variance formula, providing an accurate estimate for the sample variance.

See also  How to extrapolate in Numpy

Check also:
how to calculate a Variance in Excel