{ "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%2F27_impute_categorical_features.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/27_impute_categorical_features.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #27 ([video](https://www.youtube.com/watch?v=k3KrhjvaCq0&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=27))\n", "\n", "Need to impute missing values for a categorical feature?\n", "\n", "Two options:\n", "\n", "1. Impute the most frequent value\n", "2. Impute the value \"missing\", which treats it as a separate category\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "X = pd.DataFrame({'Shape':['square', 'square', 'oval', 'circle', np.nan]})" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Shape
0square
1square
2oval
3circle
4NaN
\n", "
" ], "text/plain": [ " Shape\n", "0 square\n", "1 square\n", "2 oval\n", "3 circle\n", "4 NaN" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.impute import SimpleImputer" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['square'],\n", " ['square'],\n", " ['oval'],\n", " ['circle'],\n", " ['square']], dtype=object)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imputer = SimpleImputer(strategy='most_frequent')\n", "imputer.fit_transform(X)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['square'],\n", " ['square'],\n", " ['oval'],\n", " ['circle'],\n", " ['missing']], dtype=object)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "imputer = SimpleImputer(strategy='constant', fill_value='missing')\n", "imputer.fit_transform(X)" ] }, { "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 }