<jupyter_start><jupyter_text>Load Dataset<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']

# show first rows data
df.head()
# data 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 preprocessing<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
encoders = {}
columns = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex','native-country']
for column in columns:
    categorical_convert = LabelEncoder()
    X_train[column] = categorical_convert.fit_transform(X_train[column])
    encoders[column] = categorical_convert<jupyter_output><empty_output><jupyter_text>Algorithm training<jupyter_code># train the Random Forest algorithm
rf = RandomForestClassifier(n_estimators = 100)
rf = rf.fit(X_train, y_train)
# train the Extra Trees algorithm
et = ExtraTreesClassifier(n_estimators = 100)
et = et.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Save preprocessing objects and RF algorithm<jupyter_code>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>
StarcoderdataJupyterStructuredCleanDedup