apache_beam.ml.inference.xgboost_inference module

class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler(model_class: Union[Callable[[...], xgboost.core.Booster], Callable[[...], xgboost.sklearn.XGBModel]], model_state: str, inference_fn: Callable[[Sequence[object], Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], Optional[Dict[str, Any]]], Iterable[apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>)[source]

Bases: apache_beam.ml.inference.base.ModelHandler, abc.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

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() → Union[xgboost.core.Booster, xgboost.sklearn.XGBModel][source]
get_metrics_namespace() → str[source]
class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerNumpy(model_class: Union[Callable[[...], xgboost.core.Booster], Callable[[...], xgboost.sklearn.XGBModel]], model_state: str, inference_fn: Callable[[Sequence[object], Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], Optional[Dict[str, Any]]], Iterable[apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>)[source]

Bases: apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler

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

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[numpy.ndarray], model: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], inference_args: Optional[Dict[str, Any]] = None) → Iterable[apache_beam.ml.inference.base.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[numpy.ndarray]) → int[source]
Returns:The number of bytes of data for a batch.
class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerPandas(model_class: Union[Callable[[...], xgboost.core.Booster], Callable[[...], xgboost.sklearn.XGBModel]], model_state: str, inference_fn: Callable[[Sequence[object], Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], Optional[Dict[str, Any]]], Iterable[apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>)[source]

Bases: apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler

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

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[pandas.core.frame.DataFrame], model: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], inference_args: Optional[Dict[str, Any]] = None) → Iterable[apache_beam.ml.inference.base.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[pandas.core.frame.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: Union[Callable[[...], xgboost.core.Booster], Callable[[...], xgboost.sklearn.XGBModel]], model_state: str, inference_fn: Callable[[Sequence[object], Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], Optional[Dict[str, Any]]], Iterable[apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>)[source]

Bases: apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler

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

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[scipy.sparse._csr.csr_matrix], model: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], inference_args: Optional[Dict[str, Any]] = None) → Iterable[apache_beam.ml.inference.base.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[scipy.sparse._csr.csr_matrix]) → int[source]
Returns:The number of bytes of data for a batch.
class apache_beam.ml.inference.xgboost_inference.XGBoostModelHandlerDatatable(model_class: Union[Callable[[...], xgboost.core.Booster], Callable[[...], xgboost.sklearn.XGBModel]], model_state: str, inference_fn: Callable[[Sequence[object], Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], Optional[Dict[str, Any]]], Iterable[apache_beam.ml.inference.base.PredictionResult]] = <function default_xgboost_inference_fn>)[source]

Bases: apache_beam.ml.inference.xgboost_inference.XGBoostModelHandler

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

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: Union[xgboost.core.Booster, xgboost.sklearn.XGBModel], inference_args: Optional[Dict[str, Any]] = None) → Iterable[apache_beam.ml.inference.base.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.