"""
<p style="color:purple; font-size:50px; font-weight:bold; text-align:center;">Clustering using Agglomerative Hierarchical Clustering Algorithm</p>
"""
"""
I will start coding directly. I hope you have some basic knowledge of Hierarchical clustering algorithm. If you want to learn, then check it out [here.](https://www.javatpoint.com/hierarchical-clustering-in-machine-learning)
"""
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Importing libraries</span>
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Loading dataset</span>
"""
df = pd.read_csv('../input/mall-customer/Mall_Customers.csv')
df.head()
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Storing input data in a variable</span>
"""
x = df.iloc[:, [3,4]].values
x
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Draw a Dendrogram</span>
"""
import scipy.cluster.hierarchy as shc  
dendro = shc.dendrogram(shc.linkage(x, method="ward"))  
plt.title("Dendrogrma Plot")  
plt.ylabel("Euclidean Distances")  
plt.xlabel("Customers")  
plt.show()
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Train the model using sklearn library</span>
"""
from sklearn.cluster import AgglomerativeClustering
model = AgglomerativeClustering(n_clusters = 5)
model.fit(x)
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Predict the clustering</span>
"""
y_pred = model.fit_predict(x)
y_pred
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Visualize the clustering</span>
"""
plt.scatter(x[y_pred == 0, 0], x[y_pred == 0, 1], s = 100, c = 'blue', label = 'Cluster 1')  
plt.scatter(x[y_pred == 1, 0], x[y_pred == 1, 1], s = 100, c = 'green', label = 'Cluster 2')  
plt.scatter(x[y_pred== 2, 0], x[y_pred == 2, 1], s = 100, c = 'red', label = 'Cluster 3')  
plt.scatter(x[y_pred == 3, 0], x[y_pred == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')  
plt.scatter(x[y_pred == 4, 0], x[y_pred == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')
plt.title('Clusters of customers')  
plt.xlabel('Annual Income (k$)')  
plt.ylabel('Spending Score (1-100)')  
plt.legend()  
plt.show()  
"""
<span style = "color:green; font-size:25px; font-weight:bold;">Conclusion : </span>I have successfully built a model of clustering.
<br><br><br><br>
<center>Thank you</center>
"""