Skip to content

About

Practical MLflow learning repository for Machine Learning Engineering, covering experiment tracking, model logging, model registry, model versioning, hyperparameter tuning, model evaluation, and end-to-end ML lifecycle management for reproducible and production-ready ML workflows.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

MLflow for Machine Learning

Python 3.11 MLflow Jupyter License: MIT

Hands-on notebooks for learning how to track machine-learning experiments with MLflow. The examples create a reproducible imbalanced binary-classification dataset, compare models, log evaluation metrics and model artifacts, and introduce remote tracking with DagsHub.

Contents

Project overview

This repository is a set of educational Jupyter notebooks, not a web application or a deployable prediction service. It focuses on the practical experiment workflow: prepare data, train classifiers, calculate metrics, and record results so that runs can be inspected and compared.

The classification examples use scikit-learn's make_classification to generate 1,000 rows with 10 features and a 90%/10% class split. The data is divided into training and test sets with stratification. One notebook applies SMOTETomek to the training data to demonstrate resampling before fitting; evaluation remains on the held-out test split.

Repository structure

.
??? 01_first_experiment/
?   ??? 01_first_experiment.ipynb
?   ??? mlruns/                  # Generated MLflow tracking data
??? 02_binary_classification/
?   ??? 02_binary_classification.ipynb
?   ??? mlruns/                  # Generated MLflow tracking data
??? 03_dagshub_tracking/
?   ??? 03_dagshub_tracking.ipynb
??? requirements.txt
??? .gitignore
??? LICENSE
??? README.md
Notebook Purpose
01_first_experiment/01_first_experiment.ipynb Fits Logistic Regression to generated imbalanced data and logs its parameters, accuracy, class recalls, macro F1 score, and model artifact to MLflow.
02_binary_classification/02_binary_classification.ipynb Compares Logistic Regression, Random Forest, XGBoost, and XGBoost trained after SMOTETomek resampling. It logs the runs and their models to MLflow.
03_dagshub_tracking/03_dagshub_tracking.ipynb Prepares the generated data, resamples the training set, and demonstrates initializing DagsHub's MLflow integration. It is a connection setup example; it currently does not train or log model runs.

MLflow run data and model artifacts are generated when notebooks execute. The repository also contains previously generated MLflow run files in the experiment folders; new runtime output is ignored by Git.

What you will learn

  • Create and split a reproducible, imbalanced binary-classification dataset.
  • Train Logistic Regression, Random Forest, and XGBoost classifiers.
  • Apply SMOTETomek to the training partition to explore class imbalance handling.
  • Inspect classification reports and compare accuracy, per-class recall, and macro F1.
  • Create MLflow experiments and log parameters, metrics, and model artifacts.
  • Configure a DagsHub-backed MLflow tracking connection using environment variables.

Requirements

  • Python 3.11 (the notebooks were last saved with a Python 3.11 kernel).
  • The packages listed in requirements.txt.
  • A browser for Jupyter and, for the local tracking examples, MLflow's UI/server.
  • A DagsHub account and repository only if you want to run the DagsHub setup notebook.

Setup

Clone the repository and enter its directory:

git clone https://github.com/Md-Emon-Hasan/MLFlow-for-Machine-Learning.git
cd MLFlow-for-Machine-Learning

Create and activate a virtual environment.

Windows PowerShell:

py -3.11 -m venv .venv
.\.venv\Scripts\Activate.ps1

macOS or Linux:

python3.11 -m venv .venv
source .venv/bin/activate

Install the project dependencies and start Jupyter:

python -m pip install --upgrade pip
python -m pip install -r requirements.txt
jupyter lab

Open the notebook you want to follow and select the virtual environment's Python 3 kernel. Run cells from top to bottom. The generated dataset is created in memory; no external dataset download is required.

Run the notebooks

Local MLflow examples

The first two notebooks set their tracking URI to http://127.0.0.1:5000. Start the MLflow UI in a separate terminal before running their MLflow logging cells:

mlflow ui --host 127.0.0.1 --port 5000

Then run the notebook cells in Jupyter. Open http://127.0.0.1:5000 to inspect the experiments. Keep the UI process running while logging. If port 5000 is already in use, choose another port and update the tracking URI in the notebook to match.

DagsHub notebook

The DagsHub notebook reads the repository owner and repository name from environment variables. Set DAGSHUB_OWNER and DAGSHUB_REPO to a DagsHub repository you can access before running its DagsHub setup cell; see DagsHub configuration. Do not put access tokens or other secrets in a notebook committed to Git.

View experiments in MLflow

The local examples record runs in MLflow. Each run can include:

  • Parameters such as model name and estimator configuration.
  • Accuracy, recall for class 0 and class 1, and macro-averaged F1.
  • A serialized model artifact logged through the scikit-learn or XGBoost MLflow flavor.

In the MLflow UI, select the experiment used by the notebook and compare runs. The binary-classification notebook currently names its experiment Anomaly Detection; the name is retained from the notebook example, although the generated task is binary classification.

DagsHub configuration

Create or choose a DagsHub repository, configure your credentials according to DagsHub's current instructions, and set these variables in the environment used to launch Jupyter.

Windows PowerShell:

$env:DAGSHUB_OWNER = "your-dagshub-username"
$env:DAGSHUB_REPO = "your-dagshub-repository"
jupyter lab

macOS or Linux:

export DAGSHUB_OWNER="your-dagshub-username"
export DAGSHUB_REPO="your-dagshub-repository"
jupyter lab

The notebook calls dagshub.init(..., mlflow=True) to configure the integration. The current notebook is a setup demonstration only; it does not yet train a model or log a run to the remote tracking server.

Reproducibility and limitations

  • Data generation and train/test splitting use fixed random seeds to make the example repeatable.
  • This is a learning project, not a production training pipeline. It does not provide a command-line training entry point, API, deployment workflow, or automated model validation.
  • The example dataset is synthetic. Its metrics are for demonstrating the workflow and should not be interpreted as real-world model performance.
  • Resampling is applied only to the training split; the held-out test data remains in its original class distribution.
  • Dependency versions are not pinned. For long-term reproducibility, create and maintain a lock file after selecting versions for your environment.
  • The notebooks use MLflow APIs that may emit deprecation notices as the ecosystem evolves. Check the installed package documentation if an API or tracking configuration changes.

Troubleshooting

Problem What to check
Notebook cannot connect to MLflow Confirm that the UI/server is running on port 5000 and that the notebook's tracking URI uses the same host and port.
ModuleNotFoundError Activate the project virtual environment, install requirements.txt, and select that environment as the Jupyter kernel.
XGBoost installation fails Use a supported Python version and follow the XGBoost installation instructions for your operating system.
DagsHub setup reports missing configuration Set DAGSHUB_OWNER and DAGSHUB_REPO in the same environment from which Jupyter is launched.
DagsHub authentication fails Check your DagsHub account access and follow the provider's current authentication guidance. Never commit credentials.
Port 5000 is occupied Start the MLflow UI on a free port and update the notebook tracking URI accordingly.

Contributing

Suggestions and pull requests are welcome. For a change:

  1. Fork the repository and create a focused branch.
  2. Make the notebook or documentation change, keeping examples reproducible and credentials out of source control.
  3. Describe the change and any setup requirements in your pull request.

License

This project is distributed under the MIT License. See LICENSE for details.

Connect with me

About

Practical MLflow learning repository for Machine Learning Engineering, covering experiment tracking, model logging, model registry, model versioning, hyperparameter tuning, model evaluation, and end-to-end ML lifecycle management for reproducible and production-ready ML workflows.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages