<jupyter_start><jupyter_text>Deploying Machine Learning ModelsThe aim of this project is to use machine learning to predict if an adult's income exceeds $50,000 per year based on census data. We will deploy this model with Django.<jupyter_code>import numpy as np
import pandas as pd 
import json
import joblib

from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import ExtraTreesClassifier<jupyter_output><empty_output><jupyter_text>DataThe dataset we will be using for this project is the [Adult Income Dataset](https://archive.ics.uci.edu/ml/datasets/adult). This dataset will be used to predict income with machine learning.<jupyter_code>#import the dataset
df = pd.read_csv('data.csv', skipinitialspace=True)
x_columns = [c for c in df.columns if c != 'income']
# input matrix and the column we want to target
X = df[x_columns]
y = df['income']

print("Input matrix X rows: ",X.shape[0],"\nInput matrix X columns: ", X.shape[1])
print("\nTarget column y rows: ", y.shape[0])
df.head(5)<jupyter_output>Input matrix X rows:  32561 
Input matrix X columns:  14

Target column y rows:  32561<jupyter_text>As we can see from the output, the X matrix has 32,561 rows and 14 columns. Each for descripes an individual as input data for algorithm. The target column y has 32,561 values, which indicates if income is over 50,000 per year. Next, we will need to split our data into training and testing categories. We will use 30% of the data for testing in this project and then begin data preprocessing.<jupyter_code>#Split data between train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1234)<jupyter_output><empty_output><jupyter_text>Preprocessing DataBefore we can implement our machine learning algorithm, we need to fill in any missing values and column labels. To fill in these columns, we can use imputation. We fill the value by using the most frequent value in that column, other methods might include filling the value with the mean, but we will simply use the most frequent value.<jupyter_code># fill in missing values using mode
train_mode = dict(X_train.mode().iloc[0]) # select most frequent
X_train = X_train.fillna(train_mode) # fill with the mode

print("Values that will fill NA: \n", train_mode)<jupyter_output>Values that will fill NA: 
 {'age': 31.0, 'workclass': 'Private', 'fnlwgt': 121124, 'education': 'HS-grad', 'education-num': 9.0, 'marital-status': 'Married-civ-spouse', 'occupation': 'Prof-specialty', 'relationship': 'Husband', 'race': 'White', 'sex': 'Male', 'capital-gain': 0.0, 'capital-loss': 0.0, 'hours-per-week': 40.0, 'native-country': 'United-States'}<jupyter_text>Next, we will use [LabelEncoder from sklearn](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html), to encode our labels and finish with data preprocessing.<jupyter_code># use LabelEncoder to convert categoricals
encoders = {}
cols = ['workclass', 'education', 'marital-status',
                'occupation', 'relationship', 'race',
                'sex','native-country']
for col in cols:
    categorical_convert = LabelEncoder()
    X_train[col] = categorical_convert.fit_transform(X_train[col])
    encoders[col] = categorical_convert<jupyter_output><empty_output><jupyter_text>Training AlgorithmWe will be using the Random Forest algorithm from sklearn as well as the Extra Trees algorithm. These classifiers will be utilized to make our predictions.<jupyter_code># train Random Forest
rf = RandomForestClassifier(n_estimators = 100)
rf = rf.fit(X_train, y_train)
# train Extra Trees
et = ExtraTreesClassifier(n_estimators=100)
et = et.fit(X_train, y_train)
# save algorithms and preprocessing
joblib.dump(train_mode, "./train_mode.joblib", compress=True)
joblib.dump(encoders, "./encoders.joblib", compress=True)
joblib.dump(rf, "./random_forest.joblib", compress=True)
joblib.dump(et, "./extra_trees.joblib", compress=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10061349693251534, 'fraction_numerical': 0.01742331288343558, 'mean_word_length': 5.638436482084691, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '604272', 'license': 'MIT', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1228, 'n_words': 441, 'path': 'research/ML.ipynb', 'repo_name': 'pratsingh/ML_Django_Project', 'size': '4075'}