CardiovascularDisease/CVD/ML_models.py
def kNN_model(X_train: pd.DataFrame, X_test: pd.DataFrame, y_train: pd.Series, y_test: pd.Series):
    """
    K-nearest neighbors model: find best hyperparameter k and weight method.
    Then fit to (unscaled) 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).
    """

    scores_u = []  # uniform weights
    scores_d = []  # weights inversely related to distance
    k_range = range(20, 71)
    for k in k_range:
        for weight in ['uniform', 'distance']:
            score = get_cv_score(X_train, y_train, KNeighborsClassifier,
                                 options={'n_neighbors': k, 'weights': weight})
            scores_u.append(score) if weight == 'uniform' else scores_d.append(score)

    fig, ax = plt.subplots(figsize=(6, 5), dpi=100)
    ax.plot(k_range, scores_u)
    ax.plot(k_range, scores_d)
    ax.set(title='K-Nearest Neighbors: Mean (cv=10) Accuracy Scores\n'
                 'for Different Weight Methods',
           xlabel='$k$ neighbors', ylabel='Accuracy')
    ax.legend(['uniform', 'distance'], loc='lower right')
    fig.savefig('kNN_cv_search.png')

    k_best_u = scores_u.index(np.max(scores_u))
    k_best_d = scores_d.index(np.max(scores_d))
    print(f"Estimator: K-nearest neighbors:\n"
          f"Optimal k={k_best_u + k_range[0]} with an accuracy of {scores_u[k_best_u]} with "
          "uniform weight method.\n"
          f"Optimal k={k_best_d + k_range[0]} with an accuracy of {scores_d[k_best_d]} with "
          "distance weight method.")

    optimal_hyperparams = {'k': k_best_u + 1 if k_best_u >= k_best_d else k_best_d + 1,
                           'weights': 'uniform' if k_best_u >= k_best_d else 'distance'}

    model = KNeighborsClassifier(n_neighbors=optimal_hyperparams['k'],
                                 weights=optimal_hyperparams['weights'])
    model.fit(X_train, y_train)  # Train the model
    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)

    K-nearest neighbors model: find best hyperparameter k and weight method.
    Then fit to (unscaled) 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).
    
K-nearest neighbors model: find best hyperparameter k and weight method.
Then fit to (unscaled) data and predict.
K - nearest neighbors model : find best hyperparameter k and weight method . Then fit to ( unscaled ) data and predict .
def kNN_model(X_train: pd.DataFrame, X_test: pd.DataFrame, y_train: pd.Series, y_test: pd.Series):
    scores_u = []  
    scores_d = []  
    k_range = range(20, 71)
    for k in k_range:
        for weight in ['uniform', 'distance']:
            score = get_cv_score(X_train, y_train, KNeighborsClassifier,
                                 options={'n_neighbors': k, 'weights': weight})
            scores_u.append(score) if weight == 'uniform' else scores_d.append(score)
    fig, ax = plt.subplots(figsize=(6, 5), dpi=100)
    ax.plot(k_range, scores_u)
    ax.plot(k_range, scores_d)
    ax.set(title='K-Nearest Neighbors: Mean (cv=10) Accuracy Scores\n'
                 'for Different Weight Methods',
           xlabel='$k$ neighbors', ylabel='Accuracy')
    ax.legend(['uniform', 'distance'], loc='lower right')
    fig.savefig('kNN_cv_search.png')
    k_best_u = scores_u.index(np.max(scores_u))
    k_best_d = scores_d.index(np.max(scores_d))
    print(f"Estimator: K-nearest neighbors:\n"
          f"Optimal k={k_best_u + k_range[0]} with an accuracy of {scores_u[k_best_u]} with "
          "uniform weight method.\n"
          f"Optimal k={k_best_d + k_range[0]} with an accuracy of {scores_d[k_best_d]} with "
          "distance weight method.")
    optimal_hyperparams = {'k': k_best_u + 1 if k_best_u >= k_best_d else k_best_d + 1,
                           'weights': 'uniform' if k_best_u >= k_best_d else 'distance'}
    model = KNeighborsClassifier(n_neighbors=optimal_hyperparams['k'],
                                 weights=optimal_hyperparams['weights'])
    model.fit(X_train, y_train)  
    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 kNN_model ( X_train : pd . DataFrame , X_test : pd . DataFrame , y_train : pd . Series , y_test : pd . Series ) : scores_u = [ ] scores_d = [ ] k_range = range ( 20 , 71 ) for k in k_range : for weight in [ 'uniform' , 'distance' ] : score = get_cv_score ( X_train , y_train , KNeighborsClassifier , options = { 'n_neighbors' : k , 'weights' : weight } ) scores_u . append ( score ) if weight == 'uniform' else scores_d . append ( score ) fig , ax = plt . subplots ( figsize = ( 6 , 5 ) , dpi = 100 ) ax . plot ( k_range , scores_u ) ax . plot ( k_range , scores_d ) ax . set ( title = 'K-Nearest Neighbors: Mean (cv=10) Accuracy Scores\n' 'for Different Weight Methods' , xlabel = '$k$ neighbors' , ylabel = 'Accuracy' ) ax . legend ( [ 'uniform' , 'distance' ] , loc = 'lower right' ) fig . savefig ( 'kNN_cv_search.png' ) k_best_u = scores_u . index ( np . max ( scores_u ) ) k_best_d = scores_d . index ( np . max ( scores_d ) ) print ( f"Estimator: K-nearest neighbors:\n" f"Optimal k={k_best_u + k_range[0]} with an accuracy of {scores_u[k_best_u]} with " "uniform weight method.\n" f"Optimal k={k_best_d + k_range[0]} with an accuracy of {scores_d[k_best_d]} with " "distance weight method." ) optimal_hyperparams = { 'k' : k_best_u + 1 if k_best_u >= k_best_d else k_best_d + 1 , 'weights' : 'uniform' if k_best_u >= k_best_d else 'distance' } model = KNeighborsClassifier ( n_neighbors = optimal_hyperparams [ 'k' ] , weights = optimal_hyperparams [ 'weights' ] ) model . fit ( X_train , y_train ) 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 )
K-nearest neighbors model: find best hyperparameter k and weight method.
K - nearest neighbors model : find best hyperparameter k and weight method .
"""
    K-nearest neighbors model: find best hyperparameter k and weight method.
    Then fit to (unscaled) 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).
    """ # uniform weights # weights inversely related to distance # Train the model # 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 .