|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Tabular models" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "from fastai.tabular import *" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "Tabular data should be in a Pandas `DataFrame`." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "path = untar_data(URLs.ADULT_SAMPLE)\n", |
| 33 | + "df = pd.read_csv(path/'adult.csv')" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "dep_var = 'salary'\n", |
| 43 | + "cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race']\n", |
| 44 | + "cont_names = ['age', 'fnlwgt', 'education-num']\n", |
| 45 | + "procs = [FillMissing, Categorify, Normalize]" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "test = TabularList.from_df(df.iloc[800:1000].copy(), path=path, cat_names=cat_names, cont_names=cont_names)" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "data = (TabularList.from_df(df, path=path, cat_names=cat_names, cont_names=cont_names, procs=procs)\n", |
| 64 | + " .split_by_idx(list(range(800,1000)))\n", |
| 65 | + " .label_from_df(cols=dep_var)\n", |
| 66 | + " .add_test(test)\n", |
| 67 | + " .databunch())" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": null, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/html": [ |
| 78 | + "<table> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <col width='10%'> <tr>\n", |
| 79 | + " <th>workclass</th>\n", |
| 80 | + " <th>education</th>\n", |
| 81 | + " <th>marital-status</th>\n", |
| 82 | + " <th>occupation</th>\n", |
| 83 | + " <th>relationship</th>\n", |
| 84 | + " <th>race</th>\n", |
| 85 | + " <th>education-num_na</th>\n", |
| 86 | + " <th>age</th>\n", |
| 87 | + " <th>fnlwgt</th>\n", |
| 88 | + " <th>education-num</th>\n", |
| 89 | + " <th>target</th>\n", |
| 90 | + " </tr>\n", |
| 91 | + " <tr>\n", |
| 92 | + " <th> Private</th>\n", |
| 93 | + " <th> HS-grad</th>\n", |
| 94 | + " <th> Never-married</th>\n", |
| 95 | + " <th> Sales</th>\n", |
| 96 | + " <th> Not-in-family</th>\n", |
| 97 | + " <th> White</th>\n", |
| 98 | + " <th>False</th>\n", |
| 99 | + " <th>-1.2158</th>\n", |
| 100 | + " <th>1.1004</th>\n", |
| 101 | + " <th>-0.4224</th>\n", |
| 102 | + " <th><50k</th>\n", |
| 103 | + " </tr>\n", |
| 104 | + " <tr>\n", |
| 105 | + " <th> ?</th>\n", |
| 106 | + " <th> HS-grad</th>\n", |
| 107 | + " <th> Widowed</th>\n", |
| 108 | + " <th> ?</th>\n", |
| 109 | + " <th> Not-in-family</th>\n", |
| 110 | + " <th> White</th>\n", |
| 111 | + " <th>False</th>\n", |
| 112 | + " <th>1.8627</th>\n", |
| 113 | + " <th>0.0976</th>\n", |
| 114 | + " <th>-0.4224</th>\n", |
| 115 | + " <th><50k</th>\n", |
| 116 | + " </tr>\n", |
| 117 | + " <tr>\n", |
| 118 | + " <th> Self-emp-not-inc</th>\n", |
| 119 | + " <th> HS-grad</th>\n", |
| 120 | + " <th> Never-married</th>\n", |
| 121 | + " <th> Craft-repair</th>\n", |
| 122 | + " <th> Own-child</th>\n", |
| 123 | + " <th> Black</th>\n", |
| 124 | + " <th>False</th>\n", |
| 125 | + " <th>0.0303</th>\n", |
| 126 | + " <th>0.2092</th>\n", |
| 127 | + " <th>-0.4224</th>\n", |
| 128 | + " <th><50k</th>\n", |
| 129 | + " </tr>\n", |
| 130 | + " <tr>\n", |
| 131 | + " <th> Private</th>\n", |
| 132 | + " <th> HS-grad</th>\n", |
| 133 | + " <th> Married-civ-spouse</th>\n", |
| 134 | + " <th> Protective-serv</th>\n", |
| 135 | + " <th> Husband</th>\n", |
| 136 | + " <th> White</th>\n", |
| 137 | + " <th>False</th>\n", |
| 138 | + " <th>1.5695</th>\n", |
| 139 | + " <th>-0.5938</th>\n", |
| 140 | + " <th>-0.4224</th>\n", |
| 141 | + " <th><50k</th>\n", |
| 142 | + " </tr>\n", |
| 143 | + " <tr>\n", |
| 144 | + " <th> Private</th>\n", |
| 145 | + " <th> HS-grad</th>\n", |
| 146 | + " <th> Married-civ-spouse</th>\n", |
| 147 | + " <th> Handlers-cleaners</th>\n", |
| 148 | + " <th> Husband</th>\n", |
| 149 | + " <th> White</th>\n", |
| 150 | + " <th>False</th>\n", |
| 151 | + " <th>-0.9959</th>\n", |
| 152 | + " <th>-0.0318</th>\n", |
| 153 | + " <th>-0.4224</th>\n", |
| 154 | + " <th><50k</th>\n", |
| 155 | + " </tr>\n", |
| 156 | + " <tr>\n", |
| 157 | + " <th> Private</th>\n", |
| 158 | + " <th> 10th</th>\n", |
| 159 | + " <th> Married-civ-spouse</th>\n", |
| 160 | + " <th> Farming-fishing</th>\n", |
| 161 | + " <th> Wife</th>\n", |
| 162 | + " <th> White</th>\n", |
| 163 | + " <th>False</th>\n", |
| 164 | + " <th>-0.7027</th>\n", |
| 165 | + " <th>0.6071</th>\n", |
| 166 | + " <th>-1.5958</th>\n", |
| 167 | + " <th><50k</th>\n", |
| 168 | + " </tr>\n", |
| 169 | + " <tr>\n", |
| 170 | + " <th> Private</th>\n", |
| 171 | + " <th> HS-grad</th>\n", |
| 172 | + " <th> Married-civ-spouse</th>\n", |
| 173 | + " <th> Machine-op-inspct</th>\n", |
| 174 | + " <th> Husband</th>\n", |
| 175 | + " <th> White</th>\n", |
| 176 | + " <th>False</th>\n", |
| 177 | + " <th>0.1036</th>\n", |
| 178 | + " <th>-0.0968</th>\n", |
| 179 | + " <th>-0.4224</th>\n", |
| 180 | + " <th><50k</th>\n", |
| 181 | + " </tr>\n", |
| 182 | + " <tr>\n", |
| 183 | + " <th> Private</th>\n", |
| 184 | + " <th> Some-college</th>\n", |
| 185 | + " <th> Married-civ-spouse</th>\n", |
| 186 | + " <th> Exec-managerial</th>\n", |
| 187 | + " <th> Own-child</th>\n", |
| 188 | + " <th> White</th>\n", |
| 189 | + " <th>False</th>\n", |
| 190 | + " <th>-0.7760</th>\n", |
| 191 | + " <th>-0.6653</th>\n", |
| 192 | + " <th>-0.0312</th>\n", |
| 193 | + " <th>>=50k</th>\n", |
| 194 | + " </tr>\n", |
| 195 | + " <tr>\n", |
| 196 | + " <th> State-gov</th>\n", |
| 197 | + " <th> Some-college</th>\n", |
| 198 | + " <th> Never-married</th>\n", |
| 199 | + " <th> Tech-support</th>\n", |
| 200 | + " <th> Own-child</th>\n", |
| 201 | + " <th> White</th>\n", |
| 202 | + " <th>False</th>\n", |
| 203 | + " <th>-0.8493</th>\n", |
| 204 | + " <th>-1.4959</th>\n", |
| 205 | + " <th>-0.0312</th>\n", |
| 206 | + " <th><50k</th>\n", |
| 207 | + " </tr>\n", |
| 208 | + " <tr>\n", |
| 209 | + " <th> Private</th>\n", |
| 210 | + " <th> 11th</th>\n", |
| 211 | + " <th> Never-married</th>\n", |
| 212 | + " <th> Machine-op-inspct</th>\n", |
| 213 | + " <th> Not-in-family</th>\n", |
| 214 | + " <th> White</th>\n", |
| 215 | + " <th>False</th>\n", |
| 216 | + " <th>-1.0692</th>\n", |
| 217 | + " <th>-0.9516</th>\n", |
| 218 | + " <th>-1.2046</th>\n", |
| 219 | + " <th><50k</th>\n", |
| 220 | + " </tr>\n", |
| 221 | + "</table>\n" |
| 222 | + ], |
| 223 | + "text/plain": [ |
| 224 | + "<IPython.core.display.HTML object>" |
| 225 | + ] |
| 226 | + }, |
| 227 | + "metadata": {}, |
| 228 | + "output_type": "display_data" |
| 229 | + } |
| 230 | + ], |
| 231 | + "source": [ |
| 232 | + "data.show_batch(rows=10)" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "metadata": {}, |
| 239 | + "outputs": [], |
| 240 | + "source": [ |
| 241 | + "learn = tabular_learner(data, layers=[200,100], metrics=accuracy)" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "code", |
| 246 | + "execution_count": null, |
| 247 | + "metadata": {}, |
| 248 | + "outputs": [ |
| 249 | + { |
| 250 | + "data": { |
| 251 | + "text/html": [ |
| 252 | + "Total time: 00:03 <p><table style='width:300px; margin-bottom:10px'>\n", |
| 253 | + " <tr>\n", |
| 254 | + " <th>epoch</th>\n", |
| 255 | + " <th>train_loss</th>\n", |
| 256 | + " <th>valid_loss</th>\n", |
| 257 | + " <th>accuracy</th>\n", |
| 258 | + " </tr>\n", |
| 259 | + " <tr>\n", |
| 260 | + " <th>1</th>\n", |
| 261 | + " <th>0.354604</th>\n", |
| 262 | + " <th>0.378520</th>\n", |
| 263 | + " <th>0.820000</th>\n", |
| 264 | + " </tr>\n", |
| 265 | + "</table>\n" |
| 266 | + ], |
| 267 | + "text/plain": [ |
| 268 | + "<IPython.core.display.HTML object>" |
| 269 | + ] |
| 270 | + }, |
| 271 | + "metadata": {}, |
| 272 | + "output_type": "display_data" |
| 273 | + } |
| 274 | + ], |
| 275 | + "source": [ |
| 276 | + "learn.fit(1, 1e-2)" |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "markdown", |
| 281 | + "metadata": {}, |
| 282 | + "source": [ |
| 283 | + "## Inference" |
| 284 | + ] |
| 285 | + }, |
| 286 | + { |
| 287 | + "cell_type": "code", |
| 288 | + "execution_count": null, |
| 289 | + "metadata": {}, |
| 290 | + "outputs": [], |
| 291 | + "source": [ |
| 292 | + "row = df.iloc[0]" |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "code", |
| 297 | + "execution_count": null, |
| 298 | + "metadata": {}, |
| 299 | + "outputs": [ |
| 300 | + { |
| 301 | + "data": { |
| 302 | + "text/plain": [ |
| 303 | + "(Category >=50k, tensor(1), tensor([0.4402, 0.5598]))" |
| 304 | + ] |
| 305 | + }, |
| 306 | + "execution_count": null, |
| 307 | + "metadata": {}, |
| 308 | + "output_type": "execute_result" |
| 309 | + } |
| 310 | + ], |
| 311 | + "source": [ |
| 312 | + "learn.predict(row)" |
| 313 | + ] |
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "code", |
| 317 | + "execution_count": null, |
| 318 | + "metadata": {}, |
| 319 | + "outputs": [], |
| 320 | + "source": [] |
| 321 | + } |
| 322 | + ], |
| 323 | + "metadata": { |
| 324 | + "kernelspec": { |
| 325 | + "display_name": "Python 3", |
| 326 | + "language": "python", |
| 327 | + "name": "python3" |
| 328 | + } |
| 329 | + }, |
| 330 | + "nbformat": 4, |
| 331 | + "nbformat_minor": 2 |
| 332 | +} |
0 commit comments