Turning the Notebook into a Python Script

Turning the Notebook into a Python Script

The Module 2 notebook is useful for exploration, but a production workflow needs a stable entry point. We refactor it into a Python script that can be called from a terminal, a scheduler, or an orchestrator.

Keep the useful code, remove the session state

Start by grouping the notebook cells by responsibility. Create separate functions for loading data, preparing features, training the model, and running the experiment. A function boundary makes the inputs and outputs visible. It also avoids depending on variables left behind by an earlier cell.

The original notebook used for experimentation

The script can still use MLflow to track parameters, metrics, and the trained model. Put the tracking calls inside the run function so every execution produces a distinct run.

Add a command-line interface

The training period should be an input, not a hard-coded constant. Use argparse to accept the year and month.

parser.add_argument('--year', type=int, required=True)
parser.add_argument('--month', type=int, required=True)
args = parser.parse_args()
run(year=args.year, month=args.month)

The training logic moved into a Python script

Now the same code can train on a different month without editing the source:

python duration-prediction.py --year 2023 --month 3

Year and month parameters exposed at the script boundary

Preserve metadata and artifacts

Record the data period, feature choices, model parameters, and evaluation metrics in MLflow. Save the model artifact with the run so a later deployment step can identify exactly which code execution produced it.

A parameterized script run with its MLflow output

This script is now a pipeline entry point. An orchestrator can call it with different periods, schedule it, retry it, or backfill historical periods without opening a notebook.

Optional analytics

Optional analytics are off unless you allow them. Your choice does not affect access to DataTalks.Club, Slack, events, or courses.

Current choice: not chosen.

Privacy Policy

If you allow analytics, self-hosted Umami counts anonymous public pageviews without analytics cookies. Private account and learning pages are excluded. Read the Privacy Policy for the complete data boundary.