-
Notifications
You must be signed in to change notification settings - Fork 0
/
prodology_2.py
47 lines (39 loc) · 1.58 KB
/
prodology_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
data = pd.read_csv("path/to/Mall_Customers.csv")
# Extracting the features for clustering (Annual Income and Spending Score)
X = data.iloc[:, [3, 4]].values
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Finding the optimal number of clusters using the Elbow Method
wcss = []
max_clusters = 10
for i in range(1, max_clusters + 1):
kmeans = KMeans(n_clusters=i, init='k-means++', random_state=42)
kmeans.fit(X_scaled)
wcss.append(kmeans.inertia_)
# Plotting the Elbow Method graph
plt.figure(figsize=(10, 5))
plt.plot(range(1, max_clusters + 1), wcss, marker='o', linestyle='--')
plt.title('Elbow Method')
plt.xlabel('Number of Clusters')
plt.ylabel('WCSS')
plt.show()
# Choose the optimal number of clusters based on the Elbow Method
optimal_num_clusters = 10
# Fitting K-Means to the dataset with the optimal number of clusters
kmeans = KMeans(n_clusters=optimal_num_clusters, init='k-means++', random_state=42)
y_kmeans = kmeans.fit_predict(X_scaled)
plt.figure(figsize=(10, 6))
for cluster_label in np.unique(y_kmeans):
plt.scatter(X_scaled[y_kmeans == cluster_label, 0], X_scaled[y_kmeans == cluster_label, 1],
s=100, label=f'Cluster {cluster_label + 1}')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='yellow', label='Centroids')
plt.title('Clusters of customers')
plt.xlabel('Scaled Annual Income')
plt.ylabel('Scaled Spending Score')
plt.legend()
plt.show()