from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve, ConfusionMatrixDisplay def plot_confusion_matrix_and_roc(model,df): """Helper function for plotting model performances""" # Make predictions on the holdout set X,y = df.drop('target',axis=1), df['target'] y_pred = model.predict(X) # Create the confusion matrix cm = confusion_matrix(y, y_pred) # Calculate the AUC ROC score auc = roc_auc_score(y, y_pred) # Get the FPR and TPR for the ROC curve fpr, tpr, thresholds = roc_curve(y, y_pred) # Create a figure with two subplots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5)) # Plot the confusion matrix ax1.matshow(cm, cmap='Blues') ax1.set_title('Confusion Matrix') ax1.set_xlabel('Predicted') ax1.set_ylabel('Actual') # Add labels to the confusion matrix # for i in range(cm.shape[0]): # for j in range(cm.shape[1]): # ax1.text(j, i, f'{cm[i, j]:n}', ha='center', va='center') # Add labels for the TP, FP, TN, and FN cells ax1.text(0, 0, f'TN: {cm[0, 0]:n}', ha='center', va='center', color='w') ax1.text(0, 1, f'FN: {cm[1, 0]:n}', ha='center', va='center', color='k') ax1.text(1, 0, f'FP: {cm[0, 1]:n}', ha='center', va='center', color='k') ax1.text(1, 1, f'TP: {cm[1, 1]:n}', ha='center', va='center', color='k') # Plot the ROC curve ax2.plot(fpr, tpr, label='AUC ROC = %0.2f' % auc) ax2.plot([0, 1], [0, 1], 'k--') ax2.set_title('ROC Curve') ax2.set_xlabel('FPR') ax2.set_ylabel('TPR') ax2.legend() plt.show() #train the model from xgboost import XGBClassifier model = XGBClassifier() model.fit(X_resampled,y_resampled) plot_confusion_matrix_and_roc(model,df_holdout)