<jupyter_start><jupyter_text>SetupI will do the necessary imports and loading of the data in this section.<jupyter_code>import json # will be needed for saving preprocessing details
import numpy as np # for data manipulation
import pandas as pd # for data manipulation
from sklearn.model_selection import train_test_split # will be used for data split
from sklearn.preprocessing import LabelEncoder # for preprocessing
from sklearn.ensemble import RandomForestClassifier # for training the algorithm
from sklearn.ensemble import ExtraTreesClassifier # for training the algorithm
import joblib # for saving algorithm and preprocessing objects<jupyter_output><empty_output><jupyter_text>Using the Adult Income dataset. The ML will be used to predict whether income exceeds $50k.year based on census data.<jupyter_code>df = pd.read_csv('https://raw.githubusercontent.com/pplonski/datasets-for-start/master/adult/data.csv', skipinitialspace=True)
x_cols = [c for c in df.columns if c!= 'income']
#set input matrix and target column
X = df[x_cols]
y = df['income']

df.head()
#split train/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>Data pre-processingThis section will consist of dealing with missing values and categorical columns.I will be using the **Random Forest** algorithm for this project.<jupyter_code>#fill missing values
train_mode = dict(X_train.mode().iloc[0])
X_train = X_train.fillna(train_mode)
print(train_mode)
#convert categoricals
#LabelEncoder will convert string values into numbers for these columns, so that the model can work correctly
encoders = {}
for column in ['workclass', 'education', 'marital-status',
                'occupation', 'relationship', 'race',
                'sex','native-country']:
    categorical_convert = LabelEncoder()
    X_train[column] = catergorical_convert.fit_transform(X_train[column])
    encoders[column] = categorical_convert<jupyter_output><empty_output><jupyter_text>Algorithms training<jupyter_code>#train Random Forest algorithm
rf = RandomForestClassifier()
rf = rf.fit(X_train, y_train)
#train Extra Trees algorithm
et = ExtraTreesClassifier(n_estimators=100)
et = et.fit(X_train, y_train)
#save the algorithm and pre-processing variables using the joblib package
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.0936532507739938, 'fraction_numerical': 0.0046439628482972135, 'mean_word_length': 6.30225988700565, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '613985', 'license': 'MIT', 'n_tokens_mistral': 798, 'n_tokens_neox': 773, 'n_words': 232, 'path': 'research/My ML.ipynb', 'repo_name': 'lewiskirby/my_ml_service', 'size': '2584'}