Credit risk: unsupervised clients clustering | by Valentina Alto | Towards Data Science

One of the industries which is heavily using Machine Learning solutions is that of Banking. In particular, let’s focus for a while on the field of consumer credit: it refers to any operation which involves a private actor, which might be a single individual rather than a family, and the bank. The idea of consumer credit is that the bank will gain from giving credit only if the client will not default (that means, will not repay the debt). Indeed, once accepted as creditworthy and received the credit, the client will plan, together with the bank, an amortization schedule according to which he will have to repay not only the debt, but also the interests.
So, if from one side giving credit is one of the incomes of a bank (because of the interests), from the other it involves a noticeable amount of risk. That’s why a great amount of time and money is invested in analyzing clients’ history, habits and likelihood of repaying the debt.
To do so, banks have always been relying on statistical models (especially scoring models), however today, with the aid of Machine Learning algorithms, their predictions about future repayments are far more reliable.
A first approach might be training an algorithm on historical data about bank’s clients, and see which are the features which characterized those who repaid the debt and those who did not. Then, when a new client will come to the bank, the algorithm will evaluate him and will state whether he will repay the debt.
As you can see, we are talking about supervised learning: historical data are provided with labels (creditworthy or not creditworthy), hence the algorithm will be trained on the train set and evaluated on the test set.
However, in this article I’m going to propose the unsupervised way of the problem: imagine we are provided with the same data but without any label. What we want to do is clustering our clients and see if, from that procedure, we can get some relevant information about their being creditworthy.
So let’s start. The dataset I’m going to use is the German Credit Risk dataset, available on Kaggle here.
import pandas as pddf=pd.read_csv(r'german_credit_data.csv')df=df.dropna()df=df.drop(['Unnamed: 0'],axis=1)
After importing and cleaning the dataset, I have to modify it a little bit. Indeed, we are provided with categorical columns, like ‘Sex’ or ‘Purpose’, however algorithms are mathematical operations and they need numbers, not strings. Hence, I will proceed with the OneHot-encoding procedure. I’m not going to explain the idea behind here, however I provide a full explanation with examples in this article.
df=pd.get_dummies(df,prefix=['Sex','Housing','Saving Account','Checking account','Purpose'], drop_first=True)
Now, before applying our clustering algorithm, we have to recall that, generally speaking, we like models when they are simple. So here, we have 19 features (initially, they were 10, but with OneHot encoding became 19) describing our clients, but do we need all of them? Couldn’t we reduce the number of features without undermining the model in terms of information loss?
The answer to this question relies on Principal Components Analysis, whose aim is, indeed, reducing the complexity of the model without losing information by creating new variables. Those latter have a peculiarity: the information is stored mainly in the first ones, so that, if you want to reduce the dimensionality of your problem, you can use only your first components with a minimum loss of information (if you want to learn more about PCA, you can read my former article here).
Now, let’s say we want to reduce our dimensionality from 19 to 2:
from sklearn.decomposition import PCA pca = PCA(2)  projected = pca.fit_transform(features)print(features.shape)print(projected.shape)
As you can see, from 19 features we are now handling only 2.
Now it’s time to apply our algorithm. For this purpose, I’m going to use K-means, which is a powerful unsupervised algorithm and I wrote a full explanation of its functioning here. Furthermore, to identify the proper number of centroids, hence labels, I will use the Elbow method technique:
import matplotlib.pyplot as pltfrom sklearn.cluster import KMeanswcss = []K = range(1,15)for k in K:    km = KMeans(n_clusters=k)    km = km.fit(projected)    wcss.append(km.inertia_)plt.plot(K, wcss, 'bx-')plt.xlabel('Number of centroids')plt.ylabel('WCSS')plt.title('Elbow Method For Optimal k')plt.show()
As you can see, the optimal k is somewhere between 3 and 4. For the sake of simplicity of the model, let’s set k=3. So let’s see how our algorithm will work:
#converting our projected array to pandas dfpca=pd.DataFrame(projected)pca.columns=['First component','Second Component']#build our algorithm with k=3, train it on pca and make predictionskmeans = KMeans(n_clusters=3, random_state=0).fit(pca)y_kmeans = kmeans.predict(pca)#plotting the results plt.scatter(pca['First component'], pca['Second Component'], c=y_kmeans, s=50, alpha=0.5,cmap='viridis')centers = kmeans.cluster_centers_plt.scatter(centers[:, 0], centers[:, 1], c='red', s=50)plt.scatter(centers[:, 0], centers[:, 1], c='red', s=50)
As you can see, our algorithm clustered our clients in three categories, according to their features (which have been stored mainly in the first two principal components).
The next step for the analyst might be inquiring about the reasons behind this segmentation, and how those reasons might affect the decision of the bank of giving credit to a client of a particular cluster.
The power of clustering techniques is that they are able to produce relevant results in only few seconds, while analyzing all the clients manually would have taken a huge amount of time.