CardiovascularDisease/CVD/ML_models.py
def group_model(X_test: pd.DataFrame, X_test_scaled: pd.DataFrame, y_test: pd.Series, models: list, *, weights=None):
    """
    Creating a group/ensemble model of all of the fitted models.

    Note: The below manipulations are equivalent to using sklearn.ensemble.VotingClassifier()
    on the unfitted models with 'soft' voting. Since we have already fit the models,
    calculating the average class probabilities manually.

    :parameter X_test: Unscaled test data.
    :parameter X_test_scaled: Scaled test data.
    :parameter y_test: Target that corresponds to test data.
    :parameter models: List of pre-fitted model objects.
    :parameter weights: Relative importance of the models in evaluating the class probabilities of the group model.
                        (default=None; i.e., all models are equally important.)
    :return: Group model predictions for test dataset and classification report (dict).
    """

    n = len(models)
    wgts = np.ones(n) / n if weights is None else weights

    p_weighted_vals = np.empty((X_test.shape[0], 2, n))  # initialize matrix

    # Get probability values for both classes for each estimator and weigh them.
    for i, model in enumerate(models):
        print(model)
        p_weighted_vals[:, :, i] = wgts[i] * model.predict_proba(X_test if str(model).startswith('K')
                                                                 else X_test_scaled)

    ensemble_p_vals = np.mean(p_weighted_vals, axis=2)
    y_pred = ensemble_p_vals.argmax(axis=1)
    print(classification_report(y_test, y_pred))

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

    Creating a group/ensemble model of all of the fitted models.

    Note: The below manipulations are equivalent to using sklearn.ensemble.VotingClassifier()
    on the unfitted models with 'soft' voting. Since we have already fit the models,
    calculating the average class probabilities manually.

    :parameter X_test: Unscaled test data.
    :parameter X_test_scaled: Scaled test data.
    :parameter y_test: Target that corresponds to test data.
    :parameter models: List of pre-fitted model objects.
    :parameter weights: Relative importance of the models in evaluating the class probabilities of the group model.
                        (default=None; i.e., all models are equally important.)
    :return: Group model predictions for test dataset and classification report (dict).
    
Creating a group/ensemble model of all of the fitted models.
Note: The below manipulations are equivalent to using sklearn.ensemble.VotingClassifier()
on the unfitted models with 'soft' voting. Since we have already fit the models,
calculating the average class probabilities manually.
Creating a group / ensemble model of all of the fitted models . Note : The below manipulations are equivalent to using sklearn . ensemble . VotingClassifier () on the unfitted models with ' soft ' voting . Since we have already fit the models calculating the average class probabilities manually .
def group_model(X_test: pd.DataFrame, X_test_scaled: pd.DataFrame, y_test: pd.Series, models: list, *, weights=None):
    n = len(models)
    wgts = np.ones(n) / n if weights is None else weights
    p_weighted_vals = np.empty((X_test.shape[0], 2, n))  
    for i, model in enumerate(models):
        print(model)
        p_weighted_vals[:, :, i] = wgts[i] * model.predict_proba(X_test if str(model).startswith('K')
                                                                 else X_test_scaled)
    ensemble_p_vals = np.mean(p_weighted_vals, axis=2)
    y_pred = ensemble_p_vals.argmax(axis=1)
    print(classification_report(y_test, y_pred))
    return y_pred, classification_report(y_test, y_pred, output_dict=True)
def group_model ( X_test : pd . DataFrame , X_test_scaled : pd . DataFrame , y_test : pd . Series , models : list , * , weights = None ) : n = len ( models ) wgts = np . ones ( n ) / n if weights is None else weights p_weighted_vals = np . empty ( ( X_test . shape [ 0 ] , 2 , n ) ) for i , model in enumerate ( models ) : print ( model ) p_weighted_vals [ : , : , i ] = wgts [ i ] * model . predict_proba ( X_test if str ( model ) . startswith ( 'K' ) else X_test_scaled ) ensemble_p_vals = np . mean ( p_weighted_vals , axis = 2 ) y_pred = ensemble_p_vals . argmax ( axis = 1 ) print ( classification_report ( y_test , y_pred ) ) return y_pred , classification_report ( y_test , y_pred , output_dict = True )
Creating a group/ensemble model of all of the fitted models.
Creating a group / ensemble model of all of the fitted models .
"""
    Creating a group/ensemble model of all of the fitted models.

    Note: The below manipulations are equivalent to using sklearn.ensemble.VotingClassifier()
    on the unfitted models with 'soft' voting. Since we have already fit the models,
    calculating the average class probabilities manually.

    :parameter X_test: Unscaled test data.
    :parameter X_test_scaled: Scaled test data.
    :parameter y_test: Target that corresponds to test data.
    :parameter models: List of pre-fitted model objects.
    :parameter weights: Relative importance of the models in evaluating the class probabilities of the group model.
                        (default=None; i.e., all models are equally important.)
    :return: Group model predictions for test dataset and classification report (dict).
    """ # initialize matrix # Get probability values for both classes for each estimator and weigh them.
Group model predictions for test dataset and classification report (dict).
Group model predictions for test dataset and classification report ( dict ) .
X_test
pd.DataFrame
Unscaled test data.
Unscaled test data .
X_test_scaled
pd.DataFrame
Scaled test data.
Scaled test data .
y_test
pd.Series
Target that corresponds to test data.
Target that corresponds to test data .
models
list
List of pre-fitted model objects.
List of pre - fitted model objects .
weights
Relative importance of the models in evaluating the class probabilities of the group model.
(default=None; i.e., all models are equally important.)
Relative importance of the models in evaluating the class probabilities of the group model . ( default = None ; i . e . all models are equally important . )