<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import gc
gc.collect()
# Load FastAI
from fastai import *
from fastai.tabular import *
# train test split
from sklearn.model_selection import train_test_split
# Load data from google drive
from google.colab import drive

drive.mount('/content/gdrive')
data_repo_url = '/content/gdrive/My Drive/Datasets/'
data_url = data_repo_url + 'Census Income/census.csv'
# Load Data
df = pd.read_csv(data_url)


# Rename target
df['high_income'] = df['income'] == '>50K'

df.head()
df.high_income.value_counts()/df.shape[0]
dep_var = 'high_income'
cat_names = ['workclass', 'education_level', 'marital-status',
              'occupation', 'relationship', 'race', 'sex', 
             'native-country']
cont_names = ['age', 'capital-gain', 'capital-loss', 'hours-per-week']
print("Categorical columns are : ", cat_names)
print('Continuous numerical columns are :', cont_names)
procs = [FillMissing, Categorify, Normalize]
df_train, df_test = train_test_split(df, test_size=0.3, random_state=42)
df_train.shape, df_test.shape
# Transformations
procs = [FillMissing, Categorify, Normalize]

# Test Tabular List
test = TabularList.from_df(df_test, cat_names=cat_names, cont_names=cont_names, procs=procs)

# Train Data Bunch
data = (TabularList.from_df(df_train, path='.', cat_names=cat_names, cont_names=cont_names, procs=procs)
                        .split_by_idx(list(range(0,200)))
                        .label_from_df(cols = dep_var)
                        .add_test(test, label=0)
                        .databunch())

data.show_batch(rows=10)
# Create deep learning model
learn = tabular_learner(data, layers=[15, 100, 200], metrics=accuracy, emb_drop=0.1, callback_fns=ShowGraph)

# select the appropriate learning rate
learn.lr_find()

# we typically find the point where the slope is steepest
learn.recorder.plot()

# Fit the model based on selected learning rate
learn.fit_one_cycle(5, max_lr=1e-3)

# Analyse our model
learn.model
learn.recorder.plot_losses()
# Predict our target value
predictions, *_ = learn.get_preds(DatasetType.Test)
labels = np.argmax(predictions, 1)
labels
# metrics
from sklearn.metrics import classification_report
report = classification_report(df_test['high_income'], labels)
print(report)<jupyter_output>precision    recall  f1-score   support

       False       0.88      0.93      0.90     10193
        True       0.74      0.60      0.66      3374

    accuracy                           0.85     13567
   macro avg       0.81      0.76      0.78     13567
weighted avg       0.84      0.85      0.84     13567
{'fraction_non_alphanumeric': 0.1070096942580164, 'fraction_numerical': 0.0343027591349739, 'mean_word_length': 3.4791318864774623, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '504176', 'license': 'Apache-2.0', 'n_tokens_mistral': 949, 'n_tokens_neox': 886, 'n_words': 218, 'path': 'TabularClassificationFastAI.ipynb', 'repo_name': 'regsevillasibal/ftw-webapp-deployment', 'size': '2682'}