variance of two arrays in python

To compute the variance of two arrays (or lists) in Python, you can use the variance function from the statistics module. Here's an example:

main.py
import statistics

array1 = [1, 2, 3, 4, 5]
array2 = [6, 7, 8, 9, 10]

variance1 = statistics.variance(array1)
variance2 = statistics.variance(array2)

print("Variance of array1:", variance1)
print("Variance of array2:", variance2)
232 chars
11 lines

In this example, we first import the statistics module. We then define two arrays array1 and array2. We compute the variance of each array using the variance function and store them in the variables variance1 and variance2. Finally, we print the variances.

Output:

main.py
Variance of array1: 2.5
Variance of array2: 2.5
48 chars
3 lines

Note that the variance function expects a list or tuple as its argument, so we can use either an array or a list in this case.

gistlibby LogSnag