×

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

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

An Introduction to Subprocess in Python with Examples

Subprocess in Python By starting new processes, the Python function subprocess is utilized to run extra programs and applications. It makes it possible to run brand-new apps straight from a Programming...

6 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python sklearn train_test_split

Sklearn: One of the most beneficial open-source libraries for learning algorithms in Python is, without a doubt, Sklearn or Scikit-Learn. The most effective tools for statistical modeling and machine learning are all included in...

6 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

Python Selectors

Python: 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 faster way....

4 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python pow() Function

Python pow() Function The pow() function in Python return the parameter ‘x’ to the power ‘y’ and if the parameter ‘z’ is present, it returns x to the power y, modulo z (computed more efficiently than pow(x, y) % z). Syntax pow(x, y[, z]) Parameter x: This parameter represents the base...

1 minute read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Hypothesis Testing in Python

Hypothesis Testing in python is widely used along with statistics. Many libraries in Python are very useful for statistics and machine learning. Libraries like numpy, scripy etc. help in hypothesis testing in...

12 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 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.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.