-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_analysis.py
More file actions
204 lines (177 loc) · 7.44 KB
/
Copy pathdata_analysis.py
File metadata and controls
204 lines (177 loc) · 7.44 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Data Analysis Script - Manila Transit Analytics
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Configure directories
INPUT_PATH = os.path.join('data', '2_cleaned', 'manila_transit_cleaned.csv')
FIGURES_DIR = os.path.join('output', 'figures')
os.makedirs(FIGURES_DIR, exist_ok=True)
# Set visual style
sns.set_theme(style="whitegrid")
plt.rcParams.update({
'font.size': 11,
'axes.labelsize': 12,
'axes.titlesize': 14,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'figure.titlesize': 16,
'figure.dpi': 150
})
# Custom premium palette
PALETTE = {
'EDSA Carousel': '#E74C3C', # Crimson Red
'MRT-3': '#3498DB', # Blue
'LRT-2': '#9B59B6', # Purple
'LRT-1': '#2ECC71' # Green
}
def load_cleaned_data(path: str) -> pd.DataFrame:
"""Loads the cleaned dataset and checks its size."""
if not os.path.exists(path):
raise FileNotFoundError(f"Cleaned dataset not found at {path}. Please run data_cleaning.py first.")
df = pd.read_csv(path)
# Convert station names to uppercase for consistent visual display in charts and tables
if 'Station_From' in df.columns:
df['Station_From'] = df['Station_From'].astype(str).str.upper()
print(f"[LOAD] Loaded {len(df):,} cleaned records.")
return df
def analyze_overall_performance(df: pd.DataFrame):
"""Answers RQ1: Overall Line Performance."""
df['Delay'] = df['Actual_Wait_Time'] - df['Scheduled_Interval']
summary = df.groupby('Transit_Line').agg(
avg_scheduled=('Scheduled_Interval', 'mean'),
avg_actual=('Actual_Wait_Time', 'mean'),
avg_delay=('Delay', 'mean')
).reset_index()
print("\n=== RQ1: OVERALL LINE PERFORMANCE ===")
print(summary.to_string(index=False, formatters={
'avg_scheduled': '{:.2f} mins'.format,
'avg_actual': '{:.2f} mins'.format,
'avg_delay': '{:.2f} mins'.format
}))
# Plot average wait times vs scheduled
plt.figure(figsize=(8, 5))
df_melt = df.melt(
id_vars=['Transit_Line'],
value_vars=['Scheduled_Interval', 'Actual_Wait_Time'],
var_name='Time_Type', value_name='Minutes'
)
df_melt['Time_Type'] = df_melt['Time_Type'].map({
'Scheduled_Interval': 'Scheduled Interval',
'Actual_Wait_Time': 'Actual Wait Time'
})
sns.barplot(
data=df_melt, x='Transit_Line', y='Minutes', hue='Time_Type',
palette=['#BDC3C7', '#2C3E50'], edgecolor='none', alpha=0.9
)
plt.title("Scheduled vs. Actual Wait Time by Transit Line", pad=15)
plt.xlabel("Transit Line")
plt.ylabel("Time (minutes)")
plt.legend(title=None, frameon=True)
plt.tight_layout()
fig_path = os.path.join(FIGURES_DIR, 'average_wait_time_by_line.png')
plt.savefig(fig_path, dpi=300)
plt.close()
print(f"[PLOT] Saved average wait time by line to '{fig_path}'")
def analyze_peak_impact(df: pd.DataFrame):
"""Answers RQ2: Peak Hour Impact."""
# Define peak period categorization
peak_map = {
'Peak Morning': 'Peak',
'Peak Evening': 'Peak',
'Mid-Day': 'Off-Peak',
'Late Night': 'Off-Peak'
}
df['Period_Type'] = df['Time_of_Day'].map(peak_map)
summary = df.groupby(['Transit_Line', 'Period_Type'])['Actual_Wait_Time'].mean().unstack()
summary['Pct_Increase'] = ((summary['Peak'] - summary['Off-Peak']) / summary['Off-Peak']) * 100
print("\n=== RQ2: PEAK HOUR IMPACT ===")
print(summary.to_string(formatters={
'Peak': '{:.2f} mins'.format,
'Off-Peak': '{:.2f} mins'.format,
'Pct_Increase': '{:.1f}%'.format
}))
# Plot Peak vs Off-Peak
plt.figure(figsize=(8, 5))
sns.barplot(
data=df, x='Transit_Line', y='Actual_Wait_Time', hue='Period_Type',
hue_order=['Off-Peak', 'Peak'], palette=['#7F8C8D', '#D35400'], alpha=0.9
)
plt.title("Average Wait Time: Peak vs. Off-Peak Periods", pad=15)
plt.xlabel("Transit Line")
plt.ylabel("Actual Wait Time (minutes)")
plt.legend(title="Period Type", frameon=True)
plt.tight_layout()
fig_path = os.path.join(FIGURES_DIR, 'peak_vs_offpeak_comparison.png')
plt.savefig(fig_path, dpi=300)
plt.close()
print(f"[PLOT] Saved peak vs off-peak comparison to '{fig_path}'")
def analyze_weather_impact(df: pd.DataFrame):
"""Answers RQ3: Weather Vulnerability."""
# Group by line and weather condition
summary = df.groupby(['Transit_Line', 'Weather_Condition'])['Actual_Wait_Time'].mean().unstack()
# Percentage increase during heavy rain compared to dry conditions (Clear & Cloudy average)
summary['Dry_Avg'] = (summary['Clear'] + summary['Cloudy']) / 2
summary['Pct_Increase_Rain'] = ((summary['Heavy Rain'] - summary['Dry_Avg']) / summary['Dry_Avg']) * 100
print("\n=== RQ3: WEATHER VULNERABILITY ===")
print(summary.to_string(formatters={
'Clear': '{:.2f} mins'.format,
'Cloudy': '{:.2f} mins'.format,
'Heavy Rain': '{:.2f} mins'.format,
'Dry_Avg': '{:.2f} mins'.format,
'Pct_Increase_Rain': '{:.1f}%'.format
}))
# Plot Weather Impact
plt.figure(figsize=(9, 5.5))
sns.barplot(
data=df, x='Transit_Line', y='Actual_Wait_Time', hue='Weather_Condition',
hue_order=['Clear', 'Cloudy', 'Heavy Rain'],
palette=['#F1C40F', '#95A5A6', '#2980B9'], alpha=0.9
)
plt.title("Average Wait Time by Weather Condition", pad=15)
plt.xlabel("Transit Line")
plt.ylabel("Actual Wait Time (minutes)")
plt.legend(title="Weather", frameon=True)
plt.tight_layout()
fig_path = os.path.join(FIGURES_DIR, 'weather_impact_by_line.png')
plt.savefig(fig_path, dpi=300)
plt.close()
print(f"[PLOT] Saved weather impact by line to '{fig_path}'")
def analyze_worst_stations(df: pd.DataFrame):
"""Answers RQ4: Worst Stations."""
df['Delay'] = df['Actual_Wait_Time'] - df['Scheduled_Interval']
station_delays = df.groupby(['Transit_Line', 'Station_From'])['Delay'].mean().reset_index()
top_5_worst = station_delays.sort_values(by='Delay', ascending=False).head(5)
print("\n=== RQ4: TOP 5 WORST PERFORMING STATIONS ===")
print(top_5_worst.to_string(index=False, formatters={
'Delay': '{:.2f} mins'.format
}))
# Plot Top 5 Worst Stations
plt.figure(figsize=(9, 5))
top_5_worst['Station_Label'] = top_5_worst['Station_From'] + " (" + top_5_worst['Transit_Line'] + ")"
sns.barplot(
data=top_5_worst, y='Station_Label', x='Delay', hue='Transit_Line',
palette=PALETTE, dodge=False, alpha=0.9
)
plt.title("Top 5 Stations with Highest Average Delay", pad=15)
plt.ylabel("Station (Transit Line)")
plt.xlabel("Average Delay (Actual - Scheduled, minutes)")
plt.legend(title="Transit Line", frameon=True)
plt.tight_layout()
fig_path = os.path.join(FIGURES_DIR, 'top_5_worst_stations.png')
plt.savefig(fig_path, dpi=300)
plt.close()
print(f"[PLOT] Saved top 5 worst stations to '{fig_path}'")
if __name__ == "__main__":
try:
cleaned_df = load_cleaned_data(INPUT_PATH)
analyze_overall_performance(cleaned_df)
analyze_peak_impact(cleaned_df)
analyze_weather_impact(cleaned_df)
analyze_worst_stations(cleaned_df)
print("\n" + "=" * 50)
print("DATA ANALYSIS COMPLETE — ALL FIGURES GENERATED")
print("=" * 50)
except Exception as e:
print(f"[ERROR] An error occurred: {e}")