<jupyter_start><jupyter_text>Hierachical Models Assignment<jupyter_code># import pandas
import pandas as pd

# import Agglomerative clustering
from sklearn.cluster import AgglomerativeClustering

# import scipy.cluster.hierarchy
import scipy.cluster.hierarchy as sch

# import numpy
import numpy as np

# import matplotlib
import matplotlib.pyplot as plt

# set figure size
plt.rcParams["figure.figsize"] = (12,8)
# Load dataframe
df = pd.read_csv('https://raw.githubusercontent.com/lucko515/clustering-python/master/Customer%20in%20Mall%20clusterng/Mall_Customers.csv')<jupyter_output><empty_output><jupyter_text>Objective: - Fit Hierarchical clustering, - find optimal number of clusters via dendrogram - and plot clusters. Again, to simplify plotting in 2-D, we can create clustering model based on the last two columns of the dataset only.<jupyter_code>df<jupyter_output><empty_output><jupyter_text>* define function plot_dendogram<jupyter_code>X = df[["Annual Income (k$)","Spending Score (1-100)"]].to_numpy()
def plot_dendrogram(X,method='ward'):
    dendrogram = sch.dendrogram(sch.linkage(X, method=method))
    plt.title("Dendrogram")
    plt.ylabel("Euclidean distances")
    plt.xlabel('Points')
    plt.show()<jupyter_output><empty_output><jupyter_text>* Plot dendrogram<jupyter_code>plot_dendrogram(X)<jupyter_output><empty_output><jupyter_text>* Fit Hierarchical clustering with optimal number of clusters<jupyter_code>ac = AgglomerativeClustering(affinity='euclidean',
                             linkage='ward', # ward, maximum, average, single
                             n_clusters = 3) # the rules<jupyter_output><empty_output><jupyter_text>* define function plot_clusters<jupyter_code>y = ac.fit_predict(X)
def plot_clusters(X,y_res, plt_cluster_centers = False):
    X_centroids = []
    Y_centroids = []

    for cluster in set(y_res):
        x = X[y_res == cluster,0]
        y = X[y_res == cluster,1]
        X_centroids.append(np.mean(x))
        Y_centroids.append(np.mean(y))

        plt.scatter(x,
                    y,
                    s=50,
                    marker='s',
                    label=f'cluster {cluster}')

    if plt_cluster_centers:
        plt.scatter(X_centroids,
                    Y_centroids,
                    marker='*',
                    c='red',
                    s=250,
                    label='centroids')
    plt.legend()
    plt.grid()
    plt.show()<jupyter_output><empty_output><jupyter_text>* Plot HC clusters<jupyter_code>plot_clusters(X,y,plt_cluster_centers=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup