Custom scikit-learn model
In this exercise you are going to create a custom model using MLflow's pyfunc flavor. Using the insurance_charges dataset, the labels must be changed from female to 0 and male to 1 for classification during training. When using the model, the strings of female or male must be returned instead of 0 or 1.
The custom model is a Classification model based on LogisticRegression and will use a Class called CustomPredict. The CustomPredict adds an additional step in the predict method that sets your labels of 0 and 1 back to female and male when the model receives input. You will be using pyfunc flavor for logging and loading your model.
Our insurance_charges dataset will be preprocessed and model will be trained using:
lr_model = LogisticRegression().fit(X_train, y_train)
The MLflow module will be imported.
This exercise is part of the course
Introduction to MLflow
Exercise instructions
- Use MLflow's
pyfuncflavor to log the custom model. - Set
pyfuncpython_modelargument to use the Custom ClassCustomPredict(). - Load the custom model using
pyfunc.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Log the pyfunc model
____.____.____(
artifact_path="lr_pyfunc",
# Set model to use CustomPredict Class
python_model=____,
artifacts={"lr_model": "lr_model"}
)
run = mlflow.last_active_run()
run_id = run.info.run_id
# Load the model in python_function format
loaded_model = ____.____.____(f"runs:/{run_id}/lr_pyfunc")