apache_beam.ml.inference.xgboost_inference module

class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler(model_class: ~typing.Callable[[...], xgboost.Booster] | ~typing.Callable[[...], xgboost.XGBModel], model_state: str, inference_fn: ~typing.Callable[[~typing.Sequence[object], xgboost.Booster | xgboost.XGBModel, ~typing.Dict[str, ~typing.Any] | None], ~typing.Iterable[~apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, **kwargs)[source]

Bases: ModelHandler[ExampleT, PredictionT, ModelT], ABC

Implementation of the ModelHandler interface for XGBoost.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandler(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))

See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html for details

Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

  • min_batch_size – optional. the minimum batch size to use when batching inputs.

  • max_batch_size – optional. the maximum batch size to use when batching inputs.

  • max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.

  • kwargs – ‘env_vars’ can be used to set environment variables before loading the model.

Supported Versions: RunInference APIs in Apache Beam have been tested with XGBoost 1.6.0 and 1.7.0

XGBoost 1.0.0 introduced support for using JSON to save and load XGBoost models. XGBoost 1.6.0, additional support for Universal Binary JSON. It is recommended to use a model trained in XGBoost 1.6.0 or higher. While you should be able to load models created in older versions, there are no guarantees this will work as expected.

This class is the superclass of all the various XGBoostModelhandlers and should not be instantiated directly. (See instead XGBoostModelHandlerNumpy, XGBoostModelHandlerPandas, etc.)

load_model() xgboost.Booster | xgboost.XGBModel[source]
get_metrics_namespace() str[source]
batch_elements_kwargs() Mapping[str, Any][source]
class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerNumpy(model_class: ~typing.Callable[[...], xgboost.Booster] | ~typing.Callable[[...], xgboost.XGBModel], model_state: str, inference_fn: ~typing.Callable[[~typing.Sequence[object], xgboost.Booster | xgboost.XGBModel, ~typing.Dict[str, ~typing.Any] | None], ~typing.Iterable[~apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, **kwargs)[source]

Bases: XGBoostModelHandler[ndarray, PredictionResult, Booster | XGBModel]

Implementation of the ModelHandler interface for XGBoost using numpy arrays as input.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandlerNumpy(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))
Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

Implementation of the ModelHandler interface for XGBoost.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandler(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))

See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html for details

Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

  • min_batch_size – optional. the minimum batch size to use when batching inputs.

  • max_batch_size – optional. the maximum batch size to use when batching inputs.

  • max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.

  • kwargs – ‘env_vars’ can be used to set environment variables before loading the model.

Supported Versions: RunInference APIs in Apache Beam have been tested with XGBoost 1.6.0 and 1.7.0

XGBoost 1.0.0 introduced support for using JSON to save and load XGBoost models. XGBoost 1.6.0, additional support for Universal Binary JSON. It is recommended to use a model trained in XGBoost 1.6.0 or higher. While you should be able to load models created in older versions, there are no guarantees this will work as expected.

This class is the superclass of all the various XGBoostModelhandlers and should not be instantiated directly. (See instead XGBoostModelHandlerNumpy, XGBoostModelHandlerPandas, etc.)

run_inference(batch: Sequence[ndarray], model: xgboost.Booster | xgboost.XGBModel, inference_args: Dict[str, Any] | None = None) Iterable[PredictionResult][source]

Runs inferences on a batch of 2d numpy arrays.

Parameters:
  • batch – A sequence of examples as 2d numpy arrays. Each row in an array is a single example. The dimensions must match the dimensions of the data used to train the model.

  • model – XGBoost booster or XBGModel (sklearn interface). Must implement predict(X). Where the parameter X is a 2d numpy array.

  • inference_args – Any additional arguments for an inference.

Returns:

An Iterable of type PredictionResult.

get_num_bytes(batch: Sequence[ndarray]) int[source]
Returns:

The number of bytes of data for a batch.

class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerPandas(model_class: ~typing.Callable[[...], xgboost.Booster] | ~typing.Callable[[...], xgboost.XGBModel], model_state: str, inference_fn: ~typing.Callable[[~typing.Sequence[object], xgboost.Booster | xgboost.XGBModel, ~typing.Dict[str, ~typing.Any] | None], ~typing.Iterable[~apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, **kwargs)[source]

Bases: XGBoostModelHandler[DataFrame, PredictionResult, Booster | XGBModel]

Implementation of the ModelHandler interface for XGBoost using pandas dataframes as input.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandlerPandas(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))
Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

Implementation of the ModelHandler interface for XGBoost.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandler(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))

See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html for details

Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

  • min_batch_size – optional. the minimum batch size to use when batching inputs.

  • max_batch_size – optional. the maximum batch size to use when batching inputs.

  • max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.

  • kwargs – ‘env_vars’ can be used to set environment variables before loading the model.

Supported Versions: RunInference APIs in Apache Beam have been tested with XGBoost 1.6.0 and 1.7.0

XGBoost 1.0.0 introduced support for using JSON to save and load XGBoost models. XGBoost 1.6.0, additional support for Universal Binary JSON. It is recommended to use a model trained in XGBoost 1.6.0 or higher. While you should be able to load models created in older versions, there are no guarantees this will work as expected.

This class is the superclass of all the various XGBoostModelhandlers and should not be instantiated directly. (See instead XGBoostModelHandlerNumpy, XGBoostModelHandlerPandas, etc.)

run_inference(batch: Sequence[DataFrame], model: xgboost.Booster | xgboost.XGBModel, inference_args: Dict[str, Any] | None = None) Iterable[PredictionResult][source]

Runs inferences on a batch of pandas dataframes.

Parameters:
  • batch – A sequence of examples as pandas dataframes. Each row in a dataframe is a single example. The dimensions must match the dimensions of the data used to train the model.

  • model – XGBoost booster or XBGModel (sklearn interface). Must implement predict(X). Where the parameter X is a pandas dataframe.

  • inference_args – Any additional arguments for an inference.

Returns:

An Iterable of type PredictionResult.

get_num_bytes(batch: Sequence[DataFrame]) int[source]
Returns:

The number of bytes of data for a batch of Numpy arrays.

class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerSciPy(model_class: ~typing.Callable[[...], xgboost.Booster] | ~typing.Callable[[...], xgboost.XGBModel], model_state: str, inference_fn: ~typing.Callable[[~typing.Sequence[object], xgboost.Booster | xgboost.XGBModel, ~typing.Dict[str, ~typing.Any] | None], ~typing.Iterable[~apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, **kwargs)[source]

Bases: XGBoostModelHandler[csr_matrix, PredictionResult, Booster | XGBModel]

Implementation of the ModelHandler interface for XGBoost using scipy matrices as input.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandlerSciPy(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))
Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

Implementation of the ModelHandler interface for XGBoost.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandler(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))

See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html for details

Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

  • min_batch_size – optional. the minimum batch size to use when batching inputs.

  • max_batch_size – optional. the maximum batch size to use when batching inputs.

  • max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.

  • kwargs – ‘env_vars’ can be used to set environment variables before loading the model.

Supported Versions: RunInference APIs in Apache Beam have been tested with XGBoost 1.6.0 and 1.7.0

XGBoost 1.0.0 introduced support for using JSON to save and load XGBoost models. XGBoost 1.6.0, additional support for Universal Binary JSON. It is recommended to use a model trained in XGBoost 1.6.0 or higher. While you should be able to load models created in older versions, there are no guarantees this will work as expected.

This class is the superclass of all the various XGBoostModelhandlers and should not be instantiated directly. (See instead XGBoostModelHandlerNumpy, XGBoostModelHandlerPandas, etc.)

run_inference(batch: Sequence[csr_matrix], model: xgboost.Booster | xgboost.XGBModel, inference_args: Dict[str, Any] | None = None) Iterable[PredictionResult][source]

Runs inferences on a batch of SciPy sparse matrices.

Parameters:
  • batch – A sequence of examples as Scipy sparse matrices. The dimensions must match the dimensions of the data used to train the model.

  • model – XGBoost booster or XBGModel (sklearn interface). Must implement predict(X). Where the parameter X is a SciPy sparse matrix.

  • inference_args – Any additional arguments for an inference.

Returns:

An Iterable of type PredictionResult.

get_num_bytes(batch: Sequence[csr_matrix]) int[source]
Returns:

The number of bytes of data for a batch.

class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerDatatable(model_class: ~typing.Callable[[...], xgboost.Booster] | ~typing.Callable[[...], xgboost.XGBModel], model_state: str, inference_fn: ~typing.Callable[[~typing.Sequence[object], xgboost.Booster | xgboost.XGBModel, ~typing.Dict[str, ~typing.Any] | None], ~typing.Iterable[~apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, **kwargs)[source]

Bases: XGBoostModelHandler[Frame, PredictionResult, Booster | XGBModel]

Implementation of the ModelHandler interface for XGBoost using datatable dataframes as input.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandlerDatatable(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))
Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

Implementation of the ModelHandler interface for XGBoost.

Example Usage:

pcoll | RunInference(
            XGBoostModelHandler(
                model_class="XGBoost Model Class",
                model_state="my_model_state.json")))

See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html for details

Parameters:
  • model_class – class of the XGBoost model that defines the model structure.

  • model_state – path to a json file that contains the model’s configuration.

  • inference_fn – the inference function to use during RunInference. default=default_xgboost_inference_fn

  • min_batch_size – optional. the minimum batch size to use when batching inputs.

  • max_batch_size – optional. the maximum batch size to use when batching inputs.

  • max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.

  • kwargs – ‘env_vars’ can be used to set environment variables before loading the model.

Supported Versions: RunInference APIs in Apache Beam have been tested with XGBoost 1.6.0 and 1.7.0

XGBoost 1.0.0 introduced support for using JSON to save and load XGBoost models. XGBoost 1.6.0, additional support for Universal Binary JSON. It is recommended to use a model trained in XGBoost 1.6.0 or higher. While you should be able to load models created in older versions, there are no guarantees this will work as expected.

This class is the superclass of all the various XGBoostModelhandlers and should not be instantiated directly. (See instead XGBoostModelHandlerNumpy, XGBoostModelHandlerPandas, etc.)

run_inference(batch: Sequence[datatable.Frame], model: xgboost.Booster | xgboost.XGBModel, inference_args: Dict[str, Any] | None = None) Iterable[PredictionResult][source]

Runs inferences on a batch of datatable dataframe.

Parameters:
  • batch – A sequence of examples as datatable dataframes. Each row in a dataframe is a single example. The dimensions must match the dimensions of the data used to train the model.

  • model – XGBoost booster or XBGModel (sklearn interface). Must implement predict(X). Where the parameter X is a datatable dataframe.

  • inference_args – Any additional arguments for an inference.

Returns:

An Iterable of type PredictionResult.

get_num_bytes(batch: Sequence[datatable.Frame]) int[source]
Returns:

The number of bytes of data for a batch.