BANA409BANA409

Clustering

mall_customers.csv.zip

online-retail.csv.zip

  • Supervised Learning: we have labeled target we want to predict, such as survival, churn, house price, etc.

    • Classification
    • Regression
  • Unsupervised learning: the data is not labeled

    • Clustering: group similar instances together into clusters
    • Association: find relationships between variables, such as “customers who bought this also bought that”
    • Dimensionality reduction: reduce the number of features while preserving the data integrity.

    The intuition of the clustering algorithm is quite simple - it is trying to achieve:

    • Maximum similarity between records within a cluster

    • Maximum dissimilarity between records of different clusters

      81332322-eae78600-9070-11ea-9be3-6e5af182da9e.png

    K-Means Algorithm

    K-means clustering is a center-based clustering method that groups the data points in k exclusive clusters.

    • Each cluster is represented by a central data instance, called Centroid (the blue triangles below)
    • Each data instance belongs to one exclusive cluster

    A Voronoi Diagram for K-Means Clustering Result:

    81335082-081e5380-9075-11ea-937f-c9cec4c65abc.png

    Centroid

    Centroid is essentially the "mean" of the data points, therefore K-Means

    import matplotlib.pyplot as plt 
    plt.style.use('seaborn')
    # Centroid is essentially the "mean" of the data points, therefore K-Means
    fig, ax = plt.subplots()
    ax.plot(1, 2, 'o', markersize=10)
    ax.plot(3, 4, 'o', markersize=10)
    ax.plot(4, 3, 'o', markersize=10)
    ax.plot((1+3+4)/3, (2+4+3)/3, '^', markersize=15) # the centroid

    Screen Shot 2022-05-10 at 2.33.50 PM.png

    Distance Metrics

    Different metrics can be used to measure the distance among data points, such as the Manhattan Distance and Euclidean Distance.

    81337844-ac09fe00-9079-11ea-844a-cbff46eab86a.png

    In Cartesian coordinates, if p=(p1,p2,...,pn)\mathbf{p} = (p_1, p_2,..., p_n) and q=(q1,q2,...,qn)\mathbf{q} = (q_1, q_2,..., q_n) are two points in Euclidean n-space, then the Euclidean distance (dd) from p\mathbf{p} to q\mathbf{q} is given by following formula:

    d(p,q)=d(q,p)=(q1p1)2+(q2p2)2++(qnpn)2=i=1n(qipi)2.{\begin{aligned} d(\mathbf {p} ,\mathbf {q} )=d(\mathbf {q} ,\mathbf {p} )&={\sqrt {(q_{1}-p_{1})^{2}+(q_{2}-p_{2})^{2}+\cdots +(q_{n}-p_{n})^{2}}}\\[8pt]&={\sqrt {\sum _{i=1}^{n}(q_{i}-p_{i})^{2}}}.\end{aligned}}

    See more at https://en.wikipedia.org/wiki/Euclidean_distance

    Key Steps of K-Means Algorithm

    Step 1: Initiate k random centroids (k specified by the user, which is 3 in the following example)

    81339165-ba591980-907b-11ea-9cec-206f7fbaf625.png

    Step 2: Assign data points to the nearest centroid to form a cluster

    81339260-db216f00-907b-11ea-9530-a28c34cdf643.png

    Step 3: Calculate the new centroids for each cluster

    81339344-0310d280-907c-11ea-9b2b-da7d5d4d4cfd.png

    Step 4: Repeat assignment to the new centroids. Assign data points to the nearest new centroid to form a cluster

    81342786-d069d880-9081-11ea-9a38-e0f4691a0b67.png

    Step 5: Repeat Steps 3 and 4 until no further change in data point assignments

    [Source]

    Smarter Initialization

    Instead of random assigning the initial centroids as discussed above, a smarter initialization algorithm was proposed in K-Means++, which tends to select centroids that are distant from one another and is able to speed up the convergence of K-Means. K-means++ is the default initialization algorithm in Scikit-Learn.

    Evaluation Metric

    • How do we know what K value to choose?
    • How do we compare different clustering solutions?

    Inertia

    The evaluation metric is called inertia, which is the sum of mean squared distance between each instance and its closest centroid.

    The smaller the inertia the denser the cluster (the points in the same cluster are closer to each other) and the K-means algorithm aims to minimize inertia.

    More formally, the k-means algorithm divides a set of n samples X into disjoint k clusters C, each described by the mean μj\mu_j (centroid) of the cluster.

    i=0nminμjC(xiμj2)\sum_{i=0}^{n}\min_{\mu_j \in C}(||x_i - \mu_j||^2)

    We then try a number of K values and plot the corresponding inertia and choose the value at the "elbow" of the plot.

    81519274-ac6ef700-930e-11ea-91cd-d3954b1828d2.png

    Silhouette Score (Optional)

    The silhouette value is a measure of how similar an object is to its own cluster (cohesion) compared to other clusters (separation).

    81510396-5e8eca80-92df-11ea-8235-ebf33521e25e.png

    [Image Source]

    For each data point iCii\in C_{i} , let

    • a(i)a(i) is the mean distance between i and all other points in the same cluster.

      a(i)=1Ci1jCi,ijd(i,j)a(i)={\frac {1}{|C_{i}|-1}}\sum _{j\in C_{i},i\neq j}d(i,j)
    • b(i)b(i) is the mean distance between i and all points its closest (therefore the nin here) neighboring cluster.

      b(i)=minki1CkjCkd(i,j)b(i)=\min _{k\neq i}{\frac {1}{|C_{k}|}}\sum _{j\in C_{k}}d(i,j)

    The Silhouette Score is:

    Screen Shot 2022-05-05 at 2.30.11 PM.png

    From the following silhouette score plot, you can clearly see that k=5 is the best.

    81519278-ae38ba80-930e-11ea-8f7f-1eeda3ca78f3.png

    If you plot the silhouette score for each data instance within each cluster, you get the silhouette diagram as shown blow (each cluster is a sort of "silhouette").

    This is how you read Silhouette Diagram:

    • best silhouette score for each data point is 1, so the "longer" the silhouette, the better

    • the red line is the mean silhouette for the k value, it's bad for a silhouette not reaching the red line, which means the points in that cluster should belong to other clusters

    • it's better to have silhouette with similar sizes, which means the data points are more evenly clustered

      81519280-b0027e00-930e-11ea-982b-423431b2ce12.png

Customer Segmentation Analysis

On this page