{ "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%2F12_pipeline_vs_make_pipeline.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/12_pipeline_vs_make_pipeline.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #12 ([video](https://www.youtube.com/watch?v=lkFwwquv_ss&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=12))\n", "\n", "Q: What's the difference between Pipeline and make_pipeline?\n", "\n", "A: Pipeline requires naming of steps, make_pipeline does not.\n", "\n", "(Same applies to ColumnTransformer vs make_column_transformer)\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('http://bit.ly/kaggletrain', nrows=6)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "cols = ['Embarked', 'Sex', 'Age', 'Fare']\n", "X = df[cols]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.preprocessing import OneHotEncoder\n", "from sklearn.impute import SimpleImputer\n", "from sklearn.linear_model import LogisticRegression" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "ohe = OneHotEncoder()\n", "imp = SimpleImputer()\n", "clf = LogisticRegression()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from sklearn.compose import make_column_transformer\n", "from sklearn.pipeline import make_pipeline" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "ct = make_column_transformer(\n", " (ohe, ['Embarked', 'Sex']),\n", " (imp, ['Age']),\n", " remainder='passthrough')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "pipe = make_pipeline(ct, clf)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from sklearn.compose import ColumnTransformer\n", "from sklearn.pipeline import Pipeline" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "ct = ColumnTransformer(\n", " [('encoder', ohe, ['Embarked', 'Sex']),\n", " ('imputer', imp, ['Age'])],\n", " remainder='passthrough')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "pipe = Pipeline([('preprocessor', ct), ('classifier', clf)])" ] }, { "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 }