How to Use Python Statistics Mean Function

How to Use Python Statistics Mean Function

3 mins read2.3K Views Comment
clickHere
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Jan 27, 2023 13:56 IST

In this article, we will discuss what is the arithmetic mean, and how to calculate mean in python. Later in the article, we will discuss how to use statistics.mean() function in python to calculate the mean.

2023_01_MicrosoftTeams-image-158.jpg

If you are a data science or machine learning aspirant, you must know how important statistics is to learn the concepts. Statistics concepts help to answer the questions such as: what are the most important features for analysis, which performance metric we should measure, what are expected outcomes, and how to differentiate valid and noise data, etc.

In simple terms, it helps with Data Exploration, Data Cleaning, Model Selection, Evaluation, and finally, hyperparameter tuning to improve the machine learning model.

When it comes to data analysis or data exploration, we mainly use mean, median, and mode.

In this article, we will learn how to calculate the data’s mean (arithmetic mean) using Python. We have a statistics module in Python from which we will import mean().

Now, let’s dive into learning how to use statistics.mean() function to find the mean of the data with the help of examples.

Must Read: Modules in Python

Must Read: Maths for Data Science

What is Mean?

Mean, or Arithmetic Mean, is the sum of observations divided by the number of observations, i.e., if we have n observations x1, x2, x3,……, xn, then:

Example -1: Find the mean of 2, 3, 4, 5, 6.

Answer: 

sum of observations = 2 + 3 + 4 + 5 + 6 = 20

number of observations = 5 

Hence, Mean = 20 / 5 = 4.

Must Check: Measure of Central Tendency: Mean, Median, and Mode

What is Programming What is Python
What is Data Science What is Machine Learning

Till now, we have learned what is mean and how to calculate the mean manually. Now let’s see how Python’s mean() function is used.

How to use statistics.mean() function?

Syntax

 
import statistics
statistics.mean([data-set])
Copy code

or

 
from statistics import mean
mean([data-set])
Copy code

where,

[dataset]: list or tuple containing the set of number

Return Value

  • mean() function returns the arithmetic mean of the dataset
  • It will return TypeError if the list or the tuple doesn’t have a numeric value.
  • It will return TypeError if no argument is passed.

Now, let’s have an example.

Find the mean of the data using statistics.mean() function.

We will take the same data as in the above example.

 
#import statistics module
import statistics
data = [2, 3, 4, 5, 6]
#print mean
print("The mean of the data is:", statistics.mean(data))
Copy code
# input data
data = [2, 3, 4, 5, 6]
# initialize variables
sum_of_observations = 0
number_of_observations = 0
# now, iterate through list and find the sum of observations and number of observations
for i in data:
sum_of_observations += i
number_of_observations += 1
mean = sum_of_observations/number_of_observations
#print
print("The mean of the data is:", mean)
Copy code

Output

2023_01_image-120.jpg

Also Read: for loop in Python

How to find the mean using sum() and len() functions in Python?

First, we will define the function and then use the sum() and len() functions.

 
# define the dataset
data = [2, 3, 4, 5, 6]
#define a function to calculate the mean
def ar_mean(data):
mean = sum(data)/len(data)
return mean
# print
print("the mean of the data is:", mean)
Copy code

Output

2023_01_image-121.jpg

Must Read: def() keyword in Python

Must Read: len() function in Python

How to Calculate the mean of Dictionary in Python?

In the dictionary, the statistics.mean() only counts the key as a number and returns the mean.

 
#import statstics module
from statistics import mean
#define dictionary
data = {2:4, 3:9, 4:16, 5:25, 6:36}
#print
print("The Mean of the data is:", mean(data))
Copy code

Output

2023_01_image-122.jpg

Also Read: Python Dictionary

Till now, we have seen several example, in which we have passed list, dictionary to find the mean of the data. But what if we will not pass any argument in the statistics.mean() function. Let’s see the next example.

Calculate the mean without passing an argument.

 
#calculate mean without passing argument in statistics.mean()
from statistics import mean
mean([])
Copy code

Output

Recursion Function in Python count() Function in Python
len() Function in Python float() Function in Python
range() Function in Python lambda() Function in Python

Conclusion

In this article, we have discussed what is mean in statistics, and how to calculate mean in python. Later in the article, we will discuss how to use statistics.mean() function in python to calculate the mean.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio