src/models/param_tuning.py
def rf_model(x_train, y_train,
             random_state=42,
             num_eval=100):
    """Train a Random Forest model and determine optimal parameters using hyperopt"""

    # load params to create SKF splits using identical params
    # TODO - fix function to accept iter for CV - keep getting index out of range
    params = load_params()
    params_split = params['train_test_split']
    params_split["random_seed"] = params["random_seed"]
    num_eval = params["param_tuning"]["num_eval"]

    # K-fold split into train and dev sets stratified by train_labels
    # using random seed for reproducibility
    skf = StratifiedKFold(n_splits=params_split['n_split'],
                          random_state=params_split['random_seed'],
                          shuffle=params_split['shuffle'])

    # objective function
    def obj_fnc(params):
        params["n_estimators"] = int(params["n_estimators"])
        params["min_samples_leaf"] = int(params["min_samples_leaf"])
        params["min_samples_split"] = int(params["min_samples_split"])
        estimator = RandomForestClassifier(**params,
                                           random_state=random_state)
        score = cross_val_score(estimator, x_train, y=y_train,
                                scoring='accuracy',
                                cv=skf.split(x_train, y_train)).mean()

        return {"loss": -score, "status": hyperopt.STATUS_OK}

    # search space
    criterion_list = ["gini", "entropy"]
    max_depth_list = [None, 4, 6, 8, 10, 12, 15, 20]
    max_features_list = ["auto", "sqrt", "log2"]

    space = {"n_estimators": hyperopt.hp.quniform("n_estimators", 10, 500, 10),
             "max_features": hyperopt.hp.choice("max_features", max_features_list,),
             "max_depth": hyperopt.hp.choice("max_depth", max_depth_list),
             "min_samples_leaf": hyperopt.hp.quniform("min_samples_leaf", 1, 10, 1),
             "min_samples_split": hyperopt.hp.quniform("min_samples_split", 2, 10, 1),
             "criterion": hyperopt.hp.choice("criterion", criterion_list)
             }

    # create trials instance
    trials = Trials()

    # compute optimal parameters
    # TODO - see if can set random_seed to Trials or fmin
    best_param = hyperopt.fmin(obj_fnc, space,
                               algo=tpe.suggest,
                               max_evals=num_eval,
                               trials=trials,
                               rstate=np.random.RandomState(random_state)
                               )

    # update criterion with text option
    best_param["criterion"] = criterion_list[best_param["criterion"]]
    best_param["max_features"] = max_features_list[best_param["max_features"]]
    best_param["max_depth"] = max_depth_list[best_param["max_depth"]]
    best_param["n_estimators"] = int(best_param["n_estimators"])
    best_param["min_samples_leaf"] = int(best_param["min_samples_leaf"])
    best_param["min_samples_split"] = int(best_param["min_samples_split"])

    return best_param
Train a Random Forest model and determine optimal parameters using hyperopt
Train a Random Forest model and determine optimal parameters using hyperopt
Train a Random Forest model and determine optimal parameters using hyperopt
def rf_model(x_train, y_train,
             random_state=42,
             num_eval=100):
    params = load_params()
    params_split = params['train_test_split']
    params_split["random_seed"] = params["random_seed"]
    num_eval = params["param_tuning"]["num_eval"]
    skf = StratifiedKFold(n_splits=params_split['n_split'],
                          random_state=params_split['random_seed'],
                          shuffle=params_split['shuffle'])
    def obj_fnc(params):
        params["n_estimators"] = int(params["n_estimators"])
        params["min_samples_leaf"] = int(params["min_samples_leaf"])
        params["min_samples_split"] = int(params["min_samples_split"])
        estimator = RandomForestClassifier(**params,
                                           random_state=random_state)
        score = cross_val_score(estimator, x_train, y=y_train,
                                scoring='accuracy',
                                cv=skf.split(x_train, y_train)).mean()
        return {"loss": -score, "status": hyperopt.STATUS_OK}
    criterion_list = ["gini", "entropy"]
    max_depth_list = [None, 4, 6, 8, 10, 12, 15, 20]
    max_features_list = ["auto", "sqrt", "log2"]
    space = {"n_estimators": hyperopt.hp.quniform("n_estimators", 10, 500, 10),
             "max_features": hyperopt.hp.choice("max_features", max_features_list,),
             "max_depth": hyperopt.hp.choice("max_depth", max_depth_list),
             "min_samples_leaf": hyperopt.hp.quniform("min_samples_leaf", 1, 10, 1),
             "min_samples_split": hyperopt.hp.quniform("min_samples_split", 2, 10, 1),
             "criterion": hyperopt.hp.choice("criterion", criterion_list)
             }
    trials = Trials()
    best_param = hyperopt.fmin(obj_fnc, space,
                               algo=tpe.suggest,
                               max_evals=num_eval,
                               trials=trials,
                               rstate=np.random.RandomState(random_state)
                               )
    best_param["criterion"] = criterion_list[best_param["criterion"]]
    best_param["max_features"] = max_features_list[best_param["max_features"]]
    best_param["max_depth"] = max_depth_list[best_param["max_depth"]]
    best_param["n_estimators"] = int(best_param["n_estimators"])
    best_param["min_samples_leaf"] = int(best_param["min_samples_leaf"])
    best_param["min_samples_split"] = int(best_param["min_samples_split"])
    return best_param
def rf_model ( x_train , y_train , random_state = 42 , num_eval = 100 ) : params = load_params ( ) params_split = params [ 'train_test_split' ] params_split [ "random_seed" ] = params [ "random_seed" ] num_eval = params [ "param_tuning" ] [ "num_eval" ] skf = StratifiedKFold ( n_splits = params_split [ 'n_split' ] , random_state = params_split [ 'random_seed' ] , shuffle = params_split [ 'shuffle' ] ) def obj_fnc ( params ) : params [ "n_estimators" ] = int ( params [ "n_estimators" ] ) params [ "min_samples_leaf" ] = int ( params [ "min_samples_leaf" ] ) params [ "min_samples_split" ] = int ( params [ "min_samples_split" ] ) estimator = RandomForestClassifier ( ** params , random_state = random_state ) score = cross_val_score ( estimator , x_train , y = y_train , scoring = 'accuracy' , cv = skf . split ( x_train , y_train ) ) . mean ( ) return { "loss" : - score , "status" : hyperopt . STATUS_OK } criterion_list = [ "gini" , "entropy" ] max_depth_list = [ None , 4 , 6 , 8 , 10 , 12 , 15 , 20 ] max_features_list = [ "auto" , "sqrt" , "log2" ] space = { "n_estimators" : hyperopt . hp . quniform ( "n_estimators" , 10 , 500 , 10 ) , "max_features" : hyperopt . hp . choice ( "max_features" , max_features_list , ) , "max_depth" : hyperopt . hp . choice ( "max_depth" , max_depth_list ) , "min_samples_leaf" : hyperopt . hp . quniform ( "min_samples_leaf" , 1 , 10 , 1 ) , "min_samples_split" : hyperopt . hp . quniform ( "min_samples_split" , 2 , 10 , 1 ) , "criterion" : hyperopt . hp . choice ( "criterion" , criterion_list ) } trials = Trials ( ) best_param = hyperopt . fmin ( obj_fnc , space , algo = tpe . suggest , max_evals = num_eval , trials = trials , rstate = np . random . RandomState ( random_state ) ) best_param [ "criterion" ] = criterion_list [ best_param [ "criterion" ] ] best_param [ "max_features" ] = max_features_list [ best_param [ "max_features" ] ] best_param [ "max_depth" ] = max_depth_list [ best_param [ "max_depth" ] ] best_param [ "n_estimators" ] = int ( best_param [ "n_estimators" ] ) best_param [ "min_samples_leaf" ] = int ( best_param [ "min_samples_leaf" ] ) best_param [ "min_samples_split" ] = int ( best_param [ "min_samples_split" ] ) return best_param
Train a Random Forest model and determine optimal parameters using hyperopt
Train a Random Forest model and determine optimal parameters using hyperopt
"""Train a Random Forest model and determine optimal parameters using hyperopt""" # load params to create SKF splits using identical params # TODO - fix function to accept iter for CV - keep getting index out of range # K-fold split into train and dev sets stratified by train_labels # using random seed for reproducibility # objective function # search space # create trials instance # compute optimal parameters # TODO - see if can set random_seed to Trials or fmin # update criterion with text option
x_train
y_train
random_state
num_eval