Sunday, 19 December 2021

Python program to calculate mean, Standard Deviation, frequencies of numbers

Python provides some built-in functions to do statistics. These built-in functions are more useful when you analyzing your data sets. The statistics module was new in Python 3.4. Click here to see list of functions available in this module and also please go throw once how to use each and every function.

Here I am presenting some functions by constraining to our requirement. If you to learn all functions you can to throw the link provided above and it is good to lean all these functions.

# Python code to demonstrate stdev() function

# importing Statistics module
import statistics

# creating a simple data - set
sample = [1, 2, 3, 4, 5]

# Prints standard deviation
# xbar is set to default value of 1
print("Standard Deviation of sample is % s "% (statistics.stdev(sample)))

The output is

Standard Deviation of sample is 1.5811388300841898 

Python program for calculating Mean

# Python program to demonstrate mean()
# function from the statistics module

# Importing the statistics module
import statistics

# list of positive integer numbers
data1 = [1, 3, 4, 5, 7, 9, 2]

x = statistics.mean(data1)

# Printing the mean
print("Mean is :", x)


Output:

Mean is : 4.428571428571429

Python program for calculating frequencies of numbers 

def CountFrequency(my_list):

	# Creating an empty dictionary
	freq = {}
	for item in my_list:
		if (item in freq):
			freq[item] += 1
		else:
			freq[item] = 1

	for key, value in freq.items():
		print ("% d : % d"%(key, value))

# Driver function
if __name__ == "__main__":
	my_list =[1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]

	CountFrequency(my_list)

Output:

1 :  5
 5 :  2
 3 :  3
 4 :  3
 2 :  4

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top