{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F27_impute_categorical_features.ipynb)\n", "\n", "[](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", " | Shape | \n", "
|---|---|
| 0 | \n", "square | \n", "
| 1 | \n", "square | \n", "
| 2 | \n", "oval | \n", "
| 3 | \n", "circle | \n", "
| 4 | \n", "NaN | \n", "