Package: h2o-mlflow-flavor
A tiny library containing aMLFlowflavor for working with H2O-3 MOJO and POJO models.Logging Models to MLFlow RegistryThe model that was trained with H2O-3 runtime can be exported to MLFlow registry withlog_modelfunction.:importmlflowimporth2o_mlflow_flavormlflow.set_tracking_uri("http://127.0.0.1:8080")h2o_model=...trainingphase...withmlflow.start_run(run_name="myrun")asrun:h2o_mlflow_flavor.log_model(h2o_model=h2o_model,artifact_path="folder",model_type="MOJO",extra_prediction_args=["--predictCalibrated"])Compared tolog_modelfunctions of the other flavors being a part of MLFlow, this function has two extra arguments:model_type- It indicates whether the model should be exported asMOJOorPOJO. The default value isMOJO.extra_prediction_args- A list of extra arguments for java scoring process. Possible values:--setConvertInvalidNum- The scoring process will convert invalid numbers to NA.--predictContributions- The scoring process will Return also Shapley values a long with the predictions. Model must support that Shapley values, otherwise scoring process will throw an error.--predictCalibrated- The scoring process will also return calibrated prediction values.Thesave_modelfunction that persists h2o binary model to MOJO or POJO has the same signature as thelog_modelfunction.Extracting Information about ModelThe flavor offers several functions to extract information about the model.get_metrics(h2o_model, metric_type=None)- Extracts metrics from the trained H2O binary model. It returns dictionary and takes following parameters:h2o_model- An H2O binary model.metric_type- The type of metrics. Possible values are “training”, “validation”, “cross_validation”. If parameter is not specified, metrics for all types are returned.get_params(h2o_model)- Extracts training parameters for the H2O binary model. It returns dictionary and expects one parameter:h2o_model- An H2O binary model.get_input_example(h2o_model, number_of_records=5, relevant_columns_only=True)- Creates an example Pandas dataset from the training dataset of H2O binary model. It takes following parameters:h2o_model- An H2O binary model.number_of_records- A number of records that will be extracted from the training dataset.relevant_columns_only- A flag indicating whether the output dataset should contain only columns required by the model. Defaults toTrue.The functions can be utilized as follows:importmlflowimporth2o_mlflow_flavormlflow.set_tracking_uri("http://127.0.0.1:8080")h2o_model=...trainingphase...withmlflow.start_run(run_name="myrun")asrun:mlflow.log_params(h2o_mlflow_flavor.get_params(h2o_model))mlflow.log_metrics(h2o_mlflow_flavor.get_metrics(h2o_model))input_example=h2o_mlflow_flavor.get_input_example(h2o_model)h2o_mlflow_flavor.log_model(h2o_model=h2o_model,input_example=input_example,artifact_path="folder",model_type="MOJO",extra_prediction_args=["--predictCalibrated"])Model ScoringAfter a model obtained from the model registry, the model doesn’t require h2o runtime for ability to score. The only thing
that model requires is ah2o-gemodel.jarwhich was persisted with the model during saving procedure.
The model could be loaded by the functionload_model(model_uri, dst_path=None). It returns an objecting making
predictions on Pandas dataframe and takes the following parameters:model_uri- An unique identifier of the model within MLFlow registry.dst_path- (Optional) A local filesystem path for downloading the persisted form of the model.The object for scoring could be obtained also via thepyfuncflavor as follows:importmlflowmlflow.set_tracking_uri("http://127.0.0.1:8080")logged_model='runs:/9a42265cf0ef484c905b02afb8fe6246/iris'loaded_model=mlflow.pyfunc.load_model(logged_model)importpandasaspddata=pd.read_csv("http://h2o-public-test-data.s3.amazonaws.com/smalldata/iris/iris_wheader.csv")loaded_model.predict(data)