MLFlow
Local
See local GUI
mlflow ui
Example
import mlflow
mlflow.set_tracking_uri("http://localhost:5000")
Databricks
Log model
import mlflow
import mlflow.sklearn
mlflow.set_experiment(path)
with mlflow.start_run(run_name="default_skl_random_forest_regressor") as run:
mlflow.sklearn.log_model(model, "DefaultRandomForestRegressor")
mlflow.log_params(params)
mlflow.log_metrics({"r2": r2, "mae": mae, "mse": mse, "rmse": rmse, "mape":_mape})
print("run id {}, experiment id {}".format(run.info.run_uuid, run.info.experiment_id))
model = mlflow.sklearn.load_model("runs:/{}/DefaultRandomForestRegressor".format(run.info.run_uuid))
Auto log model
import mlflow
mlflow.set_experiment(path)
import mlflow.sklearn
mlflow.sklearn.autolog()
with mlflow.start_run():
model = RandomForestRegressor(n_jobs=-1, random_state=42)
params = model.get_params()
# Set the model parameters
params["n_estimators"] = 1000
model = RandomForestRegressor(**params)
model.fit(X_train, y_train)
yhat = model.predict(X_test)
Register model
Click on the experiment and click register
The path remains the same
Load a model locally from databricks
export MLFLOW_TRACKING_URI="databricks"
export DATABRICKS_HOST="https://eastus2.azuredatabricks.net/"
export DATABRICKS_TOKEN="..." # https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/authentication#token-management
import mlflow.sklearn
model = mlflow.sklearn.load_model("models:/REGISTERED_MODEL_NAME/Production")