<jupyter_start><jupyter_text>Segmentation using Age and Spending Score<jupyter_code>X = df[["Age", "Spending Score (1-100)"]]
X.head()
import scipy.cluster.hierarchy as shc<jupyter_output><empty_output><jupyter_text>Using just 5 points initially for better undersatnding<jupyter_code>agglo_clusters_5 = shc.linkage(X.iloc[:5,:])
agglo_clusters_5
# return a (n-1)x4 matrix where first two columns contain the cluster(point) id(index) to form the next cluster.
# 3rd column represents the distance between the merging clusters.
# 4th column represents number of original observations in the newly formed cluster.
pd.DataFrame(agglo_clusters_5, columns = ["Cluster_id1", "Cluster_id2", "distance", "points_in_new_cluster"])
plt.title("Customer Dendrogram")
dend = shc.dendrogram(agglo_clusters_5)<jupyter_output><empty_output><jupyter_text>Using the complete data<jupyter_code>agglo_clusters = shc.linkage(X,method = "ward")
agglo_clusters
agglo_clusters.shape
plt.figure(figsize = (12,8))
plt.title("Customer Dendrogram")
dend = shc.dendrogram(shc.linkage(X, method = "ward"))<jupyter_output><empty_output><jupyter_text>Implementation using sklearn<jupyter_code>from sklearn.cluster import AgglomerativeClustering 
agglo_clustering = AgglomerativeClustering(n_clusters = 4, affinity = "euclidean", linkage = "ward")
agglo_clustering.fit(X)
labels = agglo_clustering.labels_
labels
plt.figure(figsize = (9,7))
plt.scatter(X.values[labels == 0, 0], X.values[labels == 0, 1], s = 50, c = 'red', label = 'Not old Average Spenders')
plt.scatter(X.values[labels == 1, 0], X.values[labels == 1, 1], s = 50, c = 'blue', label = 'Not Young Average Spenders')
plt.scatter(X.values[labels == 2, 0], X.values[labels == 2, 1], s = 50, c = 'green', label = 'Low Spenders')
plt.scatter(X.values[labels == 3, 0], X.values[labels == 3, 1], s = 50, c = 'maroon', label = 'Young High Spenders')
plt.title('Clusters of customers')
plt.xlabel('Age')
plt.ylabel('Spending Score (1-100)')
plt.legend()
plt.show()
from scipy.spatial import distance_matrix 
dist = distance_matrix(X, X)
dist<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13944603629417382, 'fraction_numerical': 0.02387774594078319, 'mean_word_length': 6.9961832061068705, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '661336', 'license': 'MIT', 'n_tokens_mistral': 816, 'n_tokens_neox': 759, 'n_words': 179, 'path': '08 Machine Learning/Clustering/Hierarchical Clustering.ipynb', 'repo_name': 'Anil-matcha/ISTE-Examly-Sessions', 'size': '2094'}