How to Cluster Data!. A guide to Unsupervised Machine... | by Mubarak Ganiyu | Towards Data Science

When working on data science projects, data scientists often encounter datasets that have unlabeled data points. Therefore, making it hard to derive insightful knowledge that can be used to model the behavior of the data. In order to make sense out of senseless data, unsupervised machine learning techniques are deployed to label the data points as well as provide a clear contrast between the different classes.
A remarkable unsupervised machine learning technique is called clustering. Clustering is a great mechanism for grouping unlabeled data into classes. It operates by examining the entire dataset to find similarities amongst the relationships of the variables of data points. After noticing certain similarities, it is possible to split the dataset into multiple groups in which group members possess similar attributes. K-means Clustering is the type of clustering that will be explored in this article.
Scikit-learn is a machine learning library that is available through Python. It can be used to build machine learning algorithms to make accurate predictions. The K-means clustering model was imported via scikit-learn using the code below.
from sklearn.cluster import KMeans
A dataset was needed to utilize the K-means clustering model. Thus, a dataset on the goodreads books’ ratings was downloaded from Kaggle and uploaded on Jupyter Notebook. Then, numerical variables such as average rating, number of pages, ratings count and text reviews count were extracted. Using those four variables, it was possible to fit the K-means cluster on the dataset.
The next step was to figure out the number of clusters (groups) that would be optimal to divide the dataset into reasonable groups. By trying out different numbers of clusters, it was possible to calculate their inertias. The inertia of a K-means cluster is a measure of how coherent and distinctive the clusters are. By plotting a graph of inertia vs number of clusters, the optimal number of clusters is chosen at the point that creates an elbow-like structure on the graph. This is done because increasing the number of clusters beyond the elbow point does not change the value of the inertia by a lot and it ends up creating unnecessary additional clusters/groups.
Therefore, four clusters were chosen as the number of clusters. Then, it was fitted on the goodreads book dataset in order to attribute labels to data points.
kmeans_book = KMeans(n_clusters = 4)kmeans_book.fit_predict(books_df)
By selecting four clusters, four centers that ideally represent the each cluster are created. Then, each data point’s distance is measured from the centers and the data point is labelled based on its nearest cluster center. The four cluster centers can be viewed below.
K-means clustering can also be used as a method of pre-processing when building a machine learning algorithm. This can be done by creating a pipeline in which a K-means cluster is applied on the independent variables to measure the distance of each data point from the cluster centers. Then, a classifier can be applied to label the data based on the distance of the data points to the cluster centers.
A use case of K-means clustering being a pre-processor can be found in the classification of the quality of red wine. First of all, the dataset was split into a training and testing dataset and a pipeline was created with a K-means cluster of two clusters and a decision tree classifier.
from sklearn.model_selection import train_test_splitx_train, x_test, y_train, y_test = train_test_split(x, y, random_state = 10, train_size = 0.8)pipeline = Pipeline([("kmeans", KMeans(n_clusters = 2)), ("dec", DecisionTreeClassifier())])
The pipeline code shown above can be fitted on the training data and it was used to make predictions on the independent variables of the testing data.
pipeline.fit(x_train, y_train)y_pred = pipeline.predict(x_test)
The cluster centers can be viewed in the image below.
The accuracy score of the pipeline was approximately 0.74.
Clustering provides a means for data scientists to extract insightful information from meaningless datasets. By clustering the dataset, it can be labelled and these labels can be used to model the behavior of the data points or create visualization charts to effectively distinguish different points on the dataset. In the midst of chaotic confusion that is brought forth by unlabeled datasets, K-means clustering is a great approach to establish structure to these datasets.
The link to the code that was used for this article can be viewed here.
Thank you very much for reading!