<jupyter_start><jupyter_text>Load Packages<jupyter_code># Primary Packages
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# Modelling
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

# Package to Save Model
import joblib

# Metrics
from sklearn.metrics import classification_report
# Pandas options
pd.options.display.max_columns = 999<jupyter_output><empty_output><jupyter_text>Load Data<jupyter_code>data = pd.read_csv('data/census.csv')
data.head()
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']<jupyter_output><empty_output><jupyter_text>Feature Engineering<jupyter_code># Create Boolean Target
data['high_income'] = data['income'] == '>50K'

# Multihot encode categorical variables
df_cat = pd.get_dummies(data[cat_names].astype(str))

# Reassign numerical to diff df
df_cont = data[cont_names]

# Normalize numerical features
df_cont_norm = (df_cont-df_cont.min())/(df_cont.max()-df_cont.min())

# Concatenate features
X = pd.concat([df_cat, df_cont_norm], axis=1)

# Create target df
y = data[dep_var]
X.shape, y.shape
X.columns
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.4, random_state = 42)

model = RandomForestClassifier(n_estimators=300, random_state=42)
# model = GradientBoostingRegressor(n_estimators=300, random_state=42, )

# Fit Model
%time model.fit(X_train, y_train)
y_pred = model.predict(X_test)
report = classification_report(y_pred, y_test)
print(report)<jupyter_output>precision    recall  f1-score   support

       False       0.91      0.88      0.90     14105
        True       0.63      0.70      0.67      3984

    accuracy                           0.84     18089
   macro avg       0.77      0.79      0.78     18089
weighted avg       0.85      0.84      0.85     18089<jupyter_text>Create A Simpler ModelFor our WebApp, let's just use a few simple features like:- Age- Hours per Week- Education Level- Sex- Race<jupyter_code># Get features we want and create a new dataframe
columns = ['age', 'hours-per-week', 'education_level', 'sex', 'race']
data_small = data[columns]
# Onehot encode categorical features
data_small_dummies = pd.get_dummies(data_small)
data_small_dummies.head()<jupyter_output><empty_output><jupyter_text>Generate Model<jupyter_code># Assign X and y
X_small, y_small = data_small_dummies, y
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X_small, y_small, test_size = 0.4, random_state = 42)

model = RandomForestClassifier(n_estimators=10, random_state=42)
# model = GradientBoostingRegressor(n_estimators=300, random_state=42, )

# Fit Model
%time model.fit(X_train, y_train)
# Get Test Predictions
y_pred = model.predict(X_test)

# Get Metrics
report = classification_report(y_pred, y_test)
print(report)<jupyter_output>precision    recall  f1-score   support

       False       0.89      0.83      0.86     14636
        True       0.44      0.57      0.49      3453

    accuracy                           0.78     18089
   macro avg       0.66      0.70      0.68     18089
weighted avg       0.80      0.78      0.79     18089<jupyter_text>Save Model<jupyter_code># Save Model
joblib.dump(model, 'model/census_model.pkl')<jupyter_output><empty_output><jupyter_text>Generate Predictions Using Sample Inputs and Saved Model  Education Levels<jupyter_code>education_level_values.values.tolist()
education_level_values = pd.Series(data['education_level'].unique()).str.strip()
education_level_dummies = pd.get_dummies(education_level_values)
education_level_dummies<jupyter_output><empty_output><jupyter_text>Race<jupyter_code>race_values = pd.Series(data['race'].unique()).str.strip()
race_dummies = pd.get_dummies(race_values)
race_dummies<jupyter_output><empty_output><jupyter_text>Sex<jupyter_code>sex_values = pd.Series(data['sex'].unique()).str.strip()
sex_dummies = pd.get_dummies(sex_values)
sex_dummies
data_small_dummies.columns<jupyter_output><empty_output><jupyter_text>Load Model<jupyter_code>model = joblib.load('model/census_model.pkl')
education_level_values
np.where(education_level_values == education_level_sample)
race_values
# Age
age = 21

# Hours per Pweek
hours = 80

# Education Level
education_level_sample = 'HS-grad'
education_level_sample_dummies = (education_level_dummies.loc[np.where(education_level_values.values == education_level_sample)[0]]
                                  .values.tolist()[0])

# Race
race_sample = 'White'
race_sample_dummies = race_dummies.loc[np.where(race_values.values == race_sample)[0]].values.tolist()[0]

# Gender/Sex
sex_sample = 'Male'
sex_sample_dummies = sex_dummies.loc[np.where(sex_values.values == sex_sample)[0]].values.tolist()[0]
# Concatenate features for sample prediction
sample_features = [age, hours] + education_level_sample_dummies + sex_sample_dummies + race_sample_dummies
len(sample_features)
# Sample Predictions
prediction = model.predict([sample_features])[0]
prediction<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10598813851157451, 'fraction_numerical': 0.032906064664243354, 'mean_word_length': 4.7261774370208105, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '492597', 'license': 'Apache-2.0', 'n_tokens_mistral': 1963, 'n_tokens_neox': 1827, 'n_words': 354, 'path': 'model_generator.ipynb', 'repo_name': 'regsevillasibal/ftw-webapp-deployment', 'size': '5227'}