Python Statistical Module

Python Statistical Module

The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The statistical module comes with the Python installation, so we don’t need to install it externally.

Importing statistical Module

By using the following syntax in our Python program, we can import Python statistic module in a particular python program:

import statistics

After importing the statistical module in with the help of above-mentioned syntax, we are able to use various statistics function and perform statistical operations on the numeric data given to us.

In this part of tutorial, we are going to learn and use the following statistic functions in our Python program:

1. The mean() function

2. The median() function

3. The mode() function

4. The stdev() function

5. The median_low() function

6. The median_high() function

With the help of above listed statistic functions, we will perform operations on numeric data given to us. So, let’s learn about each of them one by one and understand how they work when used in our Python program.

Python Statistical Module Built-in Functions.

Following are the important built-in methods of Python Statistical Module.

1. The mean() function:

The mean() function of statistical module is used to calculate the arithmetic mean of the datasets of given number. The arithmetic mean of some given numbers is also called as average of that number. When we use mean() function in our Python program, it returns the average of dataset that we used in the function.

Syntax:

We will use the following syntax in the given example to calculate the arithmetic mean of numeric data present in example:

statistics.mean(GivenData)

Let's understand mean() function through a Python program used in following example:

Example: Calculating arithmetic mean of a given dataset of numbers using mean() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive integer numbers as a given dataset
 GivenData = [51, 26, 7, 43, 2, 67, 8, 14, 29, 53]
 # calculating mean of given dataset with mean() function
 a = statistics.mean(GivenData)    
 # Printing the mean in the output of program
 print("The Arithmetic Mean of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Arithmetic Mean of the dataset used in our Python program i.e., GivenData is : 30

Explanation: In the above program, first we have imported statistical module in our Python program. Then we have defined a dataset of number with named GivenData that contain all the positive integer numbers in it. Then we calculated the arithmetic mean of the dataset we have defined by using the mean() function on the dataset.

After calculating the mean, we have printed the result in the output of program by using print statement. As we can see in the output, the result shown to us is the arithmetic mean or the average of the dataset we have used in the above Python program i.e., GivenData.

2. The median() function:

The median() function is used to calculate statistical median of a given dataset of numbers. The median of a given dataset is the middle most value from the total numbers given to us. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the median of numeric data present in example:

statistics.median(GivenData)

Let's understand median() function through a Python program used in following example:

Example: Calculating median of a given dataset of numbers using median() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [51, 26, 7, -43, 2, 67, -8, 14, 29, 53]
 # calculating median of given dataset with median() function
 a = statistics.median(GivenData)    
 # Printing the median in the output of program
 print("The Statistical Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Median of the dataset used in our Python program i.e., GivenData is : 20.0

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the median of the dataset we have defined by using the median() function on the dataset. After calculating the median, we have printed the result in the output of program by using print statement. As we can see in the output, the result shown to us is the median of the dataset we have used in the above Python program i.e., GivenData.

3. The mode() function:

The mode() function is used to calculate the statistical mode of a given dataset of numbers. This dataset of number either can be positive or can be negative. The mode of a dataset of numbers means that the most occurring number or value from that given dataset or list of numbers.

Syntax:

We will use the following syntax in the given example to calculate the mode of numeric data present in example:

statistics.mode(GivenData)

Let's understand mode() function through a Python program used in following example:

Example: Calculating mode of a given dataset of numbers using mode() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating mode of given dataset with mode() function
 a = statistics.mode(GivenData)    
 # Printing the mode in the output of program
 print("The Statistical Mode of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Mode of the dataset used in our Python program i.e., GivenData is: 26

Explanation: In the above Python program, first we have imported statistical module in our Python program. Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the mode of the dataset we have defined by using the mode() function on the dataset. After calculating the mode, we have printed the result in the output of program by using print statement.

As we can see in the output, the result shown to us is the mode or the most occurring number from the dataset we have used in the above Python program i.e., GivenData.

4. The stdev() function:

The stdev() function uses in our Python program when we have to calculate statistical standard deviation of a given sample which is available from the list of number provided to us. This dataset of number either can be positive or can be negative.

Syntax: We will use the following syntax in the given example to calculate the standard deviation of numeric data present in example:

statistics.stdev(GivenData)

Let's understand stdev() function through a Python program used in following example:

Example: Calculating standard deviation of a given dataset of numbers using stdev() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating standard deviation of given dataset with stdev() function
 a = statistics.stdev(GivenData)    
 # Printing the standard deviation in the output of program
 print("The Statistical Standard Deviation of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Standard Deviation of the dataset used in our Python program i.e., GivenData is : 31.07626442361301

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it. Then we calculated the standard deviation of the dataset we have defined by using the stdev() function on the dataset.

 After calculating the standard deviation, we have printed the standard deviation as result in the output of program by using print statement. As we can see in the output, the result shown to us is the standard deviation of the dataset we have used in the above Python program i.e., GivenData.

5. The median_low() function:

The median_low() function we uses in our Python program when we have to calculate statistical low median of a given dataset of numbers. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the low median of numeric data present in example:

statistics.median_low(GivenData)

Let's understand median_low() function through a Python program used in following example:

Example: Calculating low median of a given dataset of numbers using median_low() function:

 # importing python statistical module in the program
 import statistics
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating low median of given dataset with median_low() function
 a = statistics.median_low(GivenData)    
 # Printing the low median in the output of program
 print("The Statistical Low Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Low Median of the dataset used in our Python program i.e., GivenData is : 26

Explanation: In the above Python program, first we have imported statistical module in our Python program.

 Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it. Then we calculated the low median of the dataset we have defined by using the median_low() function on the dataset.

After calculating the low median, we have printed the low median as result in the output of program by using print statement. As we can see in the output, the result shown to us is the low median of the dataset we have used in the above Python program i.e., GivenData.

6. The median_high() function:

The median_high() function we uses in our Python program when we have to calculate statistical high median of a given dataset of numbers. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the high median of numeric data present in example:

statistics.median_high(GivenData)

Let's understand median_high() function through a Python program used in following example:

Example: Calculating high median of a given dataset of numbers using median_high() function:

 # importing python statistical module in the program
 import statistics
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating high median of given dataset with median_high() function
 a = statistics.median_high(GivenData)    
 # Printing the high median in the output of program
 print("The Statistical High Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical High Median of the dataset used in our Python program i.e., GivenData is: 26

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the high median of the dataset we have defined by using the median_high() function on the dataset.

After calculating the high median, we have printed the high median as result in the output of program by using print statement.

 As we can see in the output, the result shown to us is the high median of the dataset we have used in the above Python program i.e., GivenData.