Mathew K Analytics

Lesson 38 · Probability and Statistics in python

Hypothesis Testing Explained: Applying Statistical Methods to Real-World Data

Welcome to this beginner-friendly hands-on project! You will learn how to use Python to do basic probability and hypothesis testing. We will use real data…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Project: Hypothesis Testing on Real-World Datasets#

Welcome to this beginner-friendly hands-on project!

You will learn how to use Python to do basic probability and hypothesis testing.

We will use real data from a restaurant's tips to test ideas about how people tip.

You do not need any prior experience. Let's start this journey into data!

# Data setup
import warnings; warnings.filterwarnings("ignore")
import pandas as pd
import seaborn as sns
import numpy as np
np.random.seed(42)
tips = sns.load_dataset("tips")  # Load sample tips dataset
print("Shape of the dataset:", tips.shape)
print("")
print("First few rows:")
print(tips.head())
Shape of the dataset: (244, 7)

First few rows:
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

What data are we using?#

This dataset has information about restaurant bills, tip amounts, days, and whether the diner is male or female.

Each row is a meal served in a US restaurant. You can see columns for total bill, tip, sex, day, and time.

Today, we will use this dataset to check if people tip differently based on certain factors.

# Basic data summary
print("Columns:", tips.columns.tolist())
print("")
print("Missing values in each column:")
print(tips.isnull().sum())
Columns: ['total_bill', 'tip', 'sex', 'smoker', 'day', 'time', 'size']

Missing values in each column:
total_bill    0
tip           0
sex           0
smoker        0
day           0
time          0
size          0
dtype: int64
# Remove rows with missing values just in case
tips = tips.dropna()
print("New data shape after dropping missing values:", tips.shape)
New data shape after dropping missing values: (244, 7)

Problem Statement#

Suppose we want to answer: Do male and female customers tip differently on average?

We will use tip and sex columns for this.

This is a perfect place to use a hypothesis test.

# Visualize the distribution of tips by sex
import matplotlib.pyplot as plt
sns.histplot(data=tips, x="tip", hue="sex", element="step", stat="density", common_norm=False, bins=20, palette="muted")
plt.title("Tip Amounts by Sex")
plt.xlabel("Tip Amount (USD)")
plt.ylabel("Density")
plt.legend(title="Sex")
plt.show()
No description has been provided for this image

Hypothesis Testing#

A hypothesis test lets us check if any observed tip difference between male and female diners is likely to be real or just happened by chance.

Our 'null hypothesis' says both groups tip the same, on average.

Our 'alternative hypothesis' says males and females tip differently, on average.

# Quick look at average tip for each group
avg_tips = tips.groupby("sex")["tip"].mean()
print("Average tip by sex:")
print(avg_tips)
Average tip by sex:
sex
Male      3.089618
Female    2.833448
Name: tip, dtype: float64
# Two-sample t-test for difference in means
from scipy.stats import ttest_ind
male_tip = tips.loc[tips.sex == "Male", "tip"]
female_tip = tips.loc[tips.sex == "Female", "tip"]
t_stat, p_value = ttest_ind(male_tip, female_tip)
print("T-statistic:", t_stat)
print("P-value:", p_value)
T-statistic: 1.387859705421269
P-value: 0.16645623503456755
# Interpret results
alpha = 0.05
if p_value < alpha:
    print("We reject the null hypothesis. There is evidence that males and females tip differently.")
else:
    print("We cannot reject the null hypothesis. There is not enough evidence of a difference.")
    
We cannot reject the null hypothesis. There is not enough evidence of a difference.

Why does this work?#

The t-test checks if averages are probably different in reality.

Random data can be misleading. The t-test helps us know if our results could just be luck.

A p-value below 0.05 means there is less than a one in twenty chance the result is just randomness.

Note: If groups are very unequal or non-normal, more complex methods can be used.

# Practice: Try it yourself!
col = input("Choose a column to compare by (sex, smoker, time, day): ")
grp1 = input("Enter the first group name as shown in the dataset (example: Male): ")
grp2 = input("Enter the second group name: ")

data1 = tips.loc[tips[col] == grp1, "tip"]
data2 = tips.loc[tips[col] == grp2, "tip"]

t_stat2, p2 = ttest_ind(data1, data2)
print(f"Comparing average tips for {grp1} versus {grp2}")
print("t-statistic:", t_stat2)
print("p-value:", p2)
if p2 < 0.05:
    print("Result: There is a significant difference!")
else:
    print("Result: There is no significant difference.")
    
Comparing average tips for Yes versus No
t-statistic: 0.09222805186888201
p-value: 0.9265931522244976
Result: There is no significant difference.
# Mini project: Test if day of week affects tipping
for d in tips["day"].unique():
    tip_for_day = tips.loc[tips["day"] == d, "tip"]
    print(f"{d}: mean tip = {tip_for_day.mean():.2f}, count = {tip_for_day.count()}")
    
Sun: mean tip = 3.26, count = 76
Sat: mean tip = 2.99, count = 87
Thur: mean tip = 2.77, count = 62
Fri: mean tip = 2.73, count = 19
# ANOVA: Are tips different across days?
from scipy.stats import f_oneway
day_tips = [tips.loc[tips["day"] == d, "tip"] for d in tips["day"].unique()]
f_stat, pval_day = f_oneway(*day_tips)
print("F-statistic:", f_stat)
print("P-value:", pval_day)
F-statistic: 1.6723551980998697
P-value: 0.17358855530405373
# Boxplot of tips by day to visualize results
plt.figure(figsize=(8,5))
sns.boxplot(data=tips, x="day", y="tip", palette="Set3")
plt.title("Distribution of Tips by Day")
plt.xlabel("Day of Week")
plt.ylabel("Tip (USD)")
plt.show()
No description has been provided for this image

Extra Tips for Hypothesis Testing#

  • Always check for missing data first.
  • Choose the test that fits your question: t-test for two groups, ANOVA for more.
  • Visualize your data before and after testing.
  • Do not forget to consider random chance!

Keep practicing with different questions and datasets.

# Troubleshooting: Common mistakes
try:
    nonexist = tips.loc[tips['bad_col'] == 'broken', 'tip']
except Exception as e:
    print("Error: column not found. Always check spelling and column names.")
    print("More info:", e)
    
Error: column not found. Always check spelling and column names.
More info: 'bad_col'

Challenge: Your Turn!#

Try running your own hypothesis test on this tips dataset.

Can you find any other interesting differences? For example, compare lunch and dinner tips or smoker and non-smoker tips.

Share your results or questions in the comments, and do not forget to subscribe for more tutorials!

Recap and Next Steps#

Great job! You learned how to load real data, clean it, visualize it, and run basic hypothesis tests.

Hypothesis testing lets us check if two or more groups are probably different in reality, not just by random chance.

Try these steps on your own data or other datasets.

See you in the next video and happy learning!

Thank you for learning with us!#

If you found this lesson helpful, please click the like button and subscribe to our YouTube channel for more beginner Python and data science projects!

Comment your favorite part or share your own tip analysislet's keep learning together!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.