Clustering: Why to Use it. As an new data science student who came... | by Robert Miller | Towards Data Science

As an new data science student who came from a no coding background, I like to explain complex topics in a simplified way. As if talking to myself before my program started. Well I hope you are all ready for some clustering.
Lets start with what is clustering? It is something that you have done, many times in fact. Its the act of looking for similarities and putting those similar points into a group (or cluster). Lets all think back to the last time we went out to eat. The first thing you want to find out is what type of food you want, Mexican, Chinese, Italian, ect. You are making clusters of multiple restaurants with those attributes.
Now to a more Data Science example, I am going to look at different attributes of seeds and see if clustering those attributes can help predict whether a seed is of a certain species or not.
Jumping right into the code, we need to import libraries in order to execute the following lines. I am also going to read in my dataset at the bottom
%matplotlib inline import pandas as pdimport numpy as npfrom sklearn import clusterfrom sklearn import metricsfrom sklearn.metrics import pairwise_distancesimport matplotlib.pyplot as pltimport matplotlibmatplotlib.style.use('ggplot') import seaborn as snsseeds = pd.read_csv("../assets/datasets/seeds.csv")
Just looking at the first few rows of my Dataframe, this is what it looks like. The code to look at this using pandas is seeds.head(), that will show the first 5 rows
seeds.species.nunique()
I then looked at the amount of unique values in my species column, which is are Target value column (the thing we are trying to predict). It turns out there are three species in our dataset
# Plot the Data to see the distributions/relationshipscols = seeds.columns[:-1]sns.pairplot(seeds, x_vars=cols, y_vars= cols, hue='species')
This is a scatter plot of how our different variables relate to each other, and the color (or hue which I set above) is each different species. So you can start to see that in general the seeds tend to cluster with their own species for the majority of our variables (predictors).
Since we have a Target value we could stop our clustering here, but many times that we will use clustering is when we don’t have a Target value. So I will drop our Target and see if our clustering will pick up on the differences and do a good job of predicting which seeds should be clustered together.
X = seeds.drop("species", axis = 1)from sklearn.metrics import pairwise_distancesfrom sklearn import cluster, datasets, preprocessing, metricsX_scaled = preprocessing.normalize(X,axis=0)
I dropped the Target value, I also imported a few more libraries from sklearn so I can normalize my data. Normalizing data is the process of organizing the attributes and relations of the database to scale all numeric variables in the range [0,1]. We do this so that columns like permiter with values of 15 don’t show more importance than columns like compactness which are below 1
from sklearn.cluster import KMeansk = 3kmeans = cluster.KMeans(n_clusters=k)kmeans.fit(X_scaled)
I am using kmeans clustering for this problem. It sets random centroids (center points for each group) and those centroids will continually shift until they are centered in a cluster of points to make the mean distance of all points as small as possible.
inertia = kmeans.inertia_print 'Silhouette Score:', metrics.silhouette_score(X_scaled, labels, metric='euclidean')
What I am doing above is looking at two different metrics to analyze how well our clustering method did. Inertia is the sum of squared error for each cluster. Therefore the smaller the inertia the denser the cluster(closer together all the points are)
The Silhouette Score is from -1 to 1 and show how close or far away the clusters are from each other and how dense the clusters are. The closer your silhouette score is to 1 the more distinct your clusters are. If your score is 1 think of your clusters as perfect little balls that are far away from each other with no miss classification.
Clustering can be used on many problems, whether you have a Target value or not, it is helpful to seek insights and see relationships.