Model Interpretability

Author

Harriet O’Brien, Vy Tran, Saniyah Khan, Rehinatu Usman

1 Introduction

The goal of this section is to understand how the final Gradient Boosting model make predictions and to check whether the model behavior aligns with the patterns observed during EDA.

Interpretability also helps confirm that the model is using features in a reasonable way and that its errors do not follow any strong pattern.

This section includes:

  • Feature Importance
  • Partial Dependence Plots
  • Actual vs. Predicted
  • Residual Analysis

These steps provide a clear overview of how reliable and transparent the final model is.

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.inspection import PartialDependenceDisplay

2 Load Processed Features and Recreate Train–Test Split

Here we reload the processed dataset and recreate the exact same train/test split as the previous modeling file. This ensures the interpretability results match the final training conditions.

# Load the final processed feature matrix

features = pd.read_csv("../data/features_processed.csv")

# Identify the correct target column (scaled version)

target_col = "reimbursement_amount"

# Recreate X and y

y = features[target_col]
X = features.drop(columns=[target_col])

# Recreate the same train-test split used in the model file

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

# Re-train the two models for interpretability

rf = RandomForestRegressor(random_state=42)
rf.fit(X_train, y_train)

gb = GradientBoostingRegressor(random_state=42)
gb.fit(X_train, y_train)

X.head()
trip_duration_days miles_traveled total_receipts_amount cost_per_mile cost_per_day receipts_ratio miles_per_day miles_traveled_scaled total_receipts_amount_scaled trip_duration_days_scaled
0 3 93.0 1.42 3.919462 121.503333 0.003896 31.000000 -1.436568 -1.629179 -1.030280
1 1 55.0 3.60 2.292000 126.060000 0.028558 55.000000 -1.544792 -1.626243 -1.539941
2 1 47.0 17.97 2.742766 128.910000 0.139400 47.000000 -1.567576 -1.606889 -1.539941
3 2 13.0 4.67 15.655385 101.760000 0.022946 6.500000 -1.664408 -1.624801 -1.285111
4 3 88.0 5.78 4.322386 126.790000 0.015196 29.333333 -1.450808 -1.623306 -1.030280

Random Forest provides feature importance scores based on how much each variable reduces prediction error across the trees. This gives a simple but useful view of which features matter the most.

3 Feature Importance

Feature importance tells us which input variables influenced predictions the most.

3.1 Random Forest

rf_importance = rf.feature_importances_
importance_df = pd.DataFrame({
"feature": X_train.columns,
"importance": rf_importance
}).sort_values(by="importance", ascending=False)

importance_df
feature importance
2 total_receipts_amount 0.310937
8 total_receipts_amount_scaled 0.306834
9 trip_duration_days_scaled 0.097183
0 trip_duration_days 0.083328
7 miles_traveled_scaled 0.061251
1 miles_traveled 0.058532
4 cost_per_day 0.035591
5 receipts_ratio 0.022682
3 cost_per_mile 0.020036
6 miles_per_day 0.003625
plt.figure(figsize=(8,5))
sns.barplot(data=importance_df, x="importance", y="feature")
plt.title("Random Forest Feature Importance")
plt.tight_layout()
plt.show()

Higher values mean the feature had a stronger effect on predictions.

Random Forest feature importance is shown here as a supporting comparison to check consistency across tree-based models.

3.2 Gradient Boosting Feature Importance

gb_importance = gb.feature_importances_
gb_df = pd.DataFrame({
"feature": X_train.columns,
"importance": gb_importance
}).sort_values(by="importance", ascending=False)

gb_df
feature importance
8 total_receipts_amount_scaled 0.462906
2 total_receipts_amount 0.148322
0 trip_duration_days 0.109377
9 trip_duration_days_scaled 0.075527
7 miles_traveled_scaled 0.073714
1 miles_traveled 0.049045
4 cost_per_day 0.035492
5 receipts_ratio 0.023439
3 cost_per_mile 0.018935
6 miles_per_day 0.003243
plt.figure(figsize=(8,5))
sns.barplot(data=gb_df, x="importance", y="feature")
plt.title("Gradient Boosting Feature Importance")
plt.tight_layout()
plt.show()

Gradient Boosting usually focuses on fewer strong predictors. If both models highlight similar features,this increases confidence in the stability of the learned relationships.

Because several features are derived from the same underlying variables such as raw and scaled receipt amounts, feature importance values should be interpreted in relation to one another rather than individually.

4 Partial Dependence Plots (PDPs)

PDPs help show how the prediction changes when we vary one feature while keeping other constant.

features_to_plot = ["total_receipts_amount_scaled",
"miles_traveled_scaled",
"trip_duration_days"]

fig, ax = plt.subplots(figsize=(10,6))
PartialDependenceDisplay.from_estimator(
gb, X_train, features_to_plot, ax=ax
)
plt.tight_layout()
plt.show()
/opt/anaconda3/envs/compvis/lib/python3.13/site-packages/sklearn/inspection/_partial_dependence.py:717: FutureWarning: The column 0 contains integer data. Partial dependence plots are not supported for integer data: this can lead to implicit rounding with NumPy arrays or even errors with newer pandas versions. Please convert numerical featuresto floating point dtypes ahead of time to avoid problems. This will raise ValueError in scikit-learn 1.9.
  warnings.warn(

The PDPs show that predicted reimbursement increases clearly as total receipt amounts increase, confirming that receipts are the main factor of influencing reimbursement decisions.

Mileage and trip duration exhibit more gradual, monotonic effects, indicating that they contribute to reimbursement but have a lesser of a role compared to receipts.

These trends are consistent with observations made from the EDA and employee interviews, suggesting that the Gradient Boosting model is learning structured reimbursement rules rather than random or misleading relationships.

5 Actual vs Predicted

This tells us how close model predictions are to the real test values.

gb_preds = gb.predict(X_test)

plt.figure(figsize=(6,6))
plt.scatter(y_test, gb_preds, alpha=0.5)
plt.xlabel("Actual")
plt.ylabel("Predicted")
plt.title("Gradient Boosting: Actual vs Predicted")
plt.grid(True)
plt.tight_layout()
plt.show()

Most points follow a diagonal pattern, showing that the predicted reimbursement values are generally close to the actual values.

There is more variation for higher reimbursement amounts, which is expected for longer or more expensive trips. Overall, the Gradient Boosting model shows strong predictive performance without clear bias.

6 Residual Distribution + Residuals vs Predictions

Residuals help us check whether there is any bias or cosistent error pattern.

residuals = y_test - gb_preds

plt.figure(figsize=(6,4))
sns.histplot(residuals, kde=True)
plt.title("Residual Distribution")
plt.tight_layout()
plt.show()

plt.figure(figsize=(6,4))
plt.scatter(gb_preds, residuals, alpha=0.5)
plt.axhline(0, color="red", linestyle="--")
plt.xlabel("Predicted")
plt.ylabel("Residual")
plt.title("Residuals vs Predictions")
plt.tight_layout()
plt.show()

The residuals appear roughly centered around zero, with no strong bias pattern in the model’s prediction.

A slightly wider spread for higher reimbursement values suggest increased uncertainty for extreme cases, which is reasonable given that in the real-world there’s a variety of travel expenses. Overall, the residual analysis supports the reliability of the model.

7 Summary

Overall, the interpretability results are consistent with what we observed during EDA and modeling stages. The Gradient Boosting model relies heavily on receipt-related features, with trip duration and mileage providing play supporting roles, which matches how reimbursement decisions are expected to work.

Feature importance and partial dependence plots confirm that the model behaves logically and structured way, rather than fitting random noise in the data. The residual analysis also show no clear bias or error patterns.

Together, these findings suggest that the final model is both accurate and easy to interpret, and that it provides a conjecture of how the legacy reimbursement system works and its behavior.