K-Means Clustering Project: Banknote Authentication
By: John Chen, Yueh-Han
Tags: Machine Learning, Data, Unsupervised Learning, Data Mining, Data Science

K-Means Clustering Project: Banknote Authentication

Using Python (Pandas, NumPy) to gather and assess the data and scikit-learn to train a K-Means model to detect if a banknote is genuine or forged. John Chen (Yueh-Han) Jun 1·5 min read

Have you ever been in a situation where you were handing money to the clerks at a supermarket only to find that the money is fake while there was a long line of people behind you waiting to check out? Or even more embarrassing, you didn’t carry other banknotes? I personally had experienced this situation one time and that embarrassment of being assumed to be an immoral cheapskate just stuck in my head for a long time. This motivated me to conduct this project, building a K-Means Clustering model to detect if a banknote is real or fake.

Photo by Ystallonne Alves on Unsplash

Dataset Overview:

This dataset is about distinguishing genuine and forged banknotes. Data were extracted from images that were taken from genuine and forged banknote-like specimens. For digitization, an industrial camera usually used for print inspection was used. The final images have 400x 400 pixels. Due to the object lens and distance to the investigated object gray-scale pictures with a resolution of about 660 dpi were gained. A Wavelet Transform tool was used to extract features from these images. (Source: https://www.openml.org/d/1462)

Since I am a beginner in the ML world, I tried to make this project as simple as possible and only focused on running the K-Means model and calculating the result. Thus, for the purpose of focusing on K-Means itself, I only picked out two variables to build the models, which are V1 (variance of Wavelet Transformed image) and V2 (skewness of Wavelet Transformed image).

Alright, before diving into the project, let me walk you through every step in this project:

Step 1: Gather and Assess the data. (Full code) Step 2: Run K-Means.(Full code) Step 3: Re-run K-means several times to to see if we get similar results, which can tell if the K-Means model is stable or not. (Full code) Step 4: Analyze the K-Means computing results Step 5: Calculate the accuracy of the result! (Full code)

Let’s get started!

Step 1: Gather and Assess the data.

Image by author

The first step in building K-Means is to assess if this dataset is suitable for K-Means; if not, then we should choose other clustering models. After seeing this plot, I found the data distribution in the graph is neither too wide, nor too centered at one place, therefore it is worth trying to computing K-Means on this dataset. But, there is no obvious cluster in spherical shapes so we should expect the K-Means model won’t work perfectly here.

Step 2: Run K-Means.

Image by author

The start signs are the centroid of each cluster. And this graph seems to work fine.

Step 3: Re-run K-means several times to to see if we get similar results, which can make sure the K-Means model is stable in the dataset.

The section of the code is inspired by Dabbura, the author of this famous K-Means article.

Image by author

My third step is to run K-Means several times since K-Means will be randomly choosing initial places to be centroids, and then they will be changing their places according to the average distances from the members of each cluster, which will be set as the new centroids. Therefore, we normally will get different results every time we rerun K-Means, but if the results are too different among many tests, then it means that K-Means might not be suitable for this dataset, since it is not stable. Here, after running K-Means 9 times, the results we got are very similar, which means the K-Means here are stable.