Statistics is used to collect, organize, analyze and interpret data. Python provides the statistics module, which contains built-in functions for performing common statistical calculations. Python provides a built-in statistics module for performing mathematical and statistical calculations on numerical data. Some common operations include:
- Finding averages and measures of central tendency
- Measuring the spread of data
- Calculating quantiles
- Finding relationships between two datasets
- Performing simple linear regression
Example:
import statistics
data = [10, 20, 30, 40, 50]
print(statistics.mean(data))
print(statistics.median(data))
Output
30 30
Explanation:
- import statistics loads the module.
- mean() calculates the arithmetic mean.
- median() returns the middle value of the dataset.
Measures of Central Tendency
They are used to represent a typical or central value of a dataset. Python provides several functions for calculating them. The commonly used functions are:
- mean()
- median()
- mode()
- median_low()
- median_high()
- median_grouped()
- multimode()
Example:
import statistics
data = [10, 20, 20, 30, 40]
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
print("Mode:", statistics.mode(data))
Output
Mean: 24 Median: 20 Mode: 20
Explanation:
- mean() calculates the arithmetic average.
- median() returns the middle value.
- mode() returns the most frequently occurring value.
Measures of Variability
It describe how widely the values in a dataset are distributed. Python provides functions such as:
- variance()
- pvariance()
- stdev()
- pstdev()
Example:
import statistics
data = [10, 12, 14, 16, 18]
print("Variance:", statistics.variance(data))
print("Standard Deviation:", statistics.stdev(data))
Output
Variance: 10 Standard Deviation: 3.1622776601683795
Explanation:
- variance() calculates sample variance.
- pvariance() calculates population variance.
- stdev() calculates sample standard deviation.
- pstdev() calculates population standard deviation.
Geometric Mean
It is an average calculated using the product of the values instead of their sum. It is useful for data involving growth rates, ratios and multiplicative changes.
- data contains the values for which the geometric mean is calculated.
- The function returns the geometric mean as a floating-point value.
- It is useful when values are multiplied together rather than added.
Example:
import statistics
data = [2, 8, 32]
print(statistics.geometric_mean(data))
Output
7.999999999999998
Explanation:
- Geometric mean of 2, 8 and 32 is 8.
- Floating-point representation can produce a small difference from the exact value.
Harmonic Mean
It's an average based on the reciprocals of values. It is useful when working with rates and ratios.
- data contains the values.
- weights can be used when observations have different importance.
- It is commonly used for averaging rates.
Example:
import statistics
speed = [40, 60]
print(statistics.harmonic_mean(speed))
Output
48.0
Explanation:
- Harmonic mean of 40 and 60 is 48.
- It is more suitable than the arithmetic mean for averaging rates.
Quantiles
They divide ordered data into intervals containing approximately equal numbers of observations. They are useful for understanding how data is distributed.
- n specifies the number of intervals.
- n=4 divides the dataset into quartiles.
- n=10 divides it into deciles.
- n=100 can be used for percentiles.
- Function returns the cut points between the intervals.
Example:
import statistics
data = [10, 20, 30, 40, 50, 60, 70, 80]
print(statistics.quantiles(data, n=4))
Output
[22.5, 45.0, 67.5]
Explanation:
- Returned values represent the three cut points that divide the data into four groups.
- quantiles() does not require the input data to already be sorted.
Covariance
It measures how two variables change together. It indicates whether increases in one variable are associated with increases or decreases in another variable.
- x and y must contain the same number of observations.
- A positive value indicates that the variables tend to increase together.
- A negative value indicates that one variable tends to increase when the other decreases.
Example:
import statistics
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
print(statistics.covariance(x, y))
Output
5.0
Explanation:
- Positive covariance indicates that x and y increase together.
- covariance() calculates sample covariance.
Correlation
It measures the strength and direction of the relationship between two variables.
- Default method calculates Pearson correlation.
- Result ranges from -1 to +1.
- value close to +1 indicates a strong positive relationship.
- value close to -1 indicates a strong negative relationship.
- value close to 0 indicates little or no linear relationship.
Example:
import statistics
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
print(statistics.correlation(x, y))
Output
1.0
Explanation:
- Result 1.0 represents a perfect positive linear relationship.
- Both datasets must contain the same number of observations.
Linear Regression
It's used to model the relationship between two variables. Python provides linear_regression() for calculating simple linear regression.
- x represents the independent variable.
- y represents the dependent variable.
- Function returns the slope and intercept of the regression line.
- Both datasets must have the same length.
Example:
import statistics
x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 8, 10]
result = statistics.linear_regression(x, y)
print("Slope:", result.slope)
print("Intercept:", result.intercept)
Output
Slope: 2.0 Intercept: -0.20000000000000018
Explanation:
- Slope represents the expected change in y for a one-unit increase in x.
- Intercept represents the estimated value of y when x is zero.
- Regression equation is y = 2x - 0.2.