.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_model_persistence.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_model_persistence.py: ===================== Model Persistence ===================== HiClass is fully compatible with Pickle. Pickle can be used to easily store machine learning models on disk. In this example, we demonstrate how to use pickle to store and load trained classifiers. .. GENERATED FROM PYTHON SOURCE LINES 11-46 .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [['Animal' 'Reptile' 'Lizard'] ['Animal' 'Reptile' 'Snake'] ['Animal' 'Mammal' 'Cow'] ['Animal' 'Mammal' 'Sheep']] | .. code-block:: default import pickle from sklearn.linear_model import LogisticRegression from hiclass import LocalClassifierPerLevel # Define data X_train = [[1, 2], [3, 4], [5, 6], [7, 8]] X_test = [[7, 8], [5, 6], [3, 4], [1, 2]] Y_train = [ ["Animal", "Mammal", "Sheep"], ["Animal", "Mammal", "Cow"], ["Animal", "Reptile", "Snake"], ["Animal", "Reptile", "Lizard"], ] # Use Logistic Regression classifiers for every level in the hierarchy lr = LogisticRegression() classifier = LocalClassifierPerLevel(local_classifier=lr) # Train local classifier per level classifier.fit(X_train, Y_train) # Save the model to disk filename = "trained_model.sav" pickle.dump(classifier, open(filename, "wb")) # Some time in the future... # Load the model from disk loaded_model = pickle.load(open(filename, "rb")) # Predict predictions = loaded_model.predict(X_test) print(predictions) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.019 seconds) .. _sphx_glr_download_auto_examples_plot_model_persistence.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_model_persistence.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_model_persistence.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_