src/models/train_model.py
def grid_search(estimator, param_grid, X_train, y_train, X_test, y_test, batch_size=10000, nn=False):
    """ Performs grid search over parameter grid.

    Function iterates over the combinations of parameters in the parameter grid. Trains the estimator on the X_train.
    Evaluation is done on both the training set and validation set. The parameters reported are ROC AUC score calculated
    on the training and test sets.

    :param estimator: classifier for training
    :param param_grid: dictionary with parameters for the estimator
    :param X_train: training set input
    :param y_train: training set output
    :param X_test: test set input
    :param y_test: test set outpu
    :param batch_size: batch size for the neural network classifier
    :param nn: True if the estimator is a neural network model
    :return: a pandas DataFrame with the parameter combinations and the corresponding model evaluation metrics.
    """
    out = pd.DataFrame()
    for g in ParameterGrid(param_grid):
        estimator.set_params(**g)
        if nn:
            print('Fitting with params:', g)
            # Define early_stopping_monitor
            early_stopping_monitor = EarlyStopping(patience=2)

            # Fit the model
            estimator.fit(
                X_train,
                y_train,
                verbose=True,
                validation_split=0.3,
                epochs=1000,
                batch_size=batch_size,
                callbacks=[early_stopping_monitor])
            y_train_pred_prob_true = estimator.predict_proba(X_train)[:, 1]
            y_test_pred_prob_true = estimator.predict_proba(X_test)[:, 1]
            scores = {
                'train_roc_auc': roc_auc_score(y_train, y_train_pred_prob_true),
                'test_roc_auc': roc_auc_score(y_test, y_test_pred_prob_true)
            }
            print('Scores after fitting:', scores)
        else:
            estimator.fit(X_train, y_train)
            y_train_pred = estimator.predict(X_train)
            y_train_pred_prob = estimator.predict_proba(X_train)[:,1]
            y_test_pred = estimator.predict(X_test)
            y_test_pred_prob = estimator.predict_proba(X_test)[:,1]
            scores = {'train_accuracy': accuracy_score(y_train, y_train_pred),
                     'test_accuracy': accuracy_score(y_test, y_test_pred),
                     'train_roc_auc': roc_auc_score(y_train, y_train_pred_prob),
                     'test_roc_auc': roc_auc_score(y_test, y_test_pred_prob)}
        out_dict = g.copy()
        out_dict.update(scores)
        print(out_dict)
        out = out.append(out_dict, ignore_index=True)
    return out
 Performs grid search over parameter grid.

    Function iterates over the combinations of parameters in the parameter grid. Trains the estimator on the X_train.
    Evaluation is done on both the training set and validation set. The parameters reported are ROC AUC score calculated
    on the training and test sets.

    :param estimator: classifier for training
    :param param_grid: dictionary with parameters for the estimator
    :param X_train: training set input
    :param y_train: training set output
    :param X_test: test set input
    :param y_test: test set outpu
    :param batch_size: batch size for the neural network classifier
    :param nn: True if the estimator is a neural network model
    :return: a pandas DataFrame with the parameter combinations and the corresponding model evaluation metrics.
    
Performs grid search over parameter grid.
Function iterates over the combinations of parameters in the parameter grid. Trains the estimator on the X_train.
Evaluation is done on both the training set and validation set. The parameters reported are ROC AUC score calculated
on the training and test sets.
Performs grid search over parameter grid . Function iterates over the combinations of parameters in the parameter grid . Trains the estimator on the X_train . Evaluation is done on both the training set and validation set . The parameters reported are ROC AUC score calculated on the training and test sets .
def grid_search(estimator, param_grid, X_train, y_train, X_test, y_test, batch_size=10000, nn=False):
    out = pd.DataFrame()
    for g in ParameterGrid(param_grid):
        estimator.set_params(**g)
        if nn:
            print('Fitting with params:', g)
            early_stopping_monitor = EarlyStopping(patience=2)
            estimator.fit(
                X_train,
                y_train,
                verbose=True,
                validation_split=0.3,
                epochs=1000,
                batch_size=batch_size,
                callbacks=[early_stopping_monitor])
            y_train_pred_prob_true = estimator.predict_proba(X_train)[:, 1]
            y_test_pred_prob_true = estimator.predict_proba(X_test)[:, 1]
            scores = {
                'train_roc_auc': roc_auc_score(y_train, y_train_pred_prob_true),
                'test_roc_auc': roc_auc_score(y_test, y_test_pred_prob_true)
            }
            print('Scores after fitting:', scores)
        else:
            estimator.fit(X_train, y_train)
            y_train_pred = estimator.predict(X_train)
            y_train_pred_prob = estimator.predict_proba(X_train)[:,1]
            y_test_pred = estimator.predict(X_test)
            y_test_pred_prob = estimator.predict_proba(X_test)[:,1]
            scores = {'train_accuracy': accuracy_score(y_train, y_train_pred),
                     'test_accuracy': accuracy_score(y_test, y_test_pred),
                     'train_roc_auc': roc_auc_score(y_train, y_train_pred_prob),
                     'test_roc_auc': roc_auc_score(y_test, y_test_pred_prob)}
        out_dict = g.copy()
        out_dict.update(scores)
        print(out_dict)
        out = out.append(out_dict, ignore_index=True)
    return out
def grid_search ( estimator , param_grid , X_train , y_train , X_test , y_test , batch_size = 10000 , nn = False ) : out = pd . DataFrame ( ) for g in ParameterGrid ( param_grid ) : estimator . set_params ( ** g ) if nn : print ( 'Fitting with params:' , g ) early_stopping_monitor = EarlyStopping ( patience = 2 ) estimator . fit ( X_train , y_train , verbose = True , validation_split = 0.3 , epochs = 1000 , batch_size = batch_size , callbacks = [ early_stopping_monitor ] ) y_train_pred_prob_true = estimator . predict_proba ( X_train ) [ : , 1 ] y_test_pred_prob_true = estimator . predict_proba ( X_test ) [ : , 1 ] scores = { 'train_roc_auc' : roc_auc_score ( y_train , y_train_pred_prob_true ) , 'test_roc_auc' : roc_auc_score ( y_test , y_test_pred_prob_true ) } print ( 'Scores after fitting:' , scores ) else : estimator . fit ( X_train , y_train ) y_train_pred = estimator . predict ( X_train ) y_train_pred_prob = estimator . predict_proba ( X_train ) [ : , 1 ] y_test_pred = estimator . predict ( X_test ) y_test_pred_prob = estimator . predict_proba ( X_test ) [ : , 1 ] scores = { 'train_accuracy' : accuracy_score ( y_train , y_train_pred ) , 'test_accuracy' : accuracy_score ( y_test , y_test_pred ) , 'train_roc_auc' : roc_auc_score ( y_train , y_train_pred_prob ) , 'test_roc_auc' : roc_auc_score ( y_test , y_test_pred_prob ) } out_dict = g . copy ( ) out_dict . update ( scores ) print ( out_dict ) out = out . append ( out_dict , ignore_index = True ) return out
Performs grid search over parameter grid.
Performs grid search over parameter grid .
""" Performs grid search over parameter grid.

    Function iterates over the combinations of parameters in the parameter grid. Trains the estimator on the X_train.
    Evaluation is done on both the training set and validation set. The parameters reported are ROC AUC score calculated
    on the training and test sets.

    :param estimator: classifier for training
    :param param_grid: dictionary with parameters for the estimator
    :param X_train: training set input
    :param y_train: training set output
    :param X_test: test set input
    :param y_test: test set outpu
    :param batch_size: batch size for the neural network classifier
    :param nn: True if the estimator is a neural network model
    :return: a pandas DataFrame with the parameter combinations and the corresponding model evaluation metrics.
    """ # Define early_stopping_monitor # Fit the model
a pandas DataFrame with the parameter combinations and the corresponding model evaluation metrics.
a pandas DataFrame with the parameter combinations and the corresponding model evaluation metrics .
estimator
classifier for training
classifier for training
param_grid
dictionary with parameters for the estimator
dictionary with parameters for the estimator
X_train
training set input
training set input
y_train
training set output
training set output
X_test
test set input
test set input
y_test
test set outpu
test set outpu
batch_size
batch size for the neural network classifier
batch size for the neural network classifier
nn
True if the estimator is a neural network model
True if the estimator is a neural network model