<jupyter_start><jupyter_text>Tabular models<jupyter_code>from fastai.tabular import *<jupyter_output><empty_output><jupyter_text>Untar the data from `URLs.ADULT_SAMPLE` and save the result to `path`. Read the data from `path/'adult.csv'` to the variable `df`.<jupyter_code>path = untar_data(URLs.ADULT_SAMPLE)
df = pd.read_csv(path/'adult.csv')<jupyter_output><empty_output><jupyter_text>Set the `dep_var` to 'salary'. Set the `cat_names` to:- 'workclass'- 'education'- 'marital-status'- 'occupation'- 'relationship'- 'race'Set `cont_names` to:- 'age'- 'fnlwgt'- 'education-num'Set `procs` to:- FillMissing- Categorify- Normalize<jupyter_code>dep_var = 'salary'
cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race']
cont_names = ['age', 'fnlwgt', 'education-num']
procs = [FillMissing, Categorify, Normalize]<jupyter_output><empty_output><jupyter_text>Create a `TabularList` from `df` at indices 800-1000 as the test set. Assign it to variable `test`.<jupyter_code>test = TabularList.from_df(df.iloc[800:1000].copy(), path=path, cat_names=cat_names, cont_names=cont_names)<jupyter_output><empty_output><jupyter_text>Create a `TabularList.from_df` using the stuff you've created so far, split by index, label from the `dep_var` column, add the test set you created, and turn it all into a data bunch.<jupyter_code>data = (TabularList.from_df(df, path=path, cat_names=cat_names, cont_names=cont_names, procs=procs)
                           .split_by_idx(list(range(800,1000)))
                           .label_from_df(cols=dep_var)
                           .add_test(test)
                           .databunch())<jupyter_output><empty_output><jupyter_text>Show a batch.<jupyter_code>data.show_batch(rows=10)<jupyter_output><empty_output><jupyter_text>Create a learner using `tabular_learner` with layers [200, 100] and using the `accuracy` metric.<jupyter_code>learn = tabular_learner(data, layers=[200,100], metrics=accuracy)<jupyter_output><empty_output><jupyter_text>Fit the learner with one epoch, lr=1e-2.<jupyter_code>learn.fit(1, 1e-2)<jupyter_output><empty_output><jupyter_text>Inference Get a single row.<jupyter_code>row = df.iloc[0]<jupyter_output><empty_output><jupyter_text>Call learn.predict on it.<jupyter_code>learn.predict(row)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1332470892626132, 'fraction_numerical': 0.017680034497628287, 'mean_word_length': 6.03030303030303, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '30356', 'license': 'Apache-2.0', 'n_tokens_mistral': 881, 'n_tokens_neox': 831, 'n_words': 160, 'path': 'nbs/dl1/lesson4-tabular-answerkey.ipynb', 'repo_name': 'rforgione/course-v3', 'size': '2319'}