<jupyter_start><jupyter_text>Model Training<jupyter_code># Split Features and target data
X, y = dataX.iloc[:, :-1], dataX.iloc[:, -1]
print(X.shape, y.shape)
# penalty = 'l2'
# C = 1.0
# class_weight = 'balanced'
# random_state = 2018
# solver = 'liblinear'
logReg = LogisticRegression(n_jobs=-1, class_weight='balanced')
# take recall as metric as we want to get all of the fraud cases possible
scorer = make_scorer(recall_score)
scores = cross_val_score(logReg, X, y, scoring=scorer, cv=k_fold, n_jobs=-1)
logger.info('Train LogisticRegression:')
logger.info(f'CV recall: {np.mean(scores): .3f} +/- {np.std(scores): .3f}')
logger.info('Done!')
# n_estimators = 10
# max_features = 'auto'
# max_depth = None
# min_samples_split = 2
# min_samples_leaf = 1
# min_weight_fraction_leaf = 0.0
# max_leaf_nodes = None
# bootstrap = True
# oob_score = False
# n_jobs = -1
# random_state = 2018
# class_weight = 'balanced'

RFC = RandomForestClassifier(n_jobs=-1, class_weight='balanced')

scores = cross_val_score(RFC, X, y, scoring=scorer, cv=k_fold, n_jobs=-1)
logger.info('Train RandomForest:')
logger.info(f'CV recall: {np.mean(scores): .3f} +/- {np.std(scores): .3f}')
logger.info('Done!')
from lightgbm import LGBMClassifier

lgbm = LGBMClassifier(n_jobs=-1, scale_pos_weight=578)

scores = cross_val_score(lgbm, X, y, cv=k_fold, scoring=scorer, n_jobs=-1)
logger.info('Train LGBM:')
logger.info(f'CV recall: {np.mean(scores): .3f} +/- {np.std(scores): .3f}')
logger.info('Done!')
from xgboost import XGBClassifier

xgbm = XGBClassifier(n_jobs=-1, scale_pos_weight=578)
scores = cross_val_score(xgbm, X, y, cv=k_fold, scoring=scorer, n_jobs=-1)
logger.info('Train XGBM:')
logger.info(f'CV recall: {np.mean(scores): .3f} +/- {np.std(scores): .3f}')
logger.info('Done!')<jupyter_output>[I 201108 18:31:42 <ipython-input-16-ef9700ec8b02>:5] Train XGBM:
[I 201108 18:31:42 <ipython-input-16-ef9700ec8b02>:6] CV recall:  0.814 +/-  0.022
[I 201108 18:31:42 <ipython-input-16-ef9700ec8b02>:7] Done!<jupyter_text>Tuning<jupyter_code>from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
# Logistic Regression Tuning

param_grid = {
    'penalty': ['l1', 'l2'],
    'C': np.logspace(-4, 4, 20),
    'class_weight': ['balanced']
}

LRgs = GridSearchCV(logReg, param_grid=param_grid, cv=k_fold, n_jobs=-1, scoring=scorer, refit=True)
best_lr = gs.fit(X, y)
# rs = RandomizedSearchCV(logReg, param_distributions=param_grid, cv=k_fold, n_jobs=-1, scoring=scorer)
# rs.fit(X, y)
# LightGbm Tuning
rs.best_params_
gs.best_params_
gs.best_score_
rs.best_score_
from scipy.stats import randint as sp_randint
from scipy.stats import uniform as sp_uniform
param_test ={'num_leaves': sp_randint(6, 50), 
             'min_child_samples': sp_randint(100, 500), 
             'min_child_weight': [1e-5, 1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3, 1e4],
             'subsample': sp_uniform(loc=0.2, scale=0.8), 
             'colsample_bytree': sp_uniform(loc=0.4, scale=0.6),
             'reg_alpha': [0, 1e-1, 1, 2, 5, 7, 10, 50, 100],
             'reg_lambda': [0, 1e-1, 1, 5, 10, 20, 50, 100],
             'scale_pos_weight': [100, 200, 300, 400, 500, 600]}


fit_params={"early_stopping_rounds":30, 
            "eval_metric" : 'auc', 
            "eval_set" : [(X_test,y_test)],
            'eval_names': ['valid'],
            #'callbacks': [lgb.reset_parameter(learning_rate=learning_rate_010_decay_power_099)],
            'verbose': 100,
            'categorical_feature': 'auto'}


lgbm_gs = RandomizedSearchCV(
    lgbm, param_distributions=param_test, 
    n_iter=100, scoring=scorer, cv=k_fold, refit=True, n_jobs=-1,

)
lgbm_gs.fit(X, y)
lgbm_gs.best_score_
testX = pd.read_csv(CONFIG.data_path / 'interim' / 'test_data.csv')
testX.shape
X_test, y_test = testX.iloc[:, :-1], testX.iloc[:, -1]
X_test.shape, y_test.shape
gs.score(X_test, y_test)
lgbm_gs.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Evaluating<jupyter_code>from sklearn.metrics import confusion_matrix

y_pred = gs.predict(X_test)
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(confmat)
y_pred = lgbm_gs.predict(X_test)
confmat = confusion_matrix(y_true=y_test, y_pred=y_pred)
print(confmat)
from sklearn.metrics import recall_score, precision_score

print(f'Recall: {recall_score(y_true=y_test, y_pred=y_pred)}')
print(f'Precision:{precision_score(y_true=y_test, y_pred=y_pred)}')
print(f'Recall: {recall_score(y_true=y_test, y_pred=y_pred)}')
print(f'Precision:{precision_score(y_true=y_test, y_pred=y_pred)}')<jupyter_output>Recall: 0.8648648648648649
Precision:0.050117462803445575
{'fraction_non_alphanumeric': 0.15639294271400567, 'fraction_numerical': 0.055979089522979746, 'mean_word_length': 5.256130790190736, 'pattern_counts': {'":': 1, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '123017', 'license': 'FTL', 'n_tokens_mistral': 2091, 'n_tokens_neox': 1866, 'n_words': 344, 'path': 'notebooks/01_modelTrain.ipynb', 'repo_name': 'tung2921/Ml_FraudClf', 'size': '4591'}