Welcome to Day 41 of our 365-day journey to master data science and artificial intelligence, launched on February 26, 2025. Yesterday, in Day 40, we deployed Priya’s machine learning models using a Flask API on her 11-row dataset, cleaned of anomalies. The stacked ensemble delivered real-time predictions of 641 rupees for Thursday’s 9 AM sales with a mean absolute error of 3.4, guiding 32 samosas for a Busy hour, while the classifier maintained 1.0 recall for Slow hours. Today, we clarify: What is explainable AI, and why does Priya’s model predict 641 rupees for 9 AM?
Understanding Predictions
Explainable AI involves techniques to make machine learning models transparent, revealing why they produce specific outputs. Priya’s deployed models predict sales and classify hours, but why 641 rupees for 9 AM? Is it Sales_Lag, customer counts, or sentiment? Explainable AI is part of the analyze phase in our workflow, ensuring trust in predictions. Unlike black-box models like Random Forest, explainable AI shows feature impacts, helping Priya stock 32 samosas confidently.
Consider Priya reviewing her café’s sales. Her model suggests 32 samosas for 9 AM, but she needs to know why—more customers or positive reviews? Explainable AI answers, refining her 15-chai plan for 7 AM. This is the focus of Day 41.
Why Explainable AI Matters
Priya’s models—regression with 3.4 mean absolute error and classifier with 1.0 Slow recall—are effective, but:
- Trust: Staff question 641 rupees—why not 600?
- Action: Low 150-rupee sales at 7 AM—fix staffing or chai quality?
- Scale: With 35 rows, complex models need clarity.
Explainable AI builds confidence in her 632.5-rupee forecast, anomaly detection, and reinforcement learning stock, ensuring decisions align with data. Day 41 clarifies this.
Priya’s Data Recap
Her cleaned data from Day 40:
Datetime,Sales,Hour_Num,Item_Code,Weather_Rainy,Rush_Hour,Weekday,Sales_Lag,Label,Sentiment,Customer_Count,RL_Stock
2025-03-03 08:00,500,8,0,0,1,1,200,Busy,0,15,39
2025-03-03 09:00,600,9,1,0,1,1,500,Busy,0.6588,20,32
2025-03-03 10:00,500,10,1,0,0,1,600,Busy,0.4404,12,39
2025-03-03 11:00,400,11,1,0,0,1,500,Slow,0,8,39
2025-03-04 08:00,550,8,0,1,1,1,150,Busy,0.5719,16,39
2025-03-04 09:00,650,9,1,1,1,1,550,Busy,0.5859,22,33
2025-03-04 10:00,550,10,1,1,0,1,650,Busy,0,13,39
2025-03-04 11:00,450,11,1,1,0,1,550,Slow,0,9,39
2025-03-05 09:00,640,9,1,0,1,0,650,Busy,0.6369,21,32
2025-03-05 10:00,540,10,1,0,0,0,640,Busy,0,14,39
2025-03-05 11:00,440,11,1,0,0,0,540,Slow,0,10,39
- Models: Stacked ensemble, mean absolute error 3.4, 641 rupees for 9 AM.
- Issue: Opaque predictions—why 641 rupees?
Goal: Use explainable AI to reveal feature impacts—trust 641 rupees, stock 32 samosas. Day 41 begins here.
Explainable AI Basics
Techniques for Priya’s models:
- Feature Importance:
- Rank features like Sales_Lag or Customer_Count by impact.
- Works for Random Forest, Gradient Boosting.
- SHAP Values:
- Assign contributions to each feature for a prediction.
- Detailed, model-agnostic.
- Partial Dependence:
- Show how a feature affects output, holding others constant.
With 11 rows, SHAP suits Priya’s stacked ensemble—scalable to 35 rows. Day 41 applies this.
SHAP for Regression
Analyze the 641-rupee prediction:
import pandas as pd
import shap
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, StackingRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
data_clean = pd.DataFrame({
"Datetime": ["2025-03-03 08:00", "2025-03-03 09:00", "2025-03-03 10:00", "2025-03-03 11:00",
"2025-03-04 08:00", "2025-03-04 09:00", "2025-03-04 10:00", "2025-03-04 11:00",
"2025-03-05 09:00", "2025-03-05 10:00", "2025-03-05 11:00"],
"Sales": [500, 600, 500, 400, 550, 650, 550, 450, 640, 540, 440],
"Hour_Num": [8, 9, 10, 11, 8, 9, 10, 11, 9, 10, 11],
"Item_Code": [0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
"Weather_Rainy": [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
"Rush_Hour": [1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0],
"Weekday": [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
"Sales_Lag": [200, 500, 600, 500, 150, 550, 650, 550, 650, 640, 540],
"Sentiment": [0, 0.6588, 0.4404, 0, 0.5719, 0.5859, 0, 0, 0.6369, 0, 0],
"Customer_Count": [15, 20, 12, 8, 16, 22, 13, 9, 21, 14, 10],
"RL_Stock": [39, 32, 39, 39, 39, 33, 39, 39, 32, 39, 39]
})
data_clean["Datetime"] = pd.to_datetime(data_clean["Datetime"])
X = data_clean[["Hour_Num", "Item_Code", "Weather_Rainy", "Rush_Hour", "Weekday", "Sales_Lag", "Sentiment", "Customer_Count", "RL_Stock"]]
y = data_clean["Sales"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
estimators = [
("rf", RandomForestRegressor(n_estimators=20, max_depth=3, random_state=42)),
("gb", GradientBoostingRegressor(n_estimators=20, max_depth=2, random_state=42))
]
stack_reg = StackingRegressor(estimators=estimators, final_estimator=LinearRegression())
stack_reg.fit(X_train, y_train)
explainer = shap.KernelExplainer(stack_reg.predict, X_train)
shap_values = explainer.shap_values(X_test)
shap.summary_plot(shap_values, X_test, plot_type="bar")
Output (hypothetical, based on SHAP):
- Top features: Sales_Lag (0.4 impact), Customer_Count (0.3), Sentiment (0.1).
- For 641 rupees: Sales_Lag=640 pushes +50 rupees, Customer_Count=20 adds +30 rupees, Sentiment=0.6 boosts +10 rupees.
Sales_Lag and customers drive 641 rupees—trustworthy for 32 samosas. Day 41 explains this.
SHAP for Thursday 9 AM
Analyze one prediction:
new_data = pd.DataFrame({
"Hour_Num": [9],
"Item_Code": [1],
"Weather_Rainy": [0],
"Rush_Hour": [1],
"Weekday": [1],
"Sales_Lag": [640],
"Sentiment": [0.6],
"Customer_Count": [20],
"RL_Stock": [32]
}, columns=X.columns)
shap_values = explainer.shap_values(new_data)
print("SHAP for 641 rupees:", shap_values[0])
Output (hypothetical):
SHAP for 641 rupees: [2.0, 5.0, 0.0, 3.0, -1.0, 50.0, 10.0, 30.0, -2.0]
- Sales_Lag: +50 rupees (prior 640 rupees).
- Customer_Count: +30 rupees (20 customers).
- Sentiment: +10 rupees (positive reviews).
Confirms 641 rupees—stock 32 samosas, expect crowds. Day 41 clarifies this.
Classifier Explainability
For Busy/Slow:
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
y = data_clean["Label"]
estimators = [
("rf", RandomForestClassifier(n_estimators=10, max_depth=2, class_weight="balanced", random_state=42)),
("gb", GradientBoostingClassifier(n_estimators=10, max_depth=2, random_state=42))
]
stack_clf = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
stack_clf.fit(X, y)
explainer_clf = shap.KernelExplainer(stack_clf.predict_proba, X)
shap_values_clf = explainer_clf.shap_values(new_data)
print("SHAP for Busy:", shap_values_clf[1][0])
Output (hypothetical):
- Customer_Count: +0.4 probability (20 customers).
- Sales_Lag: +0.3 probability (640 rupees).
- Hour_Num: +0.1 probability (9 AM peak).
Busy label driven by crowds, prior sales—trust 32 samosas. Day 41 interprets this.
Feature Importance
Global view:
importances = stack_reg.estimators_[0][1].feature_importances_ # Random Forest
print("Feature Importance:", dict(zip(X.columns, importances)))
Output (hypothetical):
Feature Importance: {'Hour_Num': 0.1, 'Item_Code': 0.05, 'Weather_Rainy': 0.05, 'Rush_Hour': 0.1, 'Weekday': 0.05, 'Sales_Lag': 0.4, 'Sentiment': 0.1, 'Customer_Count': 0.1, 'RL_Stock': 0.05}
Sales_Lag dominates—641 rupees reflects prior sales. Day 41 ranks this.
Insights for Priya
- 641 rupees: High Sales_Lag (640 rupees) and Customer_Count (20) explain the peak—9 AM rush, stock 32 samosas.
- 7 AM Lows: 150 rupees flagged as anomaly—Sentiment (-0.4767) and low Customer_Count (4) suggest service issues, not demand.
- Trust: Staff see Sales_Lag’s role—accept 641 rupees, adjust chai prep.
Day 41 builds confidence.
Why Explainable AI?
- Clarity: Sales_Lag, Customer_Count drive 641 rupees—stock 32 samosas.
- Action: Low 7 AM sales—fix chai, not hours.
- Scale: 35 rows—explain complex models.
Enhances 632.5-rupee forecast, deployment—trusted predictions. Day 41 trusts this.
Real-World Explainable AI
Banks explain loan denials—build trust. Retail clarifies stock predictions—staff align. Priya’s explainable AI is her café’s guide—small, clear. Day 41 mirrors this.
Challenges
- Small Data: 11 rows—SHAP noisy.
- Complexity: Stacked ensemble—harder to explain.
- Trade-Off: Simpler models lose accuracy.
More data—Priya scales. Day 41 notes this.
Why This Matters
Explaining 641 rupees—32 samosas, crowds—builds Priya’s trust. Without it, predictions confuse; with it, she’s confident—profit up. Scaled, explainable AI clarifies medical diagnoses—lives saved. Day 41 trusts her.
Recap Summary
Yesterday, Day 40 deployed models—mean absolute error 3.4, 641 rupees. Today, Day 41 used explainable AI—Sales_Lag, Customer_Count drive 641 rupees, 32 samosas. It’s her trust step.
What’s Next
Tomorrow, in Day 42, we’ll predict trends: Can Priya forecast weekly sales? Spot patterns? We’ll explore time series forecasting, planning her café. Join us with curiosity!

























