This project is a basic example of a machine learning workflow using MLflow to track experiments. The script trains an ElasticNet regression model from scikit-learn to predict the quality of red wine based on its chemical properties.
The example.py script performs the following steps:
- Downloads the Wine Quality dataset from a public URL.
- Splits the data into training and testing sets.
- Trains an
ElasticNetmodel. - Evaluates the model using RMSE, MAE, and R2 metrics.
- Uses MLflow to log the following information for each run:
- Hyperparameters:
alphaandl1_ratio. - Metrics:
RMSE,MAE, andR2. - Artifacts: The trained
scikit-learnmodel itself.
- Hyperparameters:
Before you begin, ensure you have Python and Conda installed.
-
Create and Activate a Conda Environment:
# Create a new environment named 'mlops-venv' with Python 3.8 (or your preferred version) conda create --name mlops-venv python=3.8 -y # Activate the environment conda activate mlops-venv
-
Install Required Libraries:
pip install mlflow pandas scikit-learn numpy
You can run the script with default hyperparameters or provide custom values via the command line.
-
Run with Default Parameters (
alpha=0.5,l1_ratio=0.5):python example.py
-
Run with Custom Parameters: Provide
alphaandl1_ratioas command-line arguments.# Example: run with alpha=0.8 and l1_ratio=0.7 python example.py 0.8 0.7
After running the script, a new directory named mlruns will be created. This directory contains all the information about your experiment runs.
To visualize the results, use the MLflow UI.
-
Launch the MLflow UI: From the same directory that contains
mlruns, run the following command:mlflow ui
-
Open Your Browser: Navigate to http://127.0.0.1:5000 (or the address shown in your terminal).
Here, you can compare different runs, view parameters, metrics, and download the saved models from