A complete, production-ready machine learning pipeline has been implemented in the /model_pipeline directory based on your EDA notebook.
model_pipeline/
├── __init__.py # Package initialization
├── config.py # Configuration management
├── preprocessing.py # Data cleaning and preprocessing
├── feature_engineering.py # Feature creation and transformation
├── models.py # Model training and evaluation
├── predict.py # Inference and predictions
├── pipeline.py # Main orchestrator
├── utils.py # Helper functions
├── README.md # Detailed documentation
└── examples/ # Usage examples
├── train_model.py # Training example
├── make_predictions.py # Prediction examples
└── custom_pipeline.py # Advanced usage
- Loosely coupled components: Each module can be used independently
- Reusable classes: DataPreprocessor, FeatureEngineer, ModelTrainer, etc.
- Configurable: All parameters centralized in Config class
Implements all steps from your notebook:
- ✅ Missing value imputation (mode/median)
- ✅ Outlier detection and capping (IQR method)
- ✅ Feature engineering (11 new features)
- ✅ Model training (12 models)
- ✅ Model evaluation and comparison
- ✅ Model persistence (save/load)
- Fit/Transform pattern: Consistent with scikit-learn
- Error handling: Comprehensive validation
- Logging: Detailed execution logs
- Type hints: Better code clarity
- Documentation: Extensive docstrings
# Train a model
from model_pipeline import DeliveryTimePipeline
pipeline = DeliveryTimePipeline()
results = pipeline.run_full_pipeline()
# Make predictions
from model_pipeline import DeliveryTimePredictor
predictor = DeliveryTimePredictor(model_path='models/ridge_regression.pkl')
time = predictor.predict_single(Distance_km=10, Weather='Clear', ...)- Loads CSV data
- Handles missing values:
- Categorical: Mode imputation
- Numerical: Median imputation
- Detects and caps outliers using IQR method (1.5x threshold)
- Converts data types appropriately
Creates 11 engineered features:
Domain Features (4):
Estimated_Speed_kmh: Based on vehicle typeTime_per_km: Minutes per kilometerEstimated_Travel_Time: Distance × Time_per_kmTotal_Time_Estimate: Preparation + Travel
Binary Indicators (3):
Is_Rush_Hour: Morning/Evening flagIs_Bad_Weather: Rainy/Snowy/Foggy flagIs_High_Traffic: High traffic level flag
Categorical Bins (2):
Experience_Level: Novice/Intermediate/ExpertDistance_Category: Very_Short/Short/Medium/Long
Interactions (2):
Weather_Traffic: Weather × Traffic combinationVehicle_Traffic: Vehicle × Traffic combination
Trains 12 regression models:
- Linear models (4): Linear, Ridge, Lasso, ElasticNet
- Tree-based (5): Decision Tree, Random Forest, Gradient Boosting, XGBoost, LightGBM
- Others (3): AdaBoost, KNN, SVR
- Creates comparison table with all metrics
- Analyzes overfitting (train vs test gap)
- Performs cross-validation
- Extracts feature importance
- Selects best model automatically
- Saves best model with StandardScaler
- Includes feature names for consistency
- Enables easy loading for inference
python tests/test_pipeline.pyThis will:
- Run the complete pipeline
- Train all models
- Save the best model
- Make test predictions
- Verify everything works
cd model_pipeline/examples
python train_model.pycd model_pipeline/examples
python make_predictions.pyfrom model_pipeline import DeliveryTimePipeline
pipeline = DeliveryTimePipeline()
results = pipeline.run_full_pipeline(
data_path='data/Food_Delivery_Times.csv',
save_model=True
)from model_pipeline import DataPreprocessor, FeatureEngineer
preprocessor = DataPreprocessor()
df_clean = preprocessor.fit_transform(df)
engineer = FeatureEngineer()
df_engineered = engineer.fit_transform(df_clean)from model_pipeline import Config, DeliveryTimePipeline
config = Config()
config.test_size = 0.3
config.random_state = 123
pipeline = DeliveryTimePipeline(config=config)
results = pipeline.run_full_pipeline()from model_pipeline import DeliveryTimePredictor
predictor = DeliveryTimePredictor('models/ridge_regression.pkl')
# Single prediction
time = predictor.predict_single(
Distance_km=10.5,
Weather='Clear',
Traffic_Level='Medium',
Time_of_Day='Evening',
Vehicle_Type='Bike',
Preparation_Time_min=15,
Courier_Experience_yrs=3.5
)
# Batch predictions
import pandas as pd
orders = pd.read_csv('new_orders.csv')
predictions = predictor.batch_predict(orders)Purpose: Main orchestrator for the entire pipeline
Use when: You want to run the complete workflow
Key method: run_full_pipeline()
Purpose: Clean and preprocess raw data
Use when: You need to handle missing values and outliers
Key methods: fit(), transform(), fit_transform()
Purpose: Create engineered features
Use when: You want to add domain-specific features
Key methods: fit_transform()
Purpose: Train and manage models
Use when: You need to train multiple models
Key methods: train_all(), get_best_model(), save_model()
Purpose: Evaluate and compare models
Use when: You need performance metrics and comparisons
Key methods: create_comparison_table(), analyze_overfitting()
Purpose: Make predictions on new data
Use when: You need inference on new orders
Key methods: predict(), predict_single(), batch_predict()
All parameters are in config.py:
from model_pipeline import Config
config = Config()
# Customize preprocessing
config.outlier_threshold = 2.0 # More lenient outlier detection
# Customize feature engineering
config.vehicle_speeds = {'Bike': 18, 'Scooter': 28, 'Car': 35}
# Customize training
config.test_size = 0.3
config.cv_folds = 10
# Customize model hyperparameters
config.model_params['Random Forest']['n_estimators'] = 200Based on your notebook results:
| Metric | Value |
|---|---|
| Best Model | Ridge Regression |
| Test R² | 0.8199 |
| Test RMSE | 8.98 minutes |
| Test MAE | 6.04 minutes |
| Test MAPE | 10.77% |
The pipeline creates:
-
models/ - Saved model files
ridge_regression.pkl(or best model)
-
results/ - Model performance metrics
model_comparison.csvoverfitting_analysis.csv
-
logs/ - Execution logs
pipeline.log(if configured)
Run the test to verify everything works:
python tests/test_pipeline.pyExpected output:
✅ ALL TESTS PASSED - PIPELINE WORKING CORRECTLY
✅ Pipeline is ready for use
- Pipeline README:
model_pipeline/README.md - Main README:
README.md - Examples:
model_pipeline/examples/ - Docstrings: In each module
To understand the pipeline:
- Read
model_pipeline/README.md - Run
tests/test_pipeline.py - Try
examples/train_model.py - Try
examples/make_predictions.py - Explore
examples/custom_pipeline.py - Read docstrings in source code
Training workflow:
Load Data → Preprocess → Engineer Features → Encode → Split → Scale → Train → Evaluate → Select Best → Save
Prediction workflow:
Load Model → Load Data → Preprocess → Engineer Features → Encode → Scale → Predict
from sklearn.ensemble import ExtraTreesRegressor
trainer = ModelTrainer()
trainer.models['Extra Trees'] = ExtraTreesRegressor(n_estimators=100)
results = trainer.train_all(X_train, y_train, X_test, y_test)evaluator = ModelEvaluator()
importance = evaluator.get_feature_importance(
model,
feature_names,
top_n=20
)cv_results = evaluator.cross_validate_models(
models,
X,
y,
cv=10
)Import errors?
- Ensure you're in the project root directory
- Check that all dependencies are installed
Model not found?
- Train a model first using
train_model.py - Check the path in
Config.model_save_path
Prediction errors?
- Ensure all required features are present
- Check feature names match training data
Performance issues?
- Reduce number of models in
Config.model_params - Use smaller
cv_foldsfor cross-validation - Process data in batches using
batch_predict()
- ✅ Pipeline created
- ⏭️ Run
python tests/test_pipeline.py - ⏭️ Explore examples in
model_pipeline/examples/ - ⏭️ Customize configuration for your needs
- ⏭️ Integrate into your application
- ⏭️ Set up monitoring and logging
- ⏭️ Deploy to production
You now have a complete, production-ready ML pipeline that:
- ✅ Implements all steps from your notebook
- ✅ Is modular and reusable
- ✅ Follows best practices
- ✅ Is well-documented
- ✅ Is easy to customize
- ✅ Is ready for production use