<jupyter_start><jupyter_text>K-Means Clustering- Plot the graph of k (number of clusters) vs SSE.- SSE is sum of squared distances of data points to their closest cluster center.- choose appropriate k where error is minimum or significantly not reducing further.<jupyter_code>#### Using the elbow method to find the optimal number of clusters
X = data.iloc[:, [3, 4]].values
Sum_of_squared_distances = []
for i in range(1, 11):
    km = KMeans(n_clusters = i)
    km.fit(X)
    Sum_of_squared_distances.append(km.inertia_) 
plt.plot(range(1, 11), Sum_of_squared_distances, '-bo')
plt.title('The ELbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('SSE')
plt.show()<jupyter_output><empty_output><jupyter_text>In the plot above the elbow is at k=5 indicating the optimal k for this dataset is 5<jupyter_code>#### Fitting Kmeans Clustering with k=5
km = KMeans(n_clusters = 5)
y_kmeans = km.fit_predict(X)
print("Cluster Centers :\n", km.cluster_centers_)
print("SSE :", km.inertia_)
#### Visualising the clusers
for i in range(0,5):
    plt.scatter(X[y_kmeans == i, 0],X[y_kmeans == i, 1], label = 'Cluster-'+str(i))
plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1], s = 60, c = 'Black', label = 'Centroids')
plt.legend()
plt.title('Clusters of customers')
plt.xlabel('Annual Income(K$)')
plt.ylabel('Spending Score (1-100)')
plt.show()
data['target'] = list(y_kmeans)
data.head(5)
data.to_csv('./Data/Mall_Customers_KMeans_5clusters.csv')<jupyter_output><empty_output><jupyter_text>Hierarchical ClusteringLinkage Methods:- "ward" minimizes the variance of the clusters being merged.- "average" uses the average of the distances of each observation of the two sets.- "complete" or maximum linkage uses the maximum distances between all observations of the two sets.- "single" uses the minimum of the distances between all observations of the two sets.<jupyter_code>#### Using the dendrogram to find the optimal number of clusters
import scipy.cluster.hierarchy as sch

plt.figure(figsize=(15,8))
dendrogram=sch.dendrogram(sch.linkage(X,method='complete'))

plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Euclidean distances')
plt.grid(False)
plt.show()
#### Fitting Hierarchical Clustering to the dataset
from sklearn.cluster import AgglomerativeClustering
hc=AgglomerativeClustering(n_clusters=5,affinity='euclidean',
                          linkage='complete')
y_hc=hc.fit_predict(X)
#### Visualising the clusters
for i in range(0,5):
    plt.scatter(X[y_hc==i,0],X[y_hc==i,1],s=50,label='Cluster-'+str(i))
    
plt.title('Clusters of customers')
plt.xlabel('Income')
plt.ylabel('Spending Score')
plt.legend()
plt.show()
data['target']=list(y_hc)
data.head(5)
data.to_csv('./Data/Mall_Customers_Hierarchical_5clusters.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1361519168756718, 'fraction_numerical': 0.01361519168756718, 'mean_word_length': 5.98, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '370107', 'license': 'MIT', 'n_tokens_mistral': 1028, 'n_tokens_neox': 956, 'n_words': 255, 'path': 'UnSupervised Learning/Clustering/MallCustomers_Clustering.ipynb', 'repo_name': 'Naga-kalyan/Machine_Learning', 'size': '2791'}