Skip to main content

UML KMeans

UML KMeans

Published 2 min readMarkdown
# Step 1: Load and explore the dataset
import pandas as pd
df = pd.read_csv(r".\Mall_Customers.csv")
print(df.head())

#Step 2: Data Preprocessing
X = df[['Annual Income (k$)', 'Spending Score (1-100)']]

#Step 3: Perform K-means clustering
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

#Apply K-Means with k=3
kmeans = KMeans(n_clusters = 3, init = 'k-means++', random_state = 42)
y_kmeans = kmeans.fit_predict(X)

plt.scatter(X.iloc[:,0], X.iloc[:,1], c = y_kmeans, cmap = 'viridis')
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:, 1], s=200, c='red', marker='X')
plt.xlabel('Annual Income (k$)')
plt.ylabel('Spending Score (1-100)')
plt.title('Customer Segmentation using K-Means')
plt.show()

#Step 4: Compare random initialization and K-Means++ initialization
#Using random initialization
kmeans_random = KMeans(n_clusters=3, init='random', random_state=42)
y_kmeans_random = kmeans_random.fit_predict(X)

#Compare cluster centers
print("Cluster Centers (K-Means++):", kmeans.cluster_centers_)
print("Cluster Centers (Random):", kmeans_random.cluster_centers_)

#Choosing the Optimal Number of CLusters
#Elbow Method to determine the optimal number of clusters
inertia = []
for k in range(1, 11):
    kmeans = KMeans(n_clusters=k, init='k-means++', random_state=42)
    kmeans.fit(X)
    inertia.append(kmeans.inertia_)

#Plot the elbow curve
plt.plot(range(1, 11), inertia, marker='o')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Inertia')
plt.title('Elbow Method to Determine Optimal k')
plt.show()
#Here we can see that there is a huge change in the graph from the point 5 so the elbow point is 5

#EvaluationMetrics for Clustering
#ExplainSilhouette Score for evaluating clustering quality
#Compute the silhouette score for different values of k.
from sklearn.metrics import silhouette_score
for k in range(2, 10): #Here the range we are using doesn't make any significant difference
    kmeans = KMeans(n_clusters = k, init = 'k-means++', random_state = 42)
    y_kmeans = kmeans.fit_predict(X)
    score = silhouette_score(X, y_kmeans)
    print(f"Silhouette Score for k = {k}: {score}")

silhouette = silhouette_score(X, kmeans.labels_)
silhouette

No comments yet. Be the first to comment.