{ "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%2F43_ordinal_encoding_for_trees.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/43_ordinal_encoding_for_trees.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #43 ([video](https://www.youtube.com/watch?v=n_x40CdPZss&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=43))\n", "\n", "With a tree-based model, try OrdinalEncoder instead of OneHotEncoder even for nominal (unordered) features.\n", "\n", "Accuracy will often be similar, but OrdinalEncoder will be much faster!\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "df = pd.read_csv('https://www.openml.org/data/get_csv/1595261/adult-census.csv')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder\n", "from sklearn.ensemble import RandomForestClassifier\n", "from sklearn.pipeline import make_pipeline\n", "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "categorical_cols = ['workclass', 'education', 'marital-status',\n", " 'occupation', 'relationship', 'race', 'sex']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "X = df[categorical_cols]\n", "y = df['class']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(48842, 60)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# OneHotEncoder creates 60 columns\n", "ohe = OneHotEncoder()\n", "ohe.fit_transform(X).shape" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(48842, 7)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# OrdinalEncoder creates 7 columns\n", "oe = OrdinalEncoder()\n", "oe.fit_transform(X).shape" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Random Forests is a tree-based model\n", "rf = RandomForestClassifier(random_state=1, n_jobs=-1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.95 s, sys: 189 ms, total: 2.14 s\n", "Wall time: 23.2 s\n" ] }, { "data": { "text/plain": [ "0.8262561170407418" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pipeline containing OneHotEncoder\n", "ohe_pipe = make_pipeline(ohe, rf)\n", "%time cross_val_score(ohe_pipe, X, y).mean()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.67 s, sys: 133 ms, total: 1.81 s\n", "Wall time: 3.83 s\n" ] }, { "data": { "text/plain": [ "0.8256623624061437" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pipeline containing OrdinalEncoder\n", "oe_pipe = make_pipeline(oe, rf)\n", "%time cross_val_score(oe_pipe, X, y).mean()" ] }, { "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 }