{ "cells": [ { "cell_type": "markdown", "id": "2d664323-df2d-4f48-a745-195a3e5a8171", "metadata": {}, "source": [ "### Introduction\n", "\n", "After we have created a model we will typically want to save it as a binary file so it can be deployed for prediction.\n", "\n", "Seldon core is a model deployment engine. Here is an excerpt from the seldon documentation for Scikit-learn deployments\n", "\n", "> Seldon expects that your model has been saved using joblib, and it named as model.joblib. Note that this is the recommended approach to serialise models by the SKLearn project.\n", ">\n", "> _Source: https://docs.seldon.io/projects/seldon-core/en/latest/servers/sklearn.html_" ] }, { "cell_type": "markdown", "id": "d867015f-dd51-463d-8ae5-afba08444fe4", "metadata": {}, "source": [ "### Saving a model\n", "\n", "Here we use the [joblib](https://joblib.readthedocs.io/en/latest/persistence.html) Python library to save a model:" ] }, { "cell_type": "code", "execution_count": 8, "id": "6fa58627-7b0f-43b7-997e-ec0db3b29705", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.9/site-packages/sklearn/svm/_base.py:1206: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n", " warnings.warn(\n" ] }, { "data": { "text/plain": [ "['iris_model.joblib']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set the random state to make the examples repeatable\n", "import numpy as np\n", "np.random.seed(1)\n", "\n", "from sklearn.datasets import load_iris\n", "from sklearn.svm import LinearSVC\n", "\n", "iris = load_iris()\n", "X, y = iris.data, iris.target\n", "\n", "classifier = LinearSVC()\n", "model = classifier.fit(X, y)\n", "\n", "from joblib import dump, load\n", "dump(model, 'iris_model.joblib') " ] }, { "cell_type": "markdown", "id": "5b39f67d-69b3-4a57-93db-1dcb1a936b31", "metadata": {}, "source": [ "We can execute a shell command with `!`. Let's inspect the saved file:" ] }, { "cell_type": "code", "execution_count": 9, "id": "4dc28f2b-973a-469f-baf3-9043961473c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 jovyan users 858 Feb 11 15:23 iris_model.joblib\n" ] } ], "source": [ "! ls -l iris_model.joblib" ] }, { "cell_type": "markdown", "id": "8b202252-35aa-4a5d-8466-4f0c85a158d8", "metadata": {}, "source": [ "### Summary\n", "\n", "More information on model persistence can be found in the Scikit-learn [docs](https://scikit-learn.org/stable/modules/model_persistence.html#)" ] }, { "cell_type": "markdown", "id": "9f253e47-5b03-4a85-bbd9-e5d3fedec1d8", "metadata": {}, "source": [ "---\n", "\n", "### Navigation\n", "\n", "[Previous](./09_model_performance.ipynb) | [Home](./00-README-FIRST.ipynb) | [Next](./11_model_predictions.ipynb) notebook" ] }, { "cell_type": "code", "execution_count": null, "id": "88691c8f-b8c7-48f1-a139-e12f8d9d821f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }