-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_engineering.py
More file actions
283 lines (237 loc) · 11 KB
/
Copy pathfeature_engineering.py
File metadata and controls
283 lines (237 loc) · 11 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
FleetOps Anomaly Detection - Phase 5: Feature Engineering
==========================================================
Builds a feature matrix at the (truck_id, month) grain using
cross-source joins from the Logistics Operations Database.
Output: outputs/feature_matrix.csv
"""
import pandas as pd
import numpy as np
import config
def build_features():
"""Build the ML feature matrix and save to CSV."""
print("=" * 60)
print("Phase 5 - Feature Engineering")
print("=" * 60)
# ------------------------------------------------------------------
# 1. Load source data
# ------------------------------------------------------------------
print("\n[1/6] Loading source data ...")
util = pd.read_csv(config.TRUCK_UTILIZATION_FILE)
trucks = pd.read_csv(config.TRUCKS_FILE)
trips = pd.read_csv(config.TRIPS_FILE)
fuel = pd.read_csv(config.FUEL_PURCHASES_FILE)
safety = pd.read_csv(config.SAFETY_INCIDENTS_FILE)
print(f" truck_utilization_metrics : {util.shape}")
print(f" trucks : {trucks.shape}")
print(f" trips : {trips.shape}")
print(f" fuel_purchases : {fuel.shape}")
print(f" safety_incidents : {safety.shape}")
# Ensure month is datetime
util["month"] = pd.to_datetime(util["month"])
# Sort for rolling calculations
util = util.sort_values(["truck_id", "month"]).reset_index(drop=True)
# ------------------------------------------------------------------
# 2. Rolling / lag features from truck_utilization_metrics
# ------------------------------------------------------------------
print("[2/6] Computing rolling and lag features ...")
# Group by truck for rolling window calculations
grouped = util.groupby("truck_id")
# Rolling averages (use shift(1) so we don't include current month)
util["cost_rolling_3mo"] = grouped["maintenance_cost"].transform(
lambda x: x.shift(1).rolling(window=3, min_periods=1).mean()
)
util["cost_rolling_6mo"] = grouped["maintenance_cost"].transform(
lambda x: x.shift(1).rolling(window=6, min_periods=1).mean()
)
util["downtime_rolling_3mo"] = grouped["downtime_hours"].transform(
lambda x: x.shift(1).rolling(window=3, min_periods=1).mean()
)
util["maint_event_count_3mo"] = grouped["maintenance_events"].transform(
lambda x: x.shift(1).rolling(window=3, min_periods=1).sum()
)
# Lag features
util["cost_lag_1mo"] = grouped["maintenance_cost"].transform(
lambda x: x.shift(1)
)
util["cost_lag_2mo"] = grouped["maintenance_cost"].transform(
lambda x: x.shift(2)
)
# Utilization rate (current month - already present)
# Total revenue monthly
util["total_revenue_monthly"] = util["total_revenue"]
# ------------------------------------------------------------------
# 3. Truck age from trucks table
# ------------------------------------------------------------------
print("[3/6] Computing truck age ...")
trucks_lookup = trucks[["truck_id", "model_year"]].drop_duplicates()
util = util.merge(trucks_lookup, on="truck_id", how="left")
# Extract year from month column to compute age
util["year"] = util["month"].dt.year
util["truck_age_years"] = util["year"] - util["model_year"]
# Month of year for seasonality
util["month_of_year"] = util["month"].dt.month
# ------------------------------------------------------------------
# 4. Fuel cost per mile from trips + fuel_purchases
# ------------------------------------------------------------------
print("[4/6] Computing fuel cost per mile (this may take a moment) ...")
# Parse dates
trips["dispatch_date"] = pd.to_datetime(trips["dispatch_date"])
fuel["purchase_date"] = pd.to_datetime(fuel["purchase_date"])
# Create month column for aggregation
trips["trip_month"] = trips["dispatch_date"].dt.to_period("M")
fuel["fuel_month"] = fuel["purchase_date"].dt.to_period("M")
# Aggregate trips by truck and month
trip_agg = trips.groupby(["truck_id", "trip_month"]).agg(
trip_distance_sum=("actual_distance_miles", "sum"),
trip_count_monthly=("trip_id", "count"),
avg_trip_distance=("actual_distance_miles", "mean"),
).reset_index()
trip_agg["trip_month"] = trip_agg["trip_month"].dt.to_timestamp()
# Aggregate fuel by truck and month
fuel_agg = fuel.groupby(["truck_id", "fuel_month"]).agg(
fuel_cost_sum=("total_cost", "sum"),
).reset_index()
fuel_agg["fuel_month"] = fuel_agg["fuel_month"].dt.to_timestamp()
# Merge trip and fuel aggregates
trip_fuel = trip_agg.merge(
fuel_agg,
left_on=["truck_id", "trip_month"],
right_on=["truck_id", "fuel_month"],
how="left",
)
trip_fuel["fuel_cost_per_mile"] = (
trip_fuel["fuel_cost_sum"] / trip_fuel["trip_distance_sum"]
)
# Handle division by zero / missing
trip_fuel["fuel_cost_per_mile"] = trip_fuel["fuel_cost_per_mile"].replace(
[np.inf, -np.inf], np.nan
)
# Merge into main dataframe
util = util.merge(
trip_fuel[["truck_id", "trip_month", "fuel_cost_per_mile",
"trip_count_monthly", "avg_trip_distance"]],
left_on=["truck_id", "month"],
right_on=["truck_id", "trip_month"],
how="left",
)
# ------------------------------------------------------------------
# 5. Safety incident flag (any incident in last 3 months)
# ------------------------------------------------------------------
print("[5/6] Computing safety incident flags ...")
safety["incident_date"] = pd.to_datetime(safety["incident_date"])
safety["incident_month"] = safety["incident_date"].dt.to_period("M").dt.to_timestamp()
# Get unique truck-month combinations with incidents
incident_months = safety.groupby(["truck_id", "incident_month"]).size().reset_index(
name="incident_count"
)
# For each (truck_id, month) in util, check if there was any incident
# in the current or previous 2 months (3-month window)
def compute_safety_flag(df):
"""Compute 3-month rolling safety incident flag per truck."""
all_months = df[["truck_id", "month"]].drop_duplicates()
results = []
for _, row in all_months.iterrows():
tid = row["truck_id"]
m = row["month"]
# 3-month lookback window: current month and 2 prior months
start = m - pd.DateOffset(months=2)
truck_incidents = incident_months[
(incident_months["truck_id"] == tid)
& (incident_months["incident_month"] >= start)
& (incident_months["incident_month"] <= m)
]
results.append({
"truck_id": tid,
"month": m,
"safety_incident_flag": 1 if len(truck_incidents) > 0 else 0,
})
return pd.DataFrame(results)
# Vectorized approach for better performance
# Create a cross-join of truck-month with incident months
util_keys = util[["truck_id", "month"]].drop_duplicates().copy()
util_keys["_key"] = 1
# For each util row, check incidents in [month-2, month]
safety_flags = []
for truck_id in util_keys["truck_id"].unique():
truck_util = util_keys[util_keys["truck_id"] == truck_id].copy()
truck_incidents = incident_months[
incident_months["truck_id"] == truck_id
]["incident_month"].values
if len(truck_incidents) == 0:
truck_util["safety_incident_flag"] = 0
safety_flags.append(truck_util[["truck_id", "month", "safety_incident_flag"]])
continue
flags = []
for _, row in truck_util.iterrows():
m = row["month"]
start = m - pd.DateOffset(months=2)
has_incident = any(
(inc >= start) and (inc <= m)
for inc in pd.to_datetime(truck_incidents)
)
flags.append(1 if has_incident else 0)
truck_util["safety_incident_flag"] = flags
safety_flags.append(truck_util[["truck_id", "month", "safety_incident_flag"]])
safety_flag_df = pd.concat(safety_flags, ignore_index=True)
util = util.merge(safety_flag_df, on=["truck_id", "month"], how="left")
# ------------------------------------------------------------------
# 6. Target variable: high_cost_next_month
# ------------------------------------------------------------------
print("[6/6] Creating target variable ...")
# Compute the threshold (75th percentile of all monthly maintenance costs)
cost_threshold = util["maintenance_cost"].quantile(
config.HIGH_COST_PERCENTILE / 100.0
)
print(f" Maintenance cost {config.HIGH_COST_PERCENTILE}th percentile: ${cost_threshold:,.2f}")
# Shift maintenance_cost forward by 1 month per truck to get "next month's cost"
util["maintenance_cost_next_month"] = util.groupby("truck_id")[
"maintenance_cost"
].shift(-1)
# Binary target
util["high_cost_next_month"] = (
util["maintenance_cost_next_month"] > cost_threshold
).astype(float)
# Drop rows where target is NaN (last month per truck)
rows_before = len(util)
util = util.dropna(subset=["high_cost_next_month"]).reset_index(drop=True)
util["high_cost_next_month"] = util["high_cost_next_month"].astype(int)
rows_dropped = rows_before - len(util)
print(f" Dropped {rows_dropped} rows with NaN target (last month per truck)")
# ------------------------------------------------------------------
# Select final feature columns
# ------------------------------------------------------------------
feature_cols = [
"truck_id", "month", "year",
"cost_rolling_3mo", "cost_rolling_6mo",
"downtime_rolling_3mo", "utilization_rate",
"maint_event_count_3mo",
"cost_lag_1mo", "cost_lag_2mo",
"truck_age_years", "month_of_year",
"fuel_cost_per_mile", "trip_count_monthly", "avg_trip_distance",
"safety_incident_flag",
"total_revenue_monthly",
"maintenance_cost_next_month",
"high_cost_next_month",
]
feature_matrix = util[feature_cols].copy()
# ------------------------------------------------------------------
# Save output
# ------------------------------------------------------------------
output_path = config.OUTPUT_DIR / "feature_matrix.csv"
feature_matrix.to_csv(output_path, index=False)
print(f"\nFeature matrix saved to: {output_path}")
print(f" Shape: {feature_matrix.shape}")
print("\nTarget distribution (high_cost_next_month):")
dist = feature_matrix["high_cost_next_month"].value_counts()
total = len(feature_matrix)
for val in sorted(dist.index):
count = dist[val]
pct = count / total * 100
print(f" {val}: {count} ({pct:.1f}%)")
print("\n" + "=" * 60)
print("Feature engineering complete.")
print("=" * 60)
return feature_matrix
if __name__ == "__main__":
build_features()