Tabular training

Open In Colab

How to use the tabular application in fastai

To illustrate the tabular application, we will use the example of the Adult dataset where we have to predict if a person is earning more or less than $50k per year using some general data.

  1. from fastai.tabular.all import *

We can download a sample of this dataset with the usual untar_data command:

  1. path = untar_data(URLs.ADULT_SAMPLE)
  2. path.ls()
  1. (#3) [Path('/home/ml1/.fastai/data/adult_sample/models'),Path('/home/ml1/.fastai/data/adult_sample/export.pkl'),Path('/home/ml1/.fastai/data/adult_sample/adult.csv')]

Then we can have a look at how the data is structured:

  1. df = pd.read_csv(path/'adult.csv')
  2. df.head()
ageworkclassfnlwgteducationeducation-nummarital-statusoccupationrelationshipracesexcapital-gaincapital-losshours-per-weeknative-countrysalary
049Private101320Assoc-acdm12.0Married-civ-spouseNaNWifeWhiteFemale0190240United-States>=50k
144Private236746Masters14.0DivorcedExec-managerialNot-in-familyWhiteMale10520045United-States>=50k
238Private96185HS-gradNaNDivorcedNaNUnmarriedBlackFemale0032United-States<50k
338Self-emp-inc112847Prof-school15.0Married-civ-spouseProf-specialtyHusbandAsian-Pac-IslanderMale0040United-States>=50k
442Self-emp-not-inc822977th-8thNaNMarried-civ-spouseOther-serviceWifeBlackFemale0050United-States<50k

Some of the columns are continuous (like age) and we will treat them as float numbers we can feed our model directly. Others are categorical (like workclass or education) and we will convert them to a unique index that we will feed to embedding layers. We can specify our categorical and continuous column names, as well as the name of the dependent variable in TabularDataLoaders factory methods:

  1. dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
  2. cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race'],
  3. cont_names = ['age', 'fnlwgt', 'education-num'],
  4. procs = [Categorify, FillMissing, Normalize])

The last part is the list of pre-processors we apply to our data:

  • Categorify is going to take every categorical variable and make a map from integer to unique categories, then replace the values by the corresponding index.
  • FillMissing will fill the missing values in the continuous variables by the median of existing values (you can choose a specific value if you prefer)
  • Normalize will normalize the continuous variables (substract the mean and divide by the std)

To further expose what’s going on below the surface, let’s rewrite this utilizing fastai‘s TabularPandas class. We will need to make one adjustment, which is defining how we want to split our data. By default the factory method above used a random 80/20 split, so we will do the same:

  1. splits = RandomSplitter(valid_pct=0.2)(range_of(df))
  1. to = TabularPandas(df, procs=[Categorify, FillMissing,Normalize],
  2. cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race'],
  3. cont_names = ['age', 'fnlwgt', 'education-num'],
  4. y_names='salary',
  5. splits=splits)

Once we build our TabularPandas object, our data is completely preprocessed as seen below:

  1. to.xs.iloc[:2]
workclasseducationmarital-statusoccupationrelationshipraceeducation-num_naagefnlwgteducation-num
15780216152510.9840372.210372-0.033692
1744251258251-1.509555-0.319624-0.425324

Now we can build our DataLoaders again:

  1. dls = to.dataloaders(bs=64)

Later we will explore why using TabularPandas to preprocess will be valuable.

The show_batch method works like for every other application:

  1. dls.show_batch()
workclasseducationmarital-statusoccupationrelationshipraceeducation-num_naagefnlwgteducation-numsalary
0State-govBachelorsMarried-civ-spouseProf-specialtyWifeWhiteFalse41.00000075409.00118213.0>=50k
1PrivateSome-collegeNever-marriedCraft-repairNot-in-familyWhiteFalse24.00000038455.00501310.0<50k
2PrivateAssoc-acdmMarried-civ-spouseProf-specialtyHusbandWhiteFalse48.000000101299.00309312.0<50k
3PrivateHS-gradNever-marriedOther-serviceOther-relativeBlackFalse42.000000227465.9992819.0<50k
4State-govSome-collegeNever-marriedProf-specialtyNot-in-familyWhiteFalse20.999999258489.99713010.0<50k
5Local-gov12thMarried-civ-spouseTech-supportHusbandWhiteFalse39.000000207853.0000678.0<50k
6PrivateAssoc-vocMarried-civ-spouseSalesHusbandWhiteFalse36.000000238414.99893011.0>=50k
7PrivateHS-gradNever-marriedCraft-repairNot-in-familyWhiteFalse19.000000445727.9989379.0<50k
8Local-govBachelorsMarried-civ-spouse#na#HusbandWhiteTrue59.000000196013.00017410.0>=50k
9PrivateHS-gradMarried-civ-spouseProf-specialtyWifeBlackFalse39.000000147500.0004039.0<50k

We can define a model using the tabular_learner method. When we define our model, fastai will try to infer the loss function based on our y_names earlier.

Note: Sometimes with tabular data, your y‘s may be encoded (such as 0 and 1). In such a case you should explicitly pass y_block = CategoryBlock in your constructor so fastai won’t presume you are doing regression.

  1. learn = tabular_learner(dls, metrics=accuracy)

And we can train that model with the fit_one_cycle method (the fine_tune method won’t be useful here since we don’t have a pretrained model).

  1. learn.fit_one_cycle(1)
epochtrain_lossvalid_lossaccuracytime
00.3693600.3480960.84075600:05

We can then have a look at some predictions:

  1. learn.show_results()
workclasseducationmarital-statusoccupationrelationshipraceeducation-num_naagefnlwgteducation-numsalarysalary_pred
05.012.03.08.01.05.01.00.324868-1.138177-0.4240220.00.0
15.010.05.02.02.05.01.0-0.482055-1.3519111.1484380.00.0
25.012.06.012.03.05.01.0-0.7754820.138709-0.4240220.00.0
35.016.05.02.04.04.01.0-1.362335-0.227515-0.0309070.00.0
45.02.05.00.04.05.01.0-1.509048-0.191191-1.2102520.00.0
55.016.03.013.01.05.01.01.498575-0.051096-0.0309071.01.0
65.012.03.015.01.05.01.0-0.5554120.039167-0.4240220.00.0
75.01.05.06.04.05.01.0-1.582405-1.396391-1.6033670.00.0
85.03.05.013.02.05.01.0-1.3623350.158354-0.8171370.00.0

Or use the predict method on a row:

  1. row, clas, probs = learn.predict(df.iloc[0])
  1. row.show()
workclasseducationmarital-statusoccupationrelationshipraceeducation-num_naagefnlwgteducation-numsalary
0PrivateAssoc-acdmMarried-civ-spouse#na#WifeWhiteFalse49.0101319.9978812.0>=50k
  1. clas, probs
  1. (tensor(1), tensor([0.4995, 0.5005]))

To get prediction on a new dataframe, you can use the test_dl method of the DataLoaders. That dataframe does not need to have the dependent variable in its column.

  1. test_df = df.copy()
  2. test_df.drop(['salary'], axis=1, inplace=True)
  3. dl = learn.dls.test_dl(test_df)

Then Learner.get_preds will give you the predictions:

  1. learn.get_preds(dl=dl)
  1. (tensor([[0.4995, 0.5005],
  2. [0.4882, 0.5118],
  3. [0.9824, 0.0176],
  4. ...,
  5. [0.5324, 0.4676],
  6. [0.7628, 0.2372],
  7. [0.5934, 0.4066]]), None)

Note: Since machine learning models can’t magically understand categories it was never trained on, the data should reflect this. If there are different missing values in your test data you should address this before training

fastai with Other Libraries

As mentioned earlier, TabularPandas is a powerful and easy preprocessing tool for tabular data. Integration with libraries such as Random Forests and XGBoost requires only one extra step, that the .dataloaders call did for us. Let’s look at our to again. It’s values are stored in a DataFrame like object, where we can extract the cats, conts, xs and ys if we want to:

  1. to.xs[:3]
workclasseducationmarital-statusoccupationrelationshipraceeducation-num_naagefnlwgteducation-num
25387516351510.471582-1.467756-0.030907
1687211651451-1.215622-0.649792-0.030907
25852516351511.865358-0.218915-0.030907

Now that everything is encoded, you can then send this off to XGBoost or Random Forests by extracting the train and validation sets and their values:

  1. X_train, y_train = to.train.xs, to.train.ys.values.ravel()
  2. X_test, y_test = to.valid.xs, to.valid.ys.values.ravel()

And now we can directly send this in!


Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Mar 31, 2021