Statistics with Python
Statistics, in general, is the method of collection of data, tabulation, and interpretation of numerical data. It is an area of applied mathematics concern with data collection analysis, interpretation, and presentation. With statistics, we can see how data can be used to solve complex problems.
In this tutorial, we will learn about solving statistical problems with Python and will also learn the concept behind it. Let’s start by understanding some concepts that will be useful throughout the article.
Note: We will be covering descriptive statistics with the help of the statistics module provided by Python.
Understanding the Descriptive Statistics
In a layman’s term, descriptive statistics generally means describing the data with the help of some representative methods like charts, tables, Excel files, etc. The data is described in such a way that it can express some meaningful information that can also be used to find some future trends. Describing and summarizing a single variable is called univariate analysis. Describing a statistical relationship between two variables is called bivariate analysis. Describing the statistical relationship between multiple variables is called multivariate analysis.
There are two types of descriptive Statistics –
- Measure of central tendency
- Measure of variability
Measure of central tendency
The measure of central tendency is a single value that attempts to describe the whole set of data. There are three main features of central tendency –
- Median Low
- Median High
It is the sum of observations divided by the total number of observations. It is also defined as average which is the sum divided by count.
The mean() function returns the mean or average of the data passed in its arguments. If passed argument is empty, StatisticsError is raised.
The average of list values is : 2
It is the middle value of the data set. It splits the data into two halves. If the number of elements in the data set is odd then the center element is median and if it is even then the median would be the average of two central elements.
For Odd Numbers:
For Even Numbers:
median() function is used to calculate the median, i.e middle element of data. If the passed argument is empty, StatisticsError is raised.
Median of data-set 1 is 5 Median of data-set 2 is 5.9 Median of data-set 3 is 2 Median of data-set 4 is -5 Median of data-set 5 is 0.0
median_low() function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the lower of two middle elements. If the passed argument is empty, StatisticsError is raised
Median of the set is 3.5 Low Median of the set is 3
median_high() function returns the median of data in case of odd number of elements, but in case of even number of elements, returns the higher of two middle elements. If passed argument is empty, StatisticsError is raised.
Median of the set is 3.5 High Median of the set is 4
It is the value that has the highest frequency in the given data set. The data set may have no mode if the frequency of all data points is the same. Also, we can have more than one mode if we encounter two or more data points having the same frequency.
mode() function returns the number with the maximum number of occurrences. If the passed argument is empty, StatisticsError is raised.
Mode of data set 1 is 5 Mode of data set 2 is 1.3 Mode of data set 3 is 1/2 Mode of data set 4 is -2 Mode of data set 5 is black
Refer to the below article to get detailed information about averages and Measures of central tendency.
Measure of variability
Till now, we have studied the measure of central tendency but this alone is not sufficient to describe the data. To overcome this we need the measure of variability. Measure of variability is known as the spread of data or how well is our data is distributed. The most common variability measures are:
- Standard deviation
The difference between the largest and smallest data point in our data set is known as the range. The range is directly proportional to the spread of data that means the bigger the range, more the spread of data and vice versa.
Range = Largest data value – smallest data value
Maximum = 5, Minimum = 1 and Range = 4
It is defined as an average squared deviation from the mean. It is being calculated by finding the difference between every data point and the average which is also known as the mean, squaring them, adding all of them, and then dividing by the number of data points present in our data set.
where N = number of terms
u = Mean
The statistics module provides the variance() method that does all the maths behind the scene. If passed argument is empty, StatisticsError is raised.
Variance of Sample1 is 15.80952380952381 Variance of Sample2 is 3.5 Variance of Sample3 is 61.125 Variance of Sample4 is 1/45 Variance of Sample5 is 0.17613000000000006
It is defined as the square root of the variance. It is being calculated by finding the Mean, then subtract each number from the Mean which is also known as average and square the result. Adding all the values and then divide by the no of terms followed the square root.
where N = number of terms
u = Mean
stdev() method of the statistics module returns the standard deviation of the data. If passed argument is empty, StatisticsError is raised.
The Standard Deviation of Sample1 is 3.9761191895520196 The Standard Deviation of Sample2 is 1.8708286933869707 The Standard Deviation of Sample3 is 7.8182478855559445 The Standard Deviation of Sample4 is 0.41967844833872525
Refer to the below article to get detailed information about the Measure of variability.