-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
30 lines (25 loc) · 1.04 KB
/
Copy pathanalysis.py
File metadata and controls
30 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import pandas as pd
import matplotlib.pyplot as plt
# Download a real dataset
url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
data = pd.read_csv(url)
# Analyze the 'Age' column
ages = data["Age"].dropna() # dropna() removes missing values — remember this
mean_value = ages.mean()
median_value = ages.median()
std_value = ages.std()
mode_value = ages.mode()
print(f"Mean: {mean_value:.4f}")
print(f"Median: {median_value:.2f}")
print(f"SD: {std_value:.2f}")
print(f"Mode: {mode_value[0]:.2f}")
# Visual: add mean & median lines to your histogram
plt.hist(ages, bins=20, edgecolor='black', color='steelblue', alpha=0.7)
plt.axvline(mean_value, color='red', linestyle='--', label=f'Mean: {mean_value:.1f}')
plt.axvline(median_value, color='green', linestyle='--', label=f'Median: {median_value:.1f}')
plt.axvline(mode_value[0], color='orange', linestyle='--', label=f'Mode: {mode_value[0]:.1f}')
plt.title("Age Distribution — Titanic Dataset")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.legend()
plt.show()