DBSCAN — a density-based unsupervised algorithm for fraud detection | by Mahbubul Alam | Towards Data Science

According to a recent report financial losses due to fraudulent transactions have reached about $17 billion USD, with as many as 5% of consumers experiencing fraud incidents of some kind.
In light of such a big volume of financial losses, every industry is taking fraud detection seriously. It’s not just the financial industries that are susceptible, anomalies are prevalent in every single industry and can take many different forms — such as network intrusion, disturbances in business performances and abrupt changes in KPIs etc.
Fraud/anomaly/outlier detection has long been the subject of intense research in data science. In the ever-changing landscape of fraud detection, new tools and techniques are being tested and employed every day to screen out abnormalities. In this series of articles, so far I’ve discussed six different techniques for fraud detection:
Elliptic Envelope
Local Outlier Factor (LOF)
Z-score
Boxplot
Statistical techniques
Time series anomaly detection
Today I’m going to introduce another technique called DBSCAN — short for Density-Based Spatial Clustering of Applications with Noise.
As the name suggests, DBSCAN is a density-based and unsupervised machine learning algorithm. It takes multi-dimensional data as inputs and clusters them according to the model parameters — e.g. epsilon and minimum samples. Based on these parameters, the algorithm determines whether certain values in the dataset are outliers or not.
Below is a simple demonstration in Python programming language.
Scikit-learn has a DBSCAN module as part of its unsupervised machine learning algorithms. This algorithm can be used out of the box for fraud detection in only a few simple steps.
For this demo, we need three key libraries for data wrangling, visualization and modeling.
# data wranglingimport pandas as pd# visualizationimport matplotlib.pyplot as plt# algorithmfrom sklearn.cluster import DBSCAN
I am using the famous Iris dataset from an online source, so you can practice along without worrying about where to get the data from how to clean that up.
# import datadf = pd.read_csv("https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv")print(df.head())
Let’s choose a subset of data for testing the algorithm and visualize them in a scatter plot.
The two most important parameter values the model takes are (i) esp, which specifies the distance between two points i.e., how close the data points should be to one another to be considered part of a cluster; and (ii) min_samples, which specifies the minimum number of neighbors a point should have in a cluster.
# input datadata = df[["sepal_length", "sepal_width"]]# specify & fit modelmodel = DBSCAN(eps = 0.4, min_samples = 10).fit(data)
# visualize outputscolors = model.labels_plt.scatter(data["sepal_length"], data["sepal_width"], c = colors)
# outliers dataframeoutliers = data[model.labels_ == -1]print(outliers)
The purpose of this article was to introduce DBSCAN — a clustering-based unsupervised machine learning technique for fraud/outlier/anomaly detection. Its implementation can be as simple as taking just the five steps using thesklearn library. But of course, this is a simple demonstration of the concept. A real-world application would require much more experimentation to find the best model that works for a particular context and industry.