Tips for Mastering Descriptive Statistics

fimg-descr-stats

Descriptive statistics is any data analysis to get a simple summary and information about the dataset you’re working with. Before you go ahead with more involved analysis, it’s essential to understand your data. With descriptive statistics, you can calculate measures of central tendency, dispersion, and relationships between variables.

In this tutorial, we’ll look at tips for calculating descriptive statistics using one of the built-in datasets in the Seaborn library. Let’s begin!

1. Understand the Dataset

Before getting to computing the statistical measures, you should become familiar with your dataset. You can understand the structure, types of variables, and any potential data quality issues before further analysis.

We’ll start by loading the tips dataset using Seaborn, a dataset with information about tips received by waitstaff in a restaurant.

import seaborn as sns
import pandas as pd

# Load the 'tips' dataset from Seaborn
df = sns.load_dataset('tips')

Using df.info(), we can check the data types and non-null counts to ensure data completeness.

# View basic information about the dataset
print(df.info())

Output:

RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   total_bill  244 non-null    float64 
 1   tip         244 non-null    float64 
 2   sex         244 non-null    category
 3   smoker      244 non-null    category
 4   day         244 non-null    category
 5   time        244 non-null    category
 6   size        244 non-null    int64   
dtypes: category(4), float64(2), int64(1)
memory usage: 7.4 KB
None

The head() method gives us a preview of the first few rows.
df_head

With the describe() method, you can get a summary of the central tendency, dispersion, and percentiles of the dataset’s distribution.

# Summary statistics
print(df.describe())

Output:

       total_bill         tip        size
count  244.000000  244.000000  244.000000
mean    19.785943    2.998279    2.569672
std      8.902412    1.383638    0.951100
min      3.070000    1.000000    1.000000
25%     13.347500    2.000000    2.000000
50%     17.795000    2.900000    2.000000
75%     24.127500    3.562500    3.000000
max     50.810000   10.000000    6.000000

2. Measure Central Tendency

Measures of central tendency help us get insights into the typical value in your dataset. The mean, median, and mode are the most common measures to identify this central point.

Let’s compute the mean, median, and mode for the total_bill column.

# Calculate the mean, median, and mode of 'total_bill'
mean_total_bill = df['total_bill'].mean()
median_total_bill = df['total_bill'].median()
mode_total_bill = df['total_bill'].mode()[0]

print(f"Mean: {mean_total_bill:.2f}")
print(f"Median: {median_total_bill:.2f}")
print(f"Mode: {mode_total_bill:.2f}")
  • The mean_total_bill, calculated using the mean() method, represents the average of all total bill values.
  • The median median_total_bill, calculated with median(), represents the middle value when the data is sorted.
  • The mode mode_total_bill, which can be found using mode()[0], is the most frequent value in the dataset.

Output:

Mean: 19.79
Median: 17.80
Mode: 13.42

3. Measure Dispersion

Understanding the spread of your data is as important as understanding the central tendency. Dispersion measures like range, variance, and standard deviation show how spread out the values in your dataset are.

Let’s calculate these measures of dispersion for the total_bill column.

# Calculate range, variance, and standard deviation of 'total_bill'
range_total_bill = df['total_bill'].max() - df['total_bill'].min()
variance_total_bill = df['total_bill'].var()
std_dev_total_bill = df['total_bill'].std()

print(f"Range: {range_total_bill:.2f}")
print(f"Variance: {variance_total_bill:.2f}")
print(f"Standard Deviation: {std_dev_total_bill:.2f}")
  • You can find the range by subtracting the minimum value from the maximum value in the total_bill column. This gives us a basic measure of spread.
  • The variance measures the average squared deviation from the mean, indicating how much the values differ from the average.
  • The standard deviation (std_dev_total_bill here) is the square root of the variance. And standard deviation provides a measure of the average distance from the mean in the same units as the data.

Together, these metrics help us understand how much the total bill amounts vary in the dataset.

Output:

Range: 47.74
Variance: 79.25
Standard Deviation: 8.90

4. Explore Relationships with Correlation

You can use correlation to understand the relationships between features. This can also help identify related or redundant features.

The correlation matrix, calculated using corr(), shows the pairwise correlation coefficients between all numerical columns in the dataset.

# Calculate correlation matrix
correlation_matrix = df.corr()

print(correlation_matrix)

Output:

            total_bill       tip      size
total_bill    1.000000  0.675734  0.598315
tip           0.675734  1.000000  0.489299
size          0.598315  0.489299  1.000000

We then visualize the correlation matrix using a heatmap.
heatmap

5. Group By and Aggregate Data

Grouping data by a categorical variable allows you to calculate statistics for specific segments of your data.

For the tips dataset, we can calculate the average total bill for each day.

# Group by 'day' and calculate mean total bill
grouped_data = df.groupby('day')['total_bill'].mean().reset_index()
print(grouped_data)

Output:

    day  total_bill
0  Thur   17.682742
1   Fri   17.151579
2   Sat   20.441379
3   Sun   21.410000

Wrapping Up

Descriptive statistics is an important step in any data analysis. By thoroughly understanding your data through central tendency, dispersion, correlation, and visualizations, you can gain some insights before moving on to in-depth analyses. 

Happy coding!

2 Replies to “Tips for Mastering Descriptive Statistics”

Leave a Reply

Your email address will not be published. Required fields are marked *