The Python statistics.pvariance function returns the population variance of data, a non-empty sequence or iterable of real-valued numbers. The mean is the measure of the variability (spread or dispersion) of the data.
In this function, if the optional argument 'mu' is given, it should be the population mean of the data. It can also be used to compute the second argument around a point that is not the mean.
We use this function to calculate the variance for the entire population. This function throws a StatisticsError if the data is empty.
SyntaxFollowing is the basic syntax statistics.pvariance() function −
statistics.pvariance(data, mu=None)Parameters
Here, the data values can be used as any sequence, list or iterator and mu is an optional argument that varies the entire population.
Return ValueThis function returns a non-empty sequence or iterable of real valued numbers.
Example 1Now, we are calculating the population variance from the given dataset using statistics.pvariance() function.
import statistics x = [3.0, 5.5, 7.9, 9.01, 1.23, 3.24] variance = statistics.pvariance(x) print(f"The Population Variance is: {variance:.4f}")Output
This produces the following result −
The population variance is: 7.6747Example 2
In the below example, we are calculating the optional second argument 'mu' to avoid recalculations using statistics.pvariance() function.
import statistics x = [3.2, 6.5, 0.0, 1.25, 2.34, 0.13] mu = statistics.mean(x) variance = statistics.pvariance(x, mu) print(f"The Population Variance is: {variance:.4f}")Output
The output is obtained as follows −
The Population Variance is: 4.9215Example 3
Let us write a simple Python program with decimals using statistics.pvariance function.
import statistics from decimal import Decimal as D x = [D("34.56"), D("45.23"), D("12.12"), D("11.31")] variance = statistics.pvariance(x) print(f"The Population Variance is: {variance:.4f}")Output
We will get the output as follows −
The Population Variance is: 212.8412Example 4
In Population Variance, we are writing a code with fractions using statistics.pvariance() function.
import statistics from fractions import Fraction as F x = [F(2, 5), F(6, 7), F(9, 4), F(2, 1)] variance = statistics.pvariance(x) variance_float = float(variance) print(f"The Population Variance is: {variance_float:.4f}")Output
This will produce the following result −
The Population Variance is: 0.5938
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4