{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F28_joblib.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/28_joblib.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #28 ([video](https://www.youtube.com/watch?v=L5OVCoAemAk&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=28))\n", "\n", "Want to save a model (or pipeline) for later use? Use joblib!\n", "\n", "Warning: You must load it into an identical environment, and only load objects you trust 😇\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.preprocessing import OneHotEncoder\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.pipeline import make_pipeline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cols = ['Embarked', 'Sex']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('http://bit.ly/kaggletrain', nrows=10)\n", "X = df[cols]\n", "y = df['Survived']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df_new = pd.read_csv('http://bit.ly/kaggletest', nrows=10)\n", "X_new = df_new[cols]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "ohe = OneHotEncoder()\n", "logreg = LogisticRegression(solver='liblinear', random_state=1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "pipe = make_pipeline(ohe, logreg)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 0, 0, 1, 0, 1, 0, 1, 0])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe.fit(X, y)\n", "pipe.predict(X_new)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['pipe.joblib']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# save the pipeline to a file\n", "import joblib\n", "joblib.dump(pipe, 'pipe.joblib')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# load the pipeline from a file\n", "same_pipe = joblib.load('pipe.joblib')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 0, 0, 1, 0, 1, 0, 1, 0])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use it to make the same predictions\n", "same_pipe.predict(X_new)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.4" } }, "nbformat": 4, "nbformat_minor": 4 }