{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "# import seaborn as sns\n", "# import matplotlib.pyplot as plt\n", "import os\n", "from sklearn.model_selection import train_test_split\n", "\n", "import category_encoders as ce\n", "from feature_engineering import encoding\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Dataset" ] }, { "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", " \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", "
SurvivedPclassSexAgeSibSpFare
003male22.017.2500
111female38.0171.2833
213female26.007.9250
311female35.0153.1000
403male35.008.0500
\n", "
" ], "text/plain": [ " Survived Pclass Sex Age SibSp Fare\n", "0 0 3 male 22.0 1 7.2500\n", "1 1 1 female 38.0 1 71.2833\n", "2 1 3 female 26.0 0 7.9250\n", "3 1 1 female 35.0 1 53.1000\n", "4 0 3 male 35.0 0 8.0500" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "use_cols = [\n", " 'Pclass', 'Sex', 'Age', 'Fare', 'SibSp',\n", " 'Survived'\n", "]\n", "\n", "data = pd.read_csv('./data/titanic.csv', usecols=use_cols)\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((623, 6), (268, 6))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X_train, X_test, y_train, y_test = train_test_split(data, data.Survived, test_size=0.3,\n", " random_state=0)\n", "X_train.shape, X_test.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One-hot encoding\n", "replace the categorical variable by different boolean variables (0/1) to indicate whether or not certain label is true for that observation" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data1 = pd.get_dummies(data,drop_first=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "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", " \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", "
SurvivedPclassAgeSibSpFareSex_male
00322.017.25001
11138.0171.28330
21326.007.92500
31135.0153.10000
40335.008.05001
\n", "
" ], "text/plain": [ " Survived Pclass Age SibSp Fare Sex_male\n", "0 0 3 22.0 1 7.2500 1\n", "1 1 1 38.0 1 71.2833 0\n", "2 1 3 26.0 0 7.9250 0\n", "3 1 1 35.0 1 53.1000 0\n", "4 0 3 35.0 0 8.0500 1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data1.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ordinal-encoding\n", "replace the labels by some ordinal number if ordinal is meaningful" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ord_enc = ce.OrdinalEncoder(cols=['Sex']).fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Survived Pclass Sex Age SibSp Fare\n", "0 0 3 1 22.0 1 7.2500\n", "1 1 1 2 38.0 1 71.2833\n", "2 1 3 2 26.0 0 7.9250\n", "3 1 1 2 35.0 1 53.1000\n", "4 0 3 1 35.0 0 8.0500\n" ] } ], "source": [ "data4 = ord_enc.transform(data)\n", "print(data4.head(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mean encoding\n", "replace the label by the mean of the target for that label. \n", "(the target must be 0/1 valued or continuous)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Sex\n", "female 0.753488\n", "male 0.196078\n", "Name: Survived, dtype: float64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# cross check-- the mean of target group by Sex\n", "X_train['Survived'].groupby(data['Sex']).mean()\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "mean_enc = encoding.MeanEncoding(cols=['Sex']).fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Survived Pclass Sex Age SibSp Fare\n", "0 0 3 0.196078 22.0 1 7.2500\n", "1 1 1 0.753488 38.0 1 71.2833\n", "2 1 3 0.753488 26.0 0 7.9250\n", "3 1 1 0.753488 35.0 1 53.1000\n", "4 0 3 0.196078 35.0 0 8.0500\n" ] } ], "source": [ "data6 = mean_enc.transform(data)\n", "print(data6.head(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Target-encoding\n", "Similar to mean encoding, but use both posterior probability and prior probability of the target" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# create the encoder and fit with our data\n", "target_enc = ce.TargetEncoder(cols=['Sex']).fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# perform transformation\n", "# data.Survived.groupby(data['Sex']).agg(['mean'])\n", "data2 = target_enc.transform(data)" ] }, { "cell_type": "code", "execution_count": 13, "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", " \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", "
SurvivedPclassSexAgeSibSpFare
0030.19607822.017.2500
1110.75348838.0171.2833
2130.75348826.007.9250
3110.75348835.0153.1000
4030.19607835.008.0500
\n", "
" ], "text/plain": [ " Survived Pclass Sex Age SibSp Fare\n", "0 0 3 0.196078 22.0 1 7.2500\n", "1 1 1 0.753488 38.0 1 71.2833\n", "2 1 3 0.753488 26.0 0 7.9250\n", "3 1 1 0.753488 35.0 1 53.1000\n", "4 0 3 0.196078 35.0 0 8.0500" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# check the result\n", "data2.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## WOE-encoding\n", "replace the label with Weight of Evidence of each label. WOE is computed from the basic odds ratio: \n", "\n", "ln( (Proportion of Good Outcomes) / (Proportion of Bad Outcomes))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "woe_enc = ce.WOEEncoder(cols=['Sex']).fit(X_train,y_train)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data3 = woe_enc.transform(data)" ] }, { "cell_type": "code", "execution_count": 16, "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", " \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", "
SurvivedPclassSexAgeSibSpFare
003-0.95074222.017.2500
1111.55563338.0171.2833
2131.55563326.007.9250
3111.55563335.0153.1000
403-0.95074235.008.0500
\n", "
" ], "text/plain": [ " Survived Pclass Sex Age SibSp Fare\n", "0 0 3 -0.950742 22.0 1 7.2500\n", "1 1 1 1.555633 38.0 1 71.2833\n", "2 1 3 1.555633 26.0 0 7.9250\n", "3 1 1 1.555633 35.0 1 53.1000\n", "4 0 3 -0.950742 35.0 0 8.0500" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data3.head(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }