Program Design Principles#
βCode is like a business: the better you structure it, the longer it survives.β
π― Learning Outcomes#
By the end of this module, youβll:
Write clean, modular, and maintainable Python code
Apply software design patterns to ML and business applications
Use Git + GitHub like a pro for collaboration and version control
Write documentation that makes your work readable and reproducible
Build end-to-end programs ready for deployment
Test and debug code efficiently with real-world business context
π§Ό Section 8.1 β Writing Clean and Modular Code#
π‘ Key Idea:#
βCode should be written for humans first, machines second.β
β The Business-ML Developerβs Commandments:#
Functionize Everything β If you write the same code twice β make it a function.
Name Like a CEO:
Bad:
x,df2,temp3Good:
customer_df,monthly_sales,calc_profit_margin()
One Function = One Purpose.
A function that does 10 things = a business meeting that never ends.
Use Comments for Why, not What.
# clean data= β# Remove inactive customers to reduce churn bias= β
Modularize your notebooks β Split code into
.pyutilities:# utils/data_cleaning.py def remove_nulls(df): return df.dropna()
Structure your project like this:
business_ml_project/ βββ data/ βββ notebooks/ βββ src/ β βββ preprocessing.py β βββ model_training.py β βββ utils/ βββ tests/ βββ requirements.txt βββ README.md
ποΈ Section 8.2 β Design Patterns for ML Applications#
π€ Why Design Patterns?#
Design patterns = βproven blueprintsβ for recurring coding problems.
πΌ Business-ML Patterns:#
Pattern |
Use Case |
Example |
|---|---|---|
Pipeline Pattern |
Sequential steps for ML workflow |
|
Factory Pattern |
Create different models dynamically |
Select model from config file |
Observer Pattern |
Model monitoring or callback alerts |
Logging drift in production |
Strategy Pattern |
Switch between algorithms |
Try multiple churn models |
Singleton Pattern |
One shared config object |
Shared DB or API connector |
βοΈ Example: ML Factory Pattern#
class ModelFactory:
def get_model(self, model_type):
if model_type == "xgboost":
from xgboost import XGBClassifier
return XGBClassifier()
elif model_type == "logistic":
from sklearn.linear_model import LogisticRegression
return LogisticRegression()
else:
raise ValueError("Unknown model type")
model = ModelFactory().get_model("logistic")
model.fit(X_train, y_train)
π Section 8.3 β Documentation Best Practices#
βοΈ The Holy Trinity of Documentation:#
Docstrings
Jupyter annotations
π§ Example of a Perfect Function Docstring#
def calculate_roi(investment, returns):
"""
Calculate Return on Investment (ROI)
Parameters:
investment (float): Initial investment amount
returns (float): Total returns received
Returns:
float: ROI as a percentage
Example:
>>> calculate_roi(1000, 1200)
20.0
"""
return ((returns - investment) / investment) * 100
π README Template#
# Customer Churn Prediction
A machine learning project that predicts churn probability using customer activity data.
## Steps
1. Data Cleaning
2. Feature Engineering
3. Model Training (RandomForest, XGBoost)
4. Evaluation and Reporting
## Run
```bash
python src/train_model.py
> π‘ Write documentation like future-you will forget everything in 3 months.
---
## π³ Section 8.4 β Version Control with Git and GitHub
### π Git = Your Business Time Machine
You can:
- Undo every mistake ever made
- Collaborate like a real dev team
- Track every experiment
### π§ Core Workflow:
```bash
# Create repo
git init
git add .
git commit -m "Initial commit - created clean project structure"
# Link with GitHub
git remote add origin https://github.com/yourname/business-ml.git
git push -u origin main
πͺ Pro Tips#
One feature = one branch
Commit often: βsave game checkpointsβ
Use meaningful commit messages:
β βfixed stuffβ
β βadded data preprocessing for missing valuesβ
π Bonus: GitHub Actions#
Automate testing, training, and deployment with .github/workflows/ci.yml.
βοΈ Section 8.5 β Building End-to-End Programs for Deployment#
π§± What βEnd-to-Endβ Means:#
β Raw Data β Cleaned β Model β Dashboard/API β Monitor β Repeat
π Business ML Pipeline Template#
def main():
data = load_data("data/sales_forecasting.csv")
data = preprocess(data)
model = train_model(data)
save_model(model, "models/sales_model.pkl")
evaluate(model, data)
deploy_to_streamlit("models/sales_model.pkl")
if __name__ == "__main__":
main()
π Deployment Examples#
Method |
Tool |
Use Case |
|---|---|---|
Web App |
Streamlit / Flask |
Internal dashboards |
API |
FastAPI |
Serve ML predictions |
Notebook-to-App |
Gradio |
Quick business demos |
Production |
Docker + Cloud |
Full enterprise deployment |
β‘ Remember: βA model isnβt useful until someone non-technical can use it.β
π§ͺ Section 8.6 β Testing and Debugging Business Applications#
π§ Why Test?#
Because debugging in production is like fixing a plane mid-flight βοΈ
π§° Levels of Testing:#
Type |
Checks |
Example Tool |
|---|---|---|
Unit |
Single functions |
|
Integration |
End-to-end pipelines |
|
Regression |
Model output drift |
|
User Acceptance |
Business KPIs |
Manual review |
β Example Test#
def test_calculate_roi():
result = calculate_roi(1000, 1200)
assert result == 20.0, "ROI calculation failed!"
πͺ Debugging Tricks#
Use
pdb: Pythonβs built-in debuggerUse
try/exceptsmartly:try: model.fit(X, y) except ValueError as e: print("π₯ Model training failed:", e)
Add logging instead of 100 print statements:
import logging logging.basicConfig(level=logging.INFO) logging.info("Model training started.")
π Summary#
Principle |
Business Impact |
|---|---|
Clean Code |
Easier maintenance, faster onboarding |
Design Patterns |
Scalable ML pipelines |
Documentation |
Saves hours for future teams |
Git |
Version safety + collaboration |
End-to-End Design |
Deployable business apps |
Testing |
Stable production systems |
π§© Capstone Challenge#
Design a mini end-to-end ML project applying all principles:
Clean project structure
Modular functions
GitHub repo + README
Simple test suite
Streamlit app or API deployment
πΌ Example: βPredict monthly revenue for a retail chain and deploy results as a Streamlit dashboard.β
# Your code here