×

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.


Related Topics

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

Linear Regression using Sklearn with Example

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.