Python program to find variance; Through this tutorial, i am going to show you how to find or compute variance in python.
In this tutorial, i will write a programs to compute variance in python.
Python Program to Find the Variance
See the following python programs to find variance; as shown below:
- Write a program to calculate variance in python.
- Python calculate variance without numpy
Write a program to calculate variance in python
import numpy as np dataset= [21, 11, 19, 18, 29, 46, 20] variance= np.var(dataset) print(variance)
Output
108.81632653061224
Note:- Python variance() is an inbuilt function that is used to calculate the variance from the sample of data (sample is a subset of populated data). Python statistics module provides potent tools, which can be used to compute anything related to Statistics.
Python calculate variance without numpy
#define a function, to calculate variance def variance(X): mean = sum(X)/len(X) tot = 0.0 for x in X: tot = tot + (x - mean)**2 return tot/len(X) # call the function with data set x = [1, 2, 3, 4, 5, 6, 7, 8, 9] print("variance is: ", variance(x)) y = [1, 2, 3, -4, -5, -6, -7, -8] print("variance is: ", variance(y)) z = [10, -20, 30, -40, 50, -60, 70, -80] print("variance is: ", variance(z))
Output
variance is: 6.666666666666667 variance is: 16.5 variance is: 2525.0
Recommended Python Tutorials
Recommended:-Python Program to Find Square Root of Number
Recommended:-Python Program to Find Cube of Number
Recommended:-Python Palindrome Program
Recommended:-Python Program to Count Set Bits in a Number
Recommended:-Python Program for BMI Calculation
Recommended:-Python Program to Find Out Absolute Value
Recommended:-Python Random Number Generator Code
Recommended:-Python program for Zip, Zap and Zoom game
Recommended:-Python Program to Swap Two Numbers
Recommended:-Python Program to Find n-th term of Fibonacci Series
Recommended:-Python Program For Calculator
Recommended:-Python Program to Find Strong Number
Recommended:-Python Program to Convert cm to Feet and Inches
Recommended:-How to Reverse a List in Python
Recommended:-Python Program to Convert Celsius to Fahrenheit
Recommended:-Python Program to Find Standard Deviation
Recommended:-Python Program to Check Perfect Number
Recommended:-Python Program to Display Calendar
Recommended:-Python Program to Print Alphabets from A to Z