Store and Reload the Trained Models

This example shows how to store a trained hyperbox-based model and reload it to make prediction. This example will use a random hyperboxes model for illustration.

[1]:
from sklearn.datasets import make_classification
from hbbrain.numerical_data.incremental_learner.iol_gfmm import ImprovedOnlineGFMM
from hbbrain.numerical_data.ensemble_learner.random_hyperboxes import RandomHyperboxesClassifier
from hbbrain.utils.model_storage import store_model, load_model

Generate training data

[2]:
X, y = make_classification(n_samples=100, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)
[3]:
# Normalise data into the range of [0, 1]
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(X)
X = scaler.transform(X)

Training a random hyperboxes model

[4]:
clf = RandomHyperboxesClassifier(base_estimator=ImprovedOnlineGFMM(0.1), n_estimators=10, random_state=0).fit(X, y)

Make prediction

[6]:
y_pred = clf.predict([[1, 0.6, 0.5, 0.2]])
print("Predicted class for the input patter [1, 0.6, 0.5, 0.2] is %d"%y_pred[0])
Predicted class for the input patter [1, 0.6, 0.5, 0.2] is 1

Store the trained model

[7]:
store_model(clf, "store_example_model.dummy")

Reload the trained model and make prediction

[8]:
clf_load = load_model("store_example_model.dummy")
[9]:
y_pred = clf_load.predict([[1, 0.6, 0.5, 0.2]])
print("Predicted class for the input patter [1, 0.6, 0.5, 0.2] is %d"%y_pred[0])
Predicted class for the input patter [1, 0.6, 0.5, 0.2] is 1