

# Central tendency measures: mean, median, and mode

Measures of central tendency are statistical values that describe the center of a data set. These measures help us understand the typical value of a data set. The three most common measures of central tendency are the mean, median, and mode.

The mean is the average of all the values in a data set. It is calculated by summing all the values and then dividing by the number of values. The formula for calculating the mean is:

$$mean = \frac{\sum_{i=1}^{n} x_i}{n}$$

The median is the middle value of a data set when the values are arranged in ascending order. If the data set has an odd number of values, the median is the middle value. If the data set has an even number of values, the median is the average of the two middle values. The formula for calculating the median is:

$$median = \frac{x_{n/2} + x_{(n+1)/2}}{2}$$

The mode is the value that appears most frequently in a data set. If there is more than one value that appears the same number of times, the mode is the smallest of these values.

Let's calculate these measures using Python:

```python
import numpy as np

data = [1, 2, 3, 4, 5]

mean = np.mean(data)
median = np.median(data)
mode = np.mode(data)

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
```

This code calculates the mean, median, and mode of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Mean: 3.0
Median: 3.0
Mode: [1 2 3 4 5]
```

## Exercise
Calculate the mean, median, and mode of the following data set:

```python
data = [1, 2, 2, 3, 3, 4, 4, 5]
```



# The Numpy library for numerical computing

Numpy is a powerful library in Python that provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. Numpy is widely used in scientific computing, data analysis, and machine learning.

In this section, we will explore how Numpy can be used to calculate measures of central tendency and variability. We will cover the following topics:

- Array creation and concatenation
- Array properties and functions
- Array indexing

Let's start by importing the Numpy library:

```python
import numpy as np
```

Now we can create arrays using Numpy and calculate measures of central tendency and variability.



# Importing data using Pandas

Pandas is another powerful library in Python that provides data structures and functions needed to manipulate and analyze data. It is commonly used for data cleaning, data manipulation, and data analysis.

In this section, we will learn how to import data using Pandas and how to work with data frames, which are two-dimensional labeled data structures with columns of potentially different types.

Let's start by importing the Pandas library:

```python
import pandas as pd
```

Now we can import data from various sources, such as CSV files, Excel files, and SQL databases.



# Calculating measures of central tendency with Python

- Calculating the mean
- Calculating the median
- Calculating the mode

Let's start by calculating the mean of a data set:

```python
data = [1, 2, 3, 4, 5]
mean = sum(data) / len(data)
print("Mean:", mean)
```

This code calculates the mean of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Mean: 3.0
```

Now let's calculate the median of the same data set:

```python
data = [1, 2, 3, 4, 5]
data.sort()
median = data[len(data) // 2]
print("Median:", median)
```

This code calculates the median of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Median: 3
```

Finally, let's calculate the mode of the same data set:

```python
from collections import Counter

data = [1, 2, 2, 3, 3, 4, 4, 5]
counter = Counter(data)
mode = min(counter.items(), key=lambda x: x[1])[0]
print("Mode:", mode)
```

This code calculates the mode of the data set `[1, 2, 2, 3, 3, 4, 4, 5]`. The output will be:

```
Mode: 2
```

In the next section, we will learn about measures of variability: range, variance, and standard deviation.

# Variability measures: range, variance, and standard deviation

Measures of variability help us understand the spread of a data set. They provide information about how the values in a data set are distributed. The three most common measures of variability are the range, variance, and standard deviation.

The range is the difference between the maximum and minimum values in a data set. The formula for calculating the range is:

$$range = max - min$$

The variance is the average of the squared differences from the mean. It measures how much the values in a data set deviate from the mean. The formula for calculating the variance is:

$$variance = \frac{\sum_{i=1}^{n} (x_i - mean)^2}{n}$$

The standard deviation is the square root of the variance. It is a measure of how spread out the values in a data set are. The formula for calculating the standard deviation is:

$$standard\ deviation = \sqrt{\frac{\sum_{i=1}^{n} (x_i - mean)^2}{n}}$$

Let's calculate these measures using Python:

```python
import numpy as np

data = [1, 2, 3, 4, 5]

mean = np.mean(data)
range_ = max(data) - min(data)
variance = np.var(data)
std_dev = np.std(data)

print("Range:", range_)
print("Variance:", variance)
print("Standard Deviation:", std_dev)
```

This code calculates the range, variance, and standard deviation of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Range: 4
Variance: 2.0
Standard Deviation: 1.4142135623730951
```

In the next section, we will learn about the Statistics module in Python.

# The Statistics module in Python

The Statistics module in Python provides a collection of statistical functions. It is part of the Python Standard Library and does not require any additional installation.

In this section, we will learn how to use the Statistics module to calculate measures of central tendency and variability. We will cover the following topics:

- Calculating the mean
- Calculating the median
- Calculating the mode
- Calculating the range
- Calculating the variance
- Calculating the standard deviation

Let's start by calculating the mean of a data set using the Statistics module:

```python
import statistics as stats

data = [1, 2, 3, 4, 5]
mean = stats.mean(data)
print("Mean:", mean)
```

This code calculates the mean of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Mean: 3.0
```

Now let's calculate the median of the same data set using the Statistics module:

```python
data = [1, 2, 3, 4, 5]
median = stats.median(data)
print("Median:", median)
```

This code calculates the median of the data set `[1, 2, 3, 4, 5]`. The output will be:

```
Median: 3
```

Finally, let's calculate the mode of the same data set using the Statistics module:

```python
data = [1, 2, 2, 3, 3, 4, 4, 5]
mode = stats.mode(data)
print("Mode:", mode)
```

This code calculates the mode of the data set `[1, 2, 2, 3, 3, 4, 4, 5]`. The output will be:

```
Mode: 2
```

In the next section, we will learn how to apply measures of central tendency and variability to real-world data.

# Applying measures of central tendency and variability to real-world data

Let's start by importing the Pandas library:

```python
import pandas as pd
```

Now let's import data from a CSV file:

```python
data = pd.read_csv("data.csv")
print(data.head())
```

This code imports data from a CSV file named "data.csv" and prints the first five rows of the data.

Now let's calculate the mean, median, and mode for each column of the data:

```python
mean = data.mean()
median = data.median()
mode = data.mode()

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode)
```

This code calculates the mean, median, and mode for each column of the data and prints the results.

In the next section, we will learn about hypothesis testing and confidence intervals.

# Hypothesis testing and confidence intervals

Hypothesis testing and confidence intervals are statistical methods used to make inferences about a population based on a sample.

In this section, we will learn how to perform hypothesis testing and calculate confidence intervals using Python.

# Advanced topics: weighted data, robust statistics



# Conclusion and further resources

In this textbook, we have learned about the role of Python in calculating measures of central tendency and variability. We have covered topics such as the Numpy library for numerical computing, importing data using Pandas, calculating measures of central tendency and variability, and advanced topics such as weighted data and robust statistics.

If you would like to continue learning about these topics, here are some further resources:

- Numpy documentation: https://numpy.org/doc/stable/
- Pandas documentation: https://pandas.pydata.org/docs/
- Python Statistics module documentation: https://docs.python.org/3/library/statistics.html
- Hypothesis testing and confidence intervals: https://www.statisticshowto.com/hypothesis-testing/
- Weighted data and robust statistics: https://www.statisticshowto.com/robust-statistics/

Thank you for reading this textbook. We hope you have gained a deeper understanding of the role of Python in calculating measures of central tendency and variability.