×

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

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Problem-solving with algorithm and data structures using Python

What is problem-solving? There is no universal method for solving problems. It's frequently a special process that balances your immediate and long-term goals with your available resources. However, several models emphasise...

3 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

Python BS4 Code

Python BS4 Code The BS4 stands for BeautifulSoup version 4.x. The BeautifulSoup is a Python library which is used for pulling out data of the HTML & XML files using the...

14 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

STL in Python

Python has many libraries that are very useful and simplify many complex codes. In the same way, STL is also one of the python libraries used for reading and writing...

3 minutes read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.