$npx -y skills add MLOps-Courses/mlops-coding-skills --skill mlops-prototypingGuide to create structured, reproducible Jupyter notebooks for MLOps prototyping, emphasizing configuration management and pipeline integrity.
| 1 | # MLOps Prototyping |
| 2 | |
| 3 | ## Goal |
| 4 | |
| 5 | To create standardized, reproducible, and production-ready prototypes in Jupyter notebooks. This skill enforces a structured layout (Imports -> Configs -> Load -> EDA -> Modeling -> Eval) and robust engineering practices (Pipelines, Split-Verification) to prevent technical debt and data leakage. |
| 6 | |
| 7 | ## Prerequisites |
| 8 | |
| 9 | - **Language**: Python |
| 10 | - **Environment**: `uv` managed project (`.venv`) |
| 11 | - **Context**: Executed within a `.ipynb` file or converting to one. |
| 12 | |
| 13 | ## Instructions |
| 14 | |
| 15 | ### 1. Notebook Structure |
| 16 | |
| 17 | Enforce the following linear sections in every notebook to ensure readability and maintainability. |
| 18 | |
| 19 | 1. **Title & Purpose**: H1 Title and a brief description of the experiment goals. |
| 20 | 2. **Imports**: Group standard libraries, third-party, and usage-specific imports. |
| 21 | 3. **Configs**: Define **Global Constants** (paths, random seeds, hyperparameters) here. No magic numbers deeper in the code. |
| 22 | 4. **Datasets**: Load, validate, and split data. |
| 23 | 5. **Analysis (EDA)**: Inspect target distributions and correlations. |
| 24 | 6. **Modeling**: Define and train `sklearn.pipeline.Pipeline` objects. |
| 25 | 7. **Evaluations**: Compute metrics and visualize performance on held-out data. |
| 26 | |
| 27 | ### 2. Configuration Standards |
| 28 | |
| 29 | Expose all "knobs" at the top of the notebook for easy experimentation. |
| 30 | |
| 31 | - **Randomness**: Define `RANDOM_STATE = 42` and use it in splits and model initialization. |
| 32 | - **Paths**: Use `pathlib` for robust path handling. |
| 33 | |
| 34 | ```python |
| 35 | from pathlib import Path |
| 36 | ROOT = Path("..") |
| 37 | DATA_PATH = ROOT / "data" / "input.parquet" |
| 38 | ``` |
| 39 | |
| 40 | - **Hyperparameters**: Group model params (e.g., `N_ESTIMATORS`, `MAX_DEPTH`). |
| 41 | - **Toggles**: Use booleans for expensive operations (e.g., `USE_GPU = True`, `RUN_GRID_SEARCH = False`). |
| 42 | |
| 43 | ### 3. Data Management |
| 44 | |
| 45 | Ensure data integrity and prevent leakage. |
| 46 | |
| 47 | - **Loading**: Prefer `pd.read_parquet` for speed/types, or `pd.read_csv`. |
| 48 | - **Splitting**: |
| 49 | - **Always** split into `X_train`, `X_test`, `y_train`, `y_test` **before** any data-dependent transformations (imputation, scaling). |
| 50 | - **Random Split**: Use `sklearn.model_selection.train_test_split` with `stratify` for balanced classification. |
| 51 | - **Time Series**: Use `sklearn.model_selection.TimeSeriesSplit` if data has a temporal dimension (do NOT shuffle). |
| 52 | - Use `random_state=RANDOM_STATE`. |
| 53 | |
| 54 | ### 4. Pipeline Construction |
| 55 | |
| 56 | Prohibit raw data transformations on the full dataset. |
| 57 | |
| 58 | - **Mandate**: Use `sklearn.pipeline.Pipeline` or `ColumnTransformer`. |
| 59 | - **Why**: Automation of `fit` on train and `transform` on test prevents data leakage. |
| 60 | - **Example**: |
| 61 | |
| 62 | ```python |
| 63 | from sklearn.pipeline import Pipeline |
| 64 | from sklearn.preprocessing import StandardScaler, OneHotEncoder |
| 65 | from sklearn.impute import SimpleImputer |
| 66 | from sklearn.compose import ColumnTransformer |
| 67 | |
| 68 | CACHE = "./.cache" # Define a cache directory |
| 69 | |
| 70 | numeric_transformer = Pipeline(steps=[ |
| 71 | ('imputer', SimpleImputer(strategy='median')), |
| 72 | ('scaler', StandardScaler()) |
| 73 | ]) |
| 74 | |
| 75 | preprocessor = ColumnTransformer(transformers=[ |
| 76 | ('num', numeric_transformer, numeric_features) |
| 77 | ]) |
| 78 | |
| 79 | # Use 'memory' to cache transformer outputs, speeding up GridSearch |
| 80 | model = Pipeline(steps=[ |
| 81 | ('preprocessor', preprocessor), |
| 82 | ('classifier', RandomForestClassifier()) |
| 83 | ], memory=CACHE) |
| 84 | ``` |
| 85 | |
| 86 | ### 5. Evaluation & Visualization |
| 87 | |
| 88 | Go beyond accuracy/MSE. |
| 89 | |
| 90 | - **Metrics**: Use `sklearn.metrics` appropriate for the task (F1, ROC-AUC, RMSE, MAE). |
| 91 | - **Baselines**: Compare against a "Dummy" model (mean/mode) to verify learning. |
| 92 | - **Visualization**: |
| 93 | - **Regression**: Residual plots, Actual vs Predicted. |
| 94 | - **Classification**: Confusion Matrix, ROC Curve, Precision-Recall. |
| 95 | - **Feature Importance**: Visualize `feature_importances_` or SHAP values. |
| 96 | |
| 97 | ### 6. Transition to Production |
| 98 | |
| 99 | Facilitate the move from notebook to python package (`src/`). |
| 100 | |
| 101 | - **Function Refactoring**: Once a block of code is stable (e.g., a complex data cleaning step), refactor it into a function *within* the notebook. This makes moving it to a `.py` file trivial later. |
| 102 | - **Cell Tagging**: Use tags like `parameters` (for Papermill) or `export` to mark cells that should be part of the final documentation or automated pipeline. |
| 103 | - **Clean State**: Ensure the notebook runs top-to-bottom (`Restart Kernel and Run All`) without errors before committing. |
| 104 | |
| 105 | ## Self-Correction Checklist |
| 106 | |
| 107 | - [ ] **No Magic Numbers**: Are all parameters in the `Configs` section? |
| 108 | - [ ] **No Data Leakage**: Is `fit` called ONLY on `X_train`? |
| 109 | - [ ] **Reproducibility**: Is `random_state` set for all stochastic operations? |
| 110 | - [ ] **Resilience**: are paths defined relative to the project root? |
| 111 | - [ ] **Clarity**: Does the notebook read like a report (Markdown cells explaining the *Why*)? |