File size: 4,923 Bytes
a603e80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pandas as pd 
import matplotlib.pyplot as plt 
import sklearn.metrics as metrics

def make_classification_report(model, y_true, x_test, title=''):
    
    '''

    Generate and return the classification report for a model.

    

    Args: 

        Model (classification model): SKlearn compatable model.

        y_true (series or array): True labels to compare predictions

        x_test (dataframe or array): X data to generate predictions for

        title (str): Title for the report

        

    Returns:

        Dictionary of the classification results

    

    Example:

        make_classification_report(logreg_model, y_test, X_test, 

                                    title='Logistic Regression Model')

        

        '''
    # Generate predictions
    y_preds = model.predict(x_test)
    print('__________________________________________________________________')
    print(f'CLASSIFICATION REPORT FOR: \n\t{title}')
    print('__________________________________________________________________')
    print('\n')
    
    # Generate report
    report = metrics.classification_report(y_true, y_preds, 
                                target_names=['AD', 'LMCI','CN'])
    report_dict = metrics.classification_report(y_true, y_preds, 
                                                output_dict=True,
                             target_names=['AD', 'LMCI', 'CN'])
    
    # Add the title to the report dictionary
    report_dict['title'] = title
    print(report)
    print('__________________________________________________________________')
    
    return report_dict


def plot_confusion_matrix(model, X, y, title=''):
    '''

    Plots the normalized confusion matrix for a model

    

    Args:

        Model (classification model): SKlearn compatable model

        X (dataframe or array): feature columns of a dataframe

        y (series or array): target column of a dataframe

        title (str): Title for the matrix

    

    Returns:

        Plotted figure of the confusion matrix for the model

    

    Example:

        plot_confusion_matrix(logreg_model, X_test, y_test, 

        title='Logistic Regression Model')

    '''

    fig, ax = plt.subplots()
    metrics.ConfusionMatrixDisplay.from_estimator(model, X, y, normalize='true', 
                                      cmap='Blues', 
                                      display_labels=['AD', 'LMCI', 'CN'],
                                      ax=ax)


    # Remove grid lines
    plt.grid(False)
    
    # Set title
    plt.title(f'Confusion Matrix For {title}', fontdict={'fontsize':17})
    plt.show()
    print('__________________________________________________________________')
    return fig

def plot_top_features(model, xtrain, title=''):
    '''

    Plots the top important features of a tree based model

    

    Args:

        Model (classification model): SKlearn compatable model

        xtrain (dataframe or array): feature columns for the training set

        title (str): Title for the plot

        

    Returns:

        Plotted figure of feature importances for the model

    

    Example:

        plot_top_features(rf_model, X_train, title='Random Forest Model')

    '''

    # Turn the feature importances into a series 
    importances = pd.Series(model.feature_importances_, index=xtrain.columns)
    
    # Plot the top most important features
    importances.nlargest(20).sort_values().plot(kind='barh', color='green')
    plt.title(f'Most Important Features For {title}', fontdict={'fontsize':17})
    plt.xlabel('Importance')
    return importances.sort_values(ascending=False)


def evaluate_model(model, xtrain, xtest, ytest, tree=False, title=''):
    '''

    Runs all the evaluation functions on a model including the classification 

    report, confusion matrix, ROC plot, and a top features plot if the 

    model is tree based.

    

    Args:

        model (classification model): SKlearn compatable model

        xtrain (dataframe or array): feature columns of the training set

        xtest (dataframe or array): feature columns of the test set

        ytest (series or array): target column of the test set

        tree (boolean): if the model is tree based or not

        title (str): Title for the model

    

    Returns:

        The classification report, confusion matrix, precision recall plot, and 

        top features plot if tree=True

    

    Example:

        evaluate_model(logreg_model, X_train, X_test, y_test,

                        title='Logistic Regression Model')

        

    '''
    
    make_classification_report(model, ytest, xtest, title=title)
    plot_confusion_matrix(model, xtest, ytest, title=title)
    
    # Feature importance can only be run on tree based models
    if tree:
        plot_top_features(model, xtrain, title=title)