Save and Load

Saving and loading models is desired as the learning proces of a model for hgboost can take up to hours. Below is shown a code example how to save and load models.

Saving

Documentation how to save a trained model hgboost.hgboost.hgboost.save():

Save learned model in pickle file.

param filepath

Pathname to store pickle files.

type filepath

str, (default: ‘hgboost_model.pkl’)

param overwrite

Overwite file if exists.

type overwrite

bool, (default=False)

param verbose

Show message. A higher number gives more informatie. The default is 3.

type verbose

int, optional

Examples

>>> # Initialize libraries
>>> from hgboost import hgboost
>>> import pandas as pd
>>> from sklearn import datasets
>>>
>>> # Load example dataset
>>> iris = datasets.load_iris()
>>> X = pd.DataFrame(iris.data, columns=iris['feature_names'])
>>> y = iris.target
>>>
>>> # Train model using user-defined parameters
>>> hgb = hgboost(max_eval=10, threshold=0.5, cv=5, test_size=0.2, val_size=0.2, top_cv_evals=10, random_state=42)
>>> results = hgb.xgboost(X, y, method="xgb_clf_multi")
>>>
>>> # Save
>>> hgb.save(filepath='hgboost_model.pkl', overwrite=True)
>>>
returns

bool – Status whether the file is saved.

rtype

[True, False]

Loading

Documentation how to load a trained model: func:hgboost.hgboost.hgboost.load:

Load learned model.

Description

The load function will restore the trained model and results. In a fresh (new) start, you need to re-initialize the hgboost model first. By loading the model, the user defined parameters are also restored.

param filepath

Pathname to stored pickle files.

type filepath

str

param verbose

Show message. A higher number gives more information. The default is 3.

type verbose

int, optional

Examples

>>> # Initialize libraries
>>> from hgboost import hgboost
>>> import pandas as pd
>>> from sklearn import datasets
>>>
>>> # Load example dataset
>>> iris = datasets.load_iris()
>>> X = pd.DataFrame(iris.data, columns=iris['feature_names'])
>>> y = iris.target
>>>
>>> # Train model using user-defined parameters
>>> hgb = hgboost(max_eval=10, threshold=0.5, cv=5, test_size=0.2, val_size=0.2, top_cv_evals=10, random_state=42)
>>> results = hgb.xgboost(X, y, method="xgb_clf_multi")
>>>
>>> # Save
>>> hgb.save(filepath='hgboost_model.pkl', overwrite=True)
>>>
>>> # Load
>>> from hgboost import hgboost
>>> hgb = hgboost()
>>> results = hgb.load(filepath='hgboost_model.pkl')
>>>
>>> # Make predictions again with:
>>> y_pred, y_proba = hgb.predict(X)
returns
  • * dictionary containing model results.

  • * Object with trained model.