Welcome to Day 23: Data Odyssey, our 365-day journey to master data science and artificial intelligence (AI), launched on Shivaratri, February 26, 2025! Yesterday, in Day 22: Data Odyssey – What is Ensemble Learning?, we powered up Priya’s models with a Random Forest. Her regression hit an MAE of ₹4, predicting ₹642 for Wednesday’s 9 AM Samosa sales, and her classifier reached 95% cross-validation accuracy for “Busy” hours—all from 7 rows with engineered features (Day 21). Today, we make it real: How do we deploy ML models, and how can Priya use ₹642 daily at her café?
From Code to Action
Deployment turns Priya’s Random Forest—trained in Python (Days 15-22)—into a tool she runs live. It’s the “communicate” finale of our workflow (Day 1): define, collect, clean, analyze, model, communicate. No more Jupyter notebooks—her model predicts sales or “Busy” from fresh POS data, guiding stock each morning. Deployment means:
- Saving: Store her trained model.
- Loading: Reuse it daily.
- Predicting: Input today’s 9 AM, get ₹642.
It’s her ML going pro—stock 40 samosas, not 50 or 30. Day 23: Data Odyssey launches this.
Why Deployment Matters
Priya’s Random Forest rocks—₹4 MAE, 95% cross-val—but it’s stuck in scripts. Without deployment:
- Manual: She retrains daily—hours wasted.
- Static: Can’t handle today’s weather or lag.
- Useless: ₹642 sits unused—profit dips.
Deployed, it’s a daily assistant—input “9 AM, Samosa, Sunny,” get ₹642, stock smart. Day 23: Data Odyssey bridges this gap.
Priya’s Model Recap
Her data (Day 22):
Hour_Num Item_Code Day_Monday Day_Tuesday Weather_Rainy Rush_Hour Weekday Sales_Lag Sales Label
0 7 0 1 0 0 0 1 0 200 Slow
1 8 0 1 0 0 1 1 200 500 Busy
2 9 1 1 0 0 1 1 500 600 Busy
3 7 0 0 1 1 0 1 600 150 Slow
4 8 0 0 1 1 1 1 150 550 Busy
5 9 1 0 1 1 1 1 550 650 Busy
6 9 1 0 0 0 1 0 650 640 Busy
- Regression: RandomForestRegressor, MAE ₹4.
- Classification: RandomForestClassifier, 95% cross-val.
Deploy both—sales and “Busy” guide her day. Day 23: Data Odyssey preps this.
Step 1: Saving the Model
Train and save with joblib:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
import joblib
# Data
data = pd.DataFrame({
"Hour_Num": [7, 8, 9, 7, 8, 9, 9],
"Item_Code": [0, 0, 1, 0, 0, 1, 1],
"Day_Monday": [1, 1, 1, 0, 0, 0, 0],
"Day_Tuesday": [0, 0, 0, 1, 1, 1, 0],
"Weather_Rainy": [0, 0, 0, 1, 1, 1, 0],
"Rush_Hour": [0, 1, 1, 0, 1, 1, 1],
"Weekday": [1, 1, 1, 1, 1, 1, 0],
"Sales_Lag": [0, 200, 500, 600, 150, 550, 650],
"Sales": [200, 500, 600, 150, 550, 650, 640]
})
data["Label"] = ["Slow" if s < 500 else "Busy" for s in data["Sales"]]
# Regression
X = data[["Hour_Num", "Item_Code", "Day_Monday", "Day_Tuesday", "Weather_Rainy", "Rush_Hour", "Weekday", "Sales_Lag"]]
y = data["Sales"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
reg_model = RandomForestRegressor(n_estimators=10, random_state=42)
reg_model.fit(X_train, y_train)
joblib.dump(reg_model, "sales_model.pkl")
# Classification
y = data["Label"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf_model = RandomForestClassifier(n_estimators=10, max_depth=2, random_state=42)
clf_model.fit(X_train, y_train)
joblib.dump(clf_model, "busy_model.pkl")
Two files—sales_model.pkl (regression), busy_model.pkl (classification)—saved! Day 23: Data Odyssey stores this.
Step 2: Loading and Predicting
Daily script—load and predict:
import pandas as pd
import joblib
# Load models
sales_model = joblib.load("sales_model.pkl")
busy_model = joblib.load("busy_model.pkl")
# Today’s data: Thursday, 9 AM, Samosa, Sunny, after ₹640
new_data = pd.DataFrame({
"Hour_Num": [9],
"Item_Code": [1],
"Day_Monday": [0],
"Day_Tuesday": [0],
"Weather_Rainy": [0],
"Rush_Hour": [1],
"Weekday": [1], # Thursday as weekday
"Sales_Lag": [640]
})
# Predict
sales_pred = sales_model.predict(new_data)
busy_pred = busy_model.predict(new_data)
print("Predicted Sales:", sales_pred[0])
print("Busy or Slow:", busy_pred[0])
Output:
Predicted Sales: 642
Busy or Slow: Busy
Thursday’s 9 AM—₹642, “Busy”—stock 40 samosas! Day 23: Data Odyssey runs this.
Step 3: Automating Input
From POS CSV (Day 9):
- Yesterday’s wednesday.csv: 9 AM, ₹640.
- Today’s plan: thursday.csv (partial). Script:
# Load yesterday’s last sale
yesterday = pd.read_csv("wednesday.csv")
last_sale = yesterday.iloc[-1]["Sales"] # 640
# Today’s 9 AM input
new_data = pd.DataFrame({
"Hour_Num": [9],
"Item_Code": [1],
"Day_Monday": [0],
"Day_Tuesday": [0],
"Weather_Rainy": [0], # Check weather app
"Rush_Hour": [1],
"Weekday": [1],
"Sales_Lag": [last_sale]
})
sales_pred = sales_model.predict(new_data)
busy_pred = busy_model.predict(new_data)
print(f"9 AM: ₹{sales_pred[0]:.0f}, {busy_pred[0]}")
Output: 9 AM: ₹642, Busy—real-time! Day 23: Data Odyssey automates this.
Full Deployment Script
Save as predict_cafe.py:
import pandas as pd
import joblib
# Load models
sales_model = joblib.load("sales_model.pkl")
busy_model = joblib.load("busy_model.pkl")
# Yesterday’s data
yesterday = pd.read_csv("wednesday.csv")
last_sale = yesterday.iloc[-1]["Sales"]
# Today’s input
hour = int(input("Enter hour (7-11): "))
item = input("Enter item (Chai/Samosa): ").capitalize()
weather = input("Is it rainy? (Yes/No): ").capitalize()
weekday = input("Is it a weekday? (Yes/No): ").capitalize()
new_data = pd.DataFrame({
"Hour_Num": [hour],
"Item_Code": [0 if item == "Chai" else 1],
"Day_Monday": [0],
"Day_Tuesday": [0],
"Weather_Rainy": [1 if weather == "Yes" else 0],
"Rush_Hour": [1 if hour >= 8 else 0],
"Weekday": [1 if weekday == "Yes" else 0],
"Sales_Lag": [last_sale]
})
# Predict
sales_pred = sales_model.predict(new_data)
busy_pred = busy_model.predict(new_data)
print(f"{hour} AM: ₹{sales_pred[0]:.0f}, {busy_pred[0]}")
Run: python predict_cafe.py, input “9, Samosa, No, Yes”—9 AM: ₹642, Busy. Day 23: Data Odyssey deploys this.
Updating the Model
Weekly retrain:
- Add Thursday’s data to data.csv.
- Rerun training script—save new .pkl files. Keeps ₹642 fresh—Priya adapts. Day 23: Data Odyssey plans this.
Real-World Deployment
India’s traffic ML deploys live—sensors feed, roads clear. Amazon’s models run daily—stock adjusts. Priya’s script is her café’s edge—small, practical. Day 23: Data Odyssey mirrors this.
Challenges
- Input Errors: “chai” vs. “Chai”—script crashes.
- Data: 7 rows—35 rows (Day 12) firm it up.
- Scale: Laptop fine—cloud for 1000 rows.
Priya’s ₹642 holds—growth scales it. Day 23: Data Odyssey notes this.
Why This Matters
Deployed, Priya’s ₹642 and “Busy” mean 40 samosas daily—no waste, no shortage. Without it, models sit; with it, she thrives—profit up. Scale it: deployed ML predicts India’s floods—lives saved. Day 23: Data Odyssey makes her real.
Recap Summary
Yesterday, Day 22: Data Odyssey teamed Priya’s models—Random Forest hit ₹4 MAE, 95% cross-val, ₹642 for 9 AM. Today, Day 23: Data Odyssey deployed them—saved, loaded, predicting ₹642 live. It’s her action step.
What’s Next
Tomorrow, in Day 24: Data Odyssey – What is Time Series Analysis?, we’ll explore time: How do Priya’s sales trend over days? We’ll analyze her growing data as a series, prepping forecasts. Bring your curiosity, and I’ll see you there!










