# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: tf
#     language: python
#     name: tf
# ---

# +
imports_path = ".\\imports.ipynb"
TabGAN_path = ".\\TabGAN.ipynb"
utils_path = ".\\utils.ipynb"

# %run "$imports_path"

# +
# %run "$utils_path"
# %run "$TabGAN_path"

image_dir = ".\\Images"
model_dir = ".\\Model\\TabGAN_ckpt\\compare_pred_on_synthetic_adult\\"
dataset_dir = ".\\Datasets\\"
dataset_train_path = os.path.join(dataset_dir, "df_adult_edited_train.csv")
dataset_test_path = os.path.join(dataset_dir, "df_adult_edited_test.csv")
dataset_gen_dir = os.path.join(dataset_dir, "Generated_for_pred_eval_adult//")

data_train = pd.read_csv(dataset_train_path)
data_test = pd.read_csv(dataset_test_path)
discrete_columns = data_train.columns[data_train.dtypes == "object"]
response_col="income"
categories="auto"

# +
X_train = data_train.iloc[:,data_train.columns != response_col]
Y_train = data_train[response_col]
X_test = data_test.iloc[:,data_train.columns != response_col]
Y_test = data_test[response_col]


columns_discrete_bool = [b in ["object", "category"] for b in X_train.dtypes]
discrete_columns_of_X = X_train.columns[columns_discrete_bool]
numeric_columns_of_X = X_train.columns[np.logical_not(columns_discrete_bool)]

X_train[discrete_columns_of_X].astype("category")
X_test[discrete_columns_of_X].astype("category")

label_encoder = LabelEncoder()
Y_train = label_encoder.fit_transform(Y_train)
Y_test = label_encoder.transform(Y_test)

#categories = OneHotEncoder().fit(X_train[discrete_columns_of_X]).categories_
oh_encoder = OneHotEncoder(categories = categories, sparse = False)
X_train_discr = oh_encoder.fit_transform(X_train[discrete_columns_of_X])
X_train = np.concatenate((X_train[numeric_columns_of_X].to_numpy(),
                          oh_encoder.fit_transform(X_train[discrete_columns_of_X])),
                         axis = 1)
X_test = np.concatenate((X_test[numeric_columns_of_X].to_numpy(),
                          oh_encoder.transform(X_test[discrete_columns_of_X])),
                         axis = 1)
X_train
# -

X_train_small, X_validation, Y_train_small,  Y_validation = train_test_split(X_train, Y_train)

np.log(0.5)

# +
# import packages for hyperparameters tuning
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe


space={"eta": hp.uniform("eta", 0, 0.5),
       'max_depth': hp.quniform("max_depth", 3, 18, 1),
        'gamma': hp.uniform ('gamma', 0,9),
        'reg_alpha' : hp.quniform('reg_alpha', 40,180,1),
        'reg_lambda' : hp.uniform('reg_lambda', 0,1),
        'colsample_bytree' : hp.uniform('colsample_bytree', 0.5,1),
        'min_child_weight' : hp.quniform('min_child_weight', 0, 10, 1),
       "max_delta_step" : hp.uniform("max_delta_step", 0, 10),
       "subsample" : hp.uniform("subsample", 0.5, 1),
        'n_estimators': hp.quniform("n_estimators", 100, 500, 1),
        'seed': 0
    }
space2 = {
    "eta": hp.uniform("eta", 0, 0.5),
}


# +
def objective(space):
    clf= XGBClassifier(
        eta = space["eta"], 
        n_estimators = int(space['n_estimators']), max_depth = int(space['max_depth']), gamma = space['gamma'],
        reg_alpha = int(space['reg_alpha']), reg_lambda =int(space['reg_lambda']), 
        min_child_weight=int(space['min_child_weight']), colsample_bytree=int(space['colsample_bytree']),
        subsample=space['subsample'])
    
    evaluation = [( X_train_small, Y_train_small), ( X_validation, Y_validation)]
    
    clf.fit(X_train_small, Y_train_small,
            eval_set=evaluation, eval_metric="auc",
            early_stopping_rounds=10,verbose=False)
    

    pred = clf.predict(X_validation)
    accuracy = accuracy_score(Y_validation, pred>0.5)
    return {'loss': -accuracy, 'status': STATUS_OK }

def objective2(space):
    clf= XGBClassifier(eta = int(space["eta"]))
    
    evaluation = [( X_train_small, Y_train_small), ( X_validation, Y_validation)]
    
    clf.fit(X_train_small, Y_train_small,
            eval_set=evaluation, eval_metric="auc",
            early_stopping_rounds=10,verbose=False)
    

    pred = clf.predict(X_validation)
    accuracy = accuracy_score(Y_validation, pred>0.5)
    return {'loss': -accuracy, 'status': STATUS_OK }

# +
trials = Trials()

best_hyperparams = fmin(fn = objective2,
                        space = space2,
                        algo = tpe.suggest,
                        max_evals = 100,
                        trials = trials)
# -

print("The best hyperparameters are : ","\n")
print(best_hyperparams)

best_hyperparams

clf_best= XGBClassifier(
                    n_estimators = int(best_hyperparams['n_estimators']), max_depth = int(best_hyperparams['max_depth']), gamma = best_hyperparams['gamma'],
                    reg_alpha = int(best_hyperparams['reg_alpha']),min_child_weight=int(best_hyperparams['min_child_weight']),
                    colsample_bytree=int(best_hyperparams['colsample_bytree']))

clf_best= XGBClassifier(
                    eta=best_hyperparams['eta'])

clf_best.fit(X_train, Y_train)

pred = clf_best.predict(X_test)
accuracy = accuracy_score(Y_test, pred>0.5)
auc = roc_auc_score(Y_test, clf_best.predict_proba(X_test)[:,1])
print("Accurcay:", accuracy)
print("AUC:", auc)

# +
clf = XGBClassifier(
        tree_method="gpu_hist", enable_categorical=False, use_label_encoder=False, eval_metric = "logloss"
    )
# X is the dataframe we created in previous snippet
clf.fit(X_train, Y_train)
# Must use JSON for serialization, otherwise the information is lost
clf.save_model("categorical-model.json")

accuracy = accuracy_score(Y_test, clf.predict(X_test))
auc = roc_auc_score(Y_test, clf.predict_proba(X_test)[:,1])
print("Accurcay:", accuracy)
print("AUC:", auc)

StarcoderdataJupyterScriptsDedupFiltered