src/models/train_model.py
def main(train_path, cv_idx_path,
         results_dir, model_dir):
    """Train RandomForest model and predict survival on
    Kaggle test set"""
    assert (os.path.isdir(results_dir)), NotADirectoryError
    assert (os.path.isdir(model_dir)), NotADirectoryError
    results_dir = Path(results_dir).resolve()
    model_dir = Path(model_dir).resolve()

    # read files
    train_df, cv_idx = load_data([train_path, cv_idx_path],
                                 sep=",", header=0,
                                 index_col="PassengerId")
    # load params
    params = load_params()
    classifier = params["classifier"]
    target_class = params["train_test_split"]["target_class"]
    model_params = params["model_params"][classifier]

    # get independent variables (features) and
    # dependent variables (labels)
    train_feats = train_df.drop(target_class, axis=1)
    train_labels = train_df[target_class]

    # create instance using random seed for reproducibility
    if classifier.lower() == "random_forest":
        model = RandomForestClassifier(**model_params,
                                       random_state=params["random_seed"])
    elif classifier.lower() == "xgboost":
        model = XGBClassifier(random_state=params["random_seed"])
    else:
        raise NotImplementedError

    # create generator with cv splits
    split_generator = iter((np.where(cv_idx[col] == "train")[0],
                            np.where(cv_idx[col] == "test")[0]) for col in cv_idx)

    # set model scoring metrics
    # TODO - add custom metric for GMPR
    scoring = {'accuracy': 'accuracy', 'balanced_accuracy': 'balanced_accuracy',
               'f1': 'f1',
               "gmpr": make_scorer(gmpr_score, greater_is_better=True),
               'jaccard': 'jaccard', 'precision': 'precision',
               'recall': 'recall', 'roc_auc': 'roc_auc'}

    # train using cross validation
    cv_output = cross_validate(model, train_feats.to_numpy(),
                               train_labels.to_numpy(),
                               cv=split_generator,
                               fit_params=None,
                               scoring=scoring,
                               return_estimator=True)

    # get cv estimators
    cv_estimators = cv_output.pop('estimator')
    cv_metrics = pd.DataFrame(cv_output)

    # rename columns
    col_mapper = dict(zip(cv_metrics.columns,
                          [elem.replace('test_', '') for elem in cv_metrics.columns]))
    cv_metrics = cv_metrics.rename(columns=col_mapper)

    # save cv estimators as pickle file
    with open(model_dir.joinpath("estimator.pkl"), "wb") as file:
        pickle.dump(cv_estimators, file)

    # save metrics
    metrics = json.dumps(dict(cv_metrics.mean()))
    with open(results_dir.joinpath("metrics.json"), "w") as writer:
        writer.writelines(metrics)
Train RandomForest model and predict survival on
    Kaggle test set
Train RandomForest model and predict survival on
Kaggle test set
Train RandomForest model and predict survival on Kaggle test set
def main(train_path, cv_idx_path,
         results_dir, model_dir):
    assert (os.path.isdir(results_dir)), NotADirectoryError
    assert (os.path.isdir(model_dir)), NotADirectoryError
    results_dir = Path(results_dir).resolve()
    model_dir = Path(model_dir).resolve()
    train_df, cv_idx = load_data([train_path, cv_idx_path],
                                 sep=",", header=0,
                                 index_col="PassengerId")
    params = load_params()
    classifier = params["classifier"]
    target_class = params["train_test_split"]["target_class"]
    model_params = params["model_params"][classifier]
    train_feats = train_df.drop(target_class, axis=1)
    train_labels = train_df[target_class]
    if classifier.lower() == "random_forest":
        model = RandomForestClassifier(**model_params,
                                       random_state=params["random_seed"])
    elif classifier.lower() == "xgboost":
        model = XGBClassifier(random_state=params["random_seed"])
    else:
        raise NotImplementedError
    split_generator = iter((np.where(cv_idx[col] == "train")[0],
                            np.where(cv_idx[col] == "test")[0]) for col in cv_idx)
    scoring = {'accuracy': 'accuracy', 'balanced_accuracy': 'balanced_accuracy',
               'f1': 'f1',
               "gmpr": make_scorer(gmpr_score, greater_is_better=True),
               'jaccard': 'jaccard', 'precision': 'precision',
               'recall': 'recall', 'roc_auc': 'roc_auc'}
    cv_output = cross_validate(model, train_feats.to_numpy(),
                               train_labels.to_numpy(),
                               cv=split_generator,
                               fit_params=None,
                               scoring=scoring,
                               return_estimator=True)
    cv_estimators = cv_output.pop('estimator')
    cv_metrics = pd.DataFrame(cv_output)
    col_mapper = dict(zip(cv_metrics.columns,
                          [elem.replace('test_', '') for elem in cv_metrics.columns]))
    cv_metrics = cv_metrics.rename(columns=col_mapper)
    with open(model_dir.joinpath("estimator.pkl"), "wb") as file:
        pickle.dump(cv_estimators, file)
    metrics = json.dumps(dict(cv_metrics.mean()))
    with open(results_dir.joinpath("metrics.json"), "w") as writer:
        writer.writelines(metrics)
def main ( train_path , cv_idx_path , results_dir , model_dir ) : assert ( os . path . isdir ( results_dir ) ) , NotADirectoryError assert ( os . path . isdir ( model_dir ) ) , NotADirectoryError results_dir = Path ( results_dir ) . resolve ( ) model_dir = Path ( model_dir ) . resolve ( ) train_df , cv_idx = load_data ( [ train_path , cv_idx_path ] , sep = "," , header = 0 , index_col = "PassengerId" ) params = load_params ( ) classifier = params [ "classifier" ] target_class = params [ "train_test_split" ] [ "target_class" ] model_params = params [ "model_params" ] [ classifier ] train_feats = train_df . drop ( target_class , axis = 1 ) train_labels = train_df [ target_class ] if classifier . lower ( ) == "random_forest" : model = RandomForestClassifier ( ** model_params , random_state = params [ "random_seed" ] ) elif classifier . lower ( ) == "xgboost" : model = XGBClassifier ( random_state = params [ "random_seed" ] ) else : raise NotImplementedError split_generator = iter ( ( np . where ( cv_idx [ col ] == "train" ) [ 0 ] , np . where ( cv_idx [ col ] == "test" ) [ 0 ] ) for col in cv_idx ) scoring = { 'accuracy' : 'accuracy' , 'balanced_accuracy' : 'balanced_accuracy' , 'f1' : 'f1' , "gmpr" : make_scorer ( gmpr_score , greater_is_better = True ) , 'jaccard' : 'jaccard' , 'precision' : 'precision' , 'recall' : 'recall' , 'roc_auc' : 'roc_auc' } cv_output = cross_validate ( model , train_feats . to_numpy ( ) , train_labels . to_numpy ( ) , cv = split_generator , fit_params = None , scoring = scoring , return_estimator = True ) cv_estimators = cv_output . pop ( 'estimator' ) cv_metrics = pd . DataFrame ( cv_output ) col_mapper = dict ( zip ( cv_metrics . columns , [ elem . replace ( 'test_' , '' ) for elem in cv_metrics . columns ] ) ) cv_metrics = cv_metrics . rename ( columns = col_mapper ) with open ( model_dir . joinpath ( "estimator.pkl" ) , "wb" ) as file : pickle . dump ( cv_estimators , file ) metrics = json . dumps ( dict ( cv_metrics . mean ( ) ) ) with open ( results_dir . joinpath ( "metrics.json" ) , "w" ) as writer : writer . writelines ( metrics )
Train RandomForest model and predict survival on
Kaggle test set
Train RandomForest model and predict survival on Kaggle test set
"""Train RandomForest model and predict survival on
    Kaggle test set""" # read files # load params # get independent variables (features) and # dependent variables (labels) # create instance using random seed for reproducibility # create generator with cv splits # set model scoring metrics # TODO - add custom metric for GMPR # train using cross validation # get cv estimators # rename columns # save cv estimators as pickle file # save metrics
train_path
cv_idx_path
results_dir
model_dir