Wizzas's picture
Upload source code
a603e80 verified
Raw History Blame Contribute Delete
4.92 kB
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)