{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tabular data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "hide_input": true }, "outputs": [], "source": [ "from fastai.gen_doc.nbdoc import *\n", "from fastai.tabular.models import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[`tabular`](/tabular.html#tabular) contains all the necessary classes to deal with tabular data, across two modules:\n", "- [`tabular.transform`](/tabular.transform.html#tabular.transform): defines the [`TabularTransform`](/tabular.transform.html#TabularTransform) class to help with preprocessing;\n", "- [`tabular.data`](/tabular.data.html#tabular.data): defines the [`TabularDataset`](/tabular.data.html#TabularDataset) that handles that data, as well as the methods to quickly get a [`TabularDataBunch`](/tabular.data.html#TabularDataBunch).\n", "\n", "To create a model, you'll need to use [`models.tabular`](/tabular.html#tabular). See below for an end-to-end example using all these modules." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preprocessing tabular data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's import everything we need for the tabular application." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from fastai import *\n", "from fastai.tabular import * " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tabular data usually comes in the form of a delimited file (such as .csv) containing variables of different kinds: text/category, numbers, and perhaps some missing values. The example we'll work with in this section is a sample of the [adult dataset](https://archive.ics.uci.edu/ml/datasets/adult) which has some census information on individuals. We'll use it to train a model to predict whether salary is greater than \\$50k or not." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PosixPath('/home/ubuntu/fastai/fastai/../data/adult_sample')" ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "path = untar_data(URLs.ADULT_SAMPLE)\n", "path" ] }, { "cell_type": "code", "execution_count": null, "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", " \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", "
ageworkclassfnlwgteducationeducation-nummarital-statusoccupationrelationshipracesexcapital-gaincapital-losshours-per-weeknative-country>=50k
049Private101320Assoc-acdm12.0Married-civ-spouseNaNWifeWhiteFemale0190240United-States1
144Private236746Masters14.0DivorcedExec-managerialNot-in-familyWhiteMale10520045United-States1
238Private96185HS-gradNaNDivorcedNaNUnmarriedBlackFemale0032United-States0
338Self-emp-inc112847Prof-school15.0Married-civ-spouseProf-specialtyHusbandAsian-Pac-IslanderMale0040United-States1
442Self-emp-not-inc822977th-8thNaNMarried-civ-spouseOther-serviceWifeBlackFemale0050United-States0
\n", "
" ], "text/plain": [ " age workclass fnlwgt education education-num \\\n", "0 49 Private 101320 Assoc-acdm 12.0 \n", "1 44 Private 236746 Masters 14.0 \n", "2 38 Private 96185 HS-grad NaN \n", "3 38 Self-emp-inc 112847 Prof-school 15.0 \n", "4 42 Self-emp-not-inc 82297 7th-8th NaN \n", "\n", " marital-status occupation relationship race \\\n", "0 Married-civ-spouse NaN Wife White \n", "1 Divorced Exec-managerial Not-in-family White \n", "2 Divorced NaN Unmarried Black \n", "3 Married-civ-spouse Prof-specialty Husband Asian-Pac-Islander \n", "4 Married-civ-spouse Other-service Wife Black \n", "\n", " sex capital-gain capital-loss hours-per-week native-country >=50k \n", "0 Female 0 1902 40 United-States 1 \n", "1 Male 10520 0 45 United-States 1 \n", "2 Female 0 0 32 United-States 0 \n", "3 Male 0 0 40 United-States 1 \n", "4 Female 0 0 50 United-States 0 " ] }, "execution_count": null, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(path/'adult.csv')\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here all the information that will form our input is in the 14 first columns, and the dependent variable is the last column. We will split our input between two types of variables: categorical and continuous.\n", "- Categorical variables will be replaced by a category - a unique id that identifies them - before they are passed through an embedding layer.\n", "- Continuous variables will be normalized and then directly fed to the model.\n", "\n", "Another thing we need to handle are the missing values: our model isn't going to like receiving NaNs so we should remove them in a smart way. All of this preprocessing is done by [`TabularTransform`](/tabular.transform.html#TabularTransform) objects and [`TabularDataset`](/tabular.data.html#TabularDataset).\n", "\n", "We can define a bunch of Transforms that will be applied to our variables. Here we transform all categorical variables into categories. We also replace missing values for continuous variables by the median column value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tfms = [FillMissing, Categorify]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we split our data into training and validation sets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_df, valid_df = df[:-2000].copy(),df[-2000:].copy()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then let's manually split our variables into categorical and continuous variables (we can ignore the dependant variable at this stage). fastai will assume all variables that aren't dependent or categorical are continuous, unless we explicitly pass a list to the `cont_names` parameter when constructing our [`DataBunch`](/basic_data.html#DataBunch)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dep_var = '>=50k'\n", "cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we're ready to pass this information to [`TabularDataBunch.from_df`](/tabular.data.html#TabularDataBunch.from_df) to create the [`DataBunch`](/basic_data.html#DataBunch) that we'll use for training." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['capital-gain', 'age', 'fnlwgt', 'hours-per-week', 'capital-loss', 'education-num']\n" ] } ], "source": [ "data = TabularDataBunch.from_df(path, train_df, valid_df, dep_var, tfms=tfms, cat_names=cat_names)\n", "print(data.train_ds.cont_names) # `cont_names` defaults to: set(df)-set(cat_names)-{dep_var}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can grab a mini-batch of data and take a look (note that [`to_np`](/torch_core.html#to_np) here converts from pytorch tensor to numpy):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 5 12 1 8 2 3 1 40 1]\n", " [ 5 12 3 8 1 5 2 40 1]\n", " [ 5 12 7 5 3 5 2 40 1]\n", " [ 7 12 3 4 1 5 2 40 1]\n", " [ 5 8 1 11 2 5 1 40 1]]\n", "[[-0.14592563 0.7598058 -0.3914412 -0.03578891 -0.21676035 -0.42159945]\n", " [-0.14592563 -0.63151586 -0.93344957 -0.03578891 -0.21676035 -0.42159945]\n", " [-0.02316255 0.17398615 -0.69717467 -0.03578891 -0.21676035 -0.42159945]\n", " [-0.14592563 -0.04569621 0.03022396 0.7721204 -0.21676035 -0.42159945]\n", " [-0.14592563 -0.33860603 -1.5195453 -0.03578891 -0.21676035 0.7539582 ]]\n", "[0 0 0 0 0]\n" ] } ], "source": [ "(cat_x,cont_x),y = next(iter(data.train_dl))\n", "for o in (cat_x, cont_x, y): print(to_np(o[:5]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After being processed in [`TabularDataset`](/tabular.data.html#TabularDataset), the categorical variables are replaced by ids and the continuous variables are normalized. The codes corresponding to categorical variables are all put together, as are all the continuous variables." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining a model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we have our data ready in a [`DataBunch`](/basic_data.html#DataBunch), we just need to create a model to then define a [`Learner`](/basic_train.html#Learner) and start training. The fastai library has a flexible and powerful [`TabularModel`](/tabular.models.html#TabularModel) in [`models.tabular`](/tabular.html#tabular). To use that function, we just need to specify the embedding sizes for each of our categorical variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HBox(children=(IntProgress(value=0, max=1), HTML(value='0.00% [0/1 00:00<00:00]'))), HTML(value…" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Total time: 00:04\n", "epoch train loss valid loss accuracy\n", "0 0.338518 0.320982 0.848500 (00:04)\n", "\n" ] } ], "source": [ "learn = get_tabular_learner(data, layers=[200,100], emb_szs={'native-country': 10}, metrics=accuracy)\n", "learn.fit_one_cycle(1, 1e-2)" ] } ], "metadata": { "jekyll": { "keywords": "fastai", "summary": "Application to tabular/structured data", "title": "tabular" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }