K-Means Clustering Project: Banknote Authentication | by John Chen (Yueh-Han) | Towards Data Science

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.
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.
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.
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.
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.
Step 4: Analyze the K-Means computing results
There are ~600 data being clustered in group 0, and ~773 data being clustering in group 1. For group 1, V1’s mean = ~-0.12 and V2’s mean = ~-3.5. while for group 2, V1’s mean = ~0.86 and V2’s mean = ~6.1.
Step 5: Calculate the accuracy of the result.
Firstly, let’s visualize the data with correct labels and the K-Means results:
We can see the K-Means one tends to be divided by a horizontal line at V2 = 1, whereas the original one tends to be divided by a slightly slant vertical line at V1 =0. Which showed one drawback of K-Means which is that K-Means gives more weight to the bigger clusters.(The group 1 in K-Means tends to include the bigger cluster in the below position.) Let’s calculate the accuracy of this K-Means clustering model:
0.6528081692195478
K-Means Result: The accuracy of this K-Means Model is 65.3%.
Nice! Since I didn’t conduct any data cleaning process and pre-analysis here, so getting an accuracy of 65.3% is quite reasonable. However, if you are interested in optimizing the accuracy rate, you can consider conducting factor analysis to find the most influential variables to put into the models. Then you will get a better result! (For readers who are interesting in factor analysis, I found DataCamp has a good introductory article on factor analysis in python.)
Thanks for spending your time reading this article.
If you’re an employer wanting to find a data science intern, here is my LinkedIn
If you’re also working in the data science/business analytics field, feel free to give me any feedback through IG(@jchen_mmm), by email, or comment below!