CardiovascularDisease/CVD/ML_models.py
def RF_model(X_train: pd.DataFrame, X_test: pd.DataFrame, y_train: pd.Series, y_test: pd.Series):
    """
    Random Forest model model: find best hyperparameters.
    Then fit to data and predict.

    :parameter X_train: Training data.
    :parameter y_train: Target that corresponds to training data.
    :parameter X_test: Test data.
    :parameter y_test: Target that corresponds to test data.
    :return: Model object, model predictions for test dataset, and classification report (dict).
    """

    clf = RandomForestClassifier(random_state=0)

    hyperparams = {"max_features": ["auto"],
                   "max_leaf_nodes": [None],
                   "max_depth": [9]}
    cv = 10
    model = GridSearchCV(clf, hyperparams, cv=cv)
    model.fit(X_train, y_train)
    print(f"\nEstimator: Random Forest Classifier. \n"
          f"Best parameters after grid search with cross-validation (cv={cv}): \n"
          f"{model.best_params_}\nwith score {model.best_score_}")
    # If model.refit is true (default), the model automatically refits to all of X_train.
    print(f"Automatic refit to full X_train: {model.refit}")

    y_pred = model.predict(X_test)  # Predict classes in test set
    print(classification_report(y_test, y_pred))

    return model, y_pred, classification_report(y_test, y_pred, output_dict=True)

    Random Forest model model: find best hyperparameters.
    Then fit to data and predict.

    :parameter X_train: Training data.
    :parameter y_train: Target that corresponds to training data.
    :parameter X_test: Test data.
    :parameter y_test: Target that corresponds to test data.
    :return: Model object, model predictions for test dataset, and classification report (dict).
    
Random Forest model model: find best hyperparameters.
Then fit to data and predict.
Random Forest model model : find best hyperparameters . Then fit to data and predict .
def RF_model(X_train: pd.DataFrame, X_test: pd.DataFrame, y_train: pd.Series, y_test: pd.Series):
    clf = RandomForestClassifier(random_state=0)
    hyperparams = {"max_features": ["auto"],
                   "max_leaf_nodes": [None],
                   "max_depth": [9]}
    cv = 10
    model = GridSearchCV(clf, hyperparams, cv=cv)
    model.fit(X_train, y_train)
    print(f"\nEstimator: Random Forest Classifier. \n"
          f"Best parameters after grid search with cross-validation (cv={cv}): \n"
          f"{model.best_params_}\nwith score {model.best_score_}")
    print(f"Automatic refit to full X_train: {model.refit}")
    y_pred = model.predict(X_test)  
    print(classification_report(y_test, y_pred))
    return model, y_pred, classification_report(y_test, y_pred, output_dict=True)
def RF_model ( X_train : pd . DataFrame , X_test : pd . DataFrame , y_train : pd . Series , y_test : pd . Series ) : clf = RandomForestClassifier ( random_state = 0 ) hyperparams = { "max_features" : [ "auto" ] , "max_leaf_nodes" : [ None ] , "max_depth" : [ 9 ] } cv = 10 model = GridSearchCV ( clf , hyperparams , cv = cv ) model . fit ( X_train , y_train ) print ( f"\nEstimator: Random Forest Classifier. \n" f"Best parameters after grid search with cross-validation (cv={cv}): \n" f"{model.best_params_}\nwith score {model.best_score_}" ) print ( f"Automatic refit to full X_train: {model.refit}" ) y_pred = model . predict ( X_test ) print ( classification_report ( y_test , y_pred ) ) return model , y_pred , classification_report ( y_test , y_pred , output_dict = True )
Random Forest model model: find best hyperparameters.
Random Forest model model : find best hyperparameters .
"""
    Random Forest model model: find best hyperparameters.
    Then fit to data and predict.

    :parameter X_train: Training data.
    :parameter y_train: Target that corresponds to training data.
    :parameter X_test: Test data.
    :parameter y_test: Target that corresponds to test data.
    :return: Model object, model predictions for test dataset, and classification report (dict).
    """ # If model.refit is true (default), the model automatically refits to all of X_train. # Predict classes in test set
Model object, model predictions for test dataset, and classification report (dict).
Model object model predictions for test dataset and classification report ( dict ) .
X_train
pd.DataFrame
None
X_test
pd.DataFrame
None
y_train
pd.Series
Target that corresponds to training data.
Target that corresponds to training data .
y_test
pd.Series
Target that corresponds to test data.
Target that corresponds to test data .