Chapter 05
Scenario 2: A cross-functional team with one data scientist working on an ML model
NotebookPython 3.9.12 ('exp-tracking-env')11 cells
Scenario 2: A cross-functional team with one data scientist working on an ML model
MLflow setup:
- tracking server: yes, local server
- backend store: sqlite database
- artifacts store: local filesystem
The experiments can be explored locally by accessing the local tracking server.
To run this example you need to launch the mlflow server locally by running the following command in your terminal:
mlflow server --backend-store-uri sqlite:///backend.db
In [ ]python · cell 2
python
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:5000")In [ ]python · cell 3
python
print(f"tracking URI: '{mlflow.get_tracking_uri()}'")In [ ]python · cell 4
python
mlflow.search_experiments()In [ ]python · cell 5
python
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
mlflow.set_experiment("my-experiment-1")
with mlflow.start_run():
X, y = load_iris(return_X_y=True)
params = {"C": 0.1, "random_state": 42}
mlflow.log_params(params)
lr = LogisticRegression(**params).fit(X, y)
y_pred = lr.predict(X)
mlflow.log_metric("accuracy", accuracy_score(y, y_pred))
mlflow.sklearn.log_model(lr, artifact_path="models")
print(f"default artifacts URI: '{mlflow.get_artifact_uri()}'")In [ ]python · cell 6
python
mlflow.search_experiments()Interacting with the model registry
In [ ]python · cell 8
python
from mlflow.tracking import MlflowClient
client = MlflowClient("http://127.0.0.1:5000")In [ ]python · cell 9
python
client.search_registered_models()In [ ]python · cell 10
python
run_id = client.search_runs(experiment_ids='1')[0].info.run_id
mlflow.register_model(
model_uri=f"runs:/{run_id}/models",
name='iris-classifier'
)In [ ]python · cell 11
python
