Lesson 1 of 5
Move Training Code into a Script and Track an Experiment
Extract your notebook training code into a reusable script and log a training run with MLflow, a local experiment tracker.
7 minMLOps
After this lesson, you will be able to turn a notebook training session into a reusable Python script and record its results with MLflow, a local experiment tracker.
Start from a Notebook
You have trained a simple classification model in a Jupyter notebook. The notebook has cells that load data, split it, train a model, and print the accuracy. You want to run this training again later, with different data or settings, without opening the notebook and running cells by hand.
A script is a plain Python file that runs from top to bottom. Moving your training code into a script makes it repeatable: you can run it with one command, pass arguments, and see the same steps every time.
Create a new folder for this course. Inside it, create a file called train.py with the code from your notebook, cleaned up into functions. Here is a complete example:
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
def load_data():
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target
return X, y
def train_model(X, y, n_estimators):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=n_estimators, random_state=42)
model.fit(X_train, y_train)
accuracy = accuracy_score(y_test, model.predict(X_test))
return model, accuracy
if __name__ == "__main__":
X, y = load_data()
model, accuracy = train_model(X, y, n_estimators=100)
print(f"Accuracy: {accuracy:.4f}")The if __name__ == "__main__" block runs only when you execute the script directly, not when you import it. This lets you reuse the functions later.
Run the script from your terminal:
python train.pyYou will see the accuracy printed. The exact number may vary slightly because of the random split, but it should be high, around 0.9 or more.
Track an Experiment with MLflow
When you change a setting, such as the number of trees in the random forest, you want to remember which setting gave which accuracy. MLflow is a free tool that records each run of your script: the parameters you used, the metrics you got, and the model itself.
Install MLflow. This lesson is written for MLflow version 2.x:
pip install "mlflow>=2,<3"MLflow has a tracking server that stores runs in a local folder. By default, it uses a folder called mlruns in your current directory. You can start the server to see a web interface, but for now you only need to log runs from your script.
Add MLflow logging to train.py. You will record the parameter n_estimators and the metric accuracy. Here is the updated script:
import pandas as pd
import mlflow
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
def load_data():
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target
return X, y
def train_model(X, y, n_estimators):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=n_estimators, random_state=42)
model.fit(X_train, y_train)
accuracy = accuracy_score(y_test, model.predict(X_test))
return model, accuracy
if __name__ == "__main__":
mlflow.set_experiment("iris_classification")
with mlflow.start_run():
X, y = load_data()
n_estimators = 100
mlflow.log_param("n_estimators", n_estimators)
model, accuracy = train_model(X, y, n_estimators)
mlflow.log_metric("accuracy", accuracy)
print(f"Accuracy: {accuracy:.4f}")The mlflow.set_experiment line creates an experiment named iris_classification if it does not exist. mlflow.start_run() begins a new run. Inside the run, log_param records a single value, and log_metric records a number that measures performance.
Run the script again:
python train.pyNow MLflow has saved a run in the mlruns folder. To see it, start the MLflow UI:
mlflow uiOpen http://localhost:5000 in your browser. You will see the experiment iris_classification with one run. Click the run to see the parameter n_estimators and the metric accuracy.
Make the Script Reusable with Arguments
Right now, the number of trees is hard-coded. To try different values without editing the file, accept it as a command-line argument. Python's argparse module does this.
Update train.py to read n_estimators from the command line. Here is the final version for this lesson:
import argparse
import pandas as pd
import mlflow
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
def load_data():
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = iris.target
return X, y
def train_model(X, y, n_estimators):
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=n_estimators, random_state=42)
model.fit(X_train, y_train)
accuracy = accuracy_score(y_test, model.predict(X_test))
return model, accuracy
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--n_estimators", type=int, default=100)
args = parser.parse_args()
mlflow.set_experiment("iris_classification")
with mlflow.start_run():
X, y = load_data()
mlflow.log_param("n_estimators", args.n_estimators)
model, accuracy = train_model(X, y, args.n_estimators)
mlflow.log_metric("accuracy", accuracy)
print(f"Accuracy: {accuracy:.4f}")Run it with a different value:
python train.py --n_estimators 50Check the MLflow UI again. You will see a second run with n_estimators=50 and its own accuracy. Compare the two runs to see which setting worked better.
What You Have Built
You now have a single script that loads data, trains a model, and logs the run to MLflow. You can change the number of trees from the command line and keep a record of every attempt.
Key takeaways
- A training script makes your notebook code repeatable and easier to run with different settings.
- MLflow logs parameters and metrics for each run, so you can compare experiments.
- Use
argparseto pass settings like the number of trees without editing the script. - The MLflow UI at
http://localhost:5000shows all runs in an experiment.
Try it
Change the script to also log the test_size parameter. Add a command-line argument --test_size with a default of 0.2, pass it to train_test_split, and log it with MLflow. Run the script twice with different test sizes, such as 0.2 and 0.3. In the MLflow UI, verify that each run shows the correct test_size and its accuracy. A good result is two runs with different test_size values and no errors when running the script.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.