K Nearest Neighbors
Contents
K Nearest Neighbors#
We use a classification model to predict which customers will default on their credit card debt.
Data#
To learn more about the data and all of the data preparation steps, take a look at this page. Here, we simply import a Python script which includes all of the necessary steps.
from data_prep_credit import *
Model#
from sklearn import neighbors
clf = neighbors.KNeighborsClassifier(n_neighbors=2)
y_pred = clf.fit(X_train, y_train).predict(X_test)
Confusion matrix#
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
import matplotlib.pyplot as plt
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=clf.classes_)
disp.plot()
plt.show()
Classification report#
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred, digits=3))
precision recall f1-score support
0.0 0.975 0.996 0.985 2909
1.0 0.571 0.176 0.269 91
accuracy 0.971 3000
macro avg 0.773 0.586 0.627 3000
weighted avg 0.963 0.971 0.963 3000