<jupyter_start><jupyter_text>Clustering-Hierarchical Clustering. Contents.1. Introduction1. Understanding a dendrogram and the agglomerative approach.    1. Identifying the clusters based on the dendrogram.1. Hierarchical Algorithm.1. Defining the Dissimilarity measure between two points.1. Choosing the Disimilarity Measure. Introduction.As seen in my earlier [post](https://github.com/GeorgeOduor/unsupervised-learning/blob/master/k-means%20clustering/CLUSTERING.ipynb) and [project](https://github.com/GeorgeOduor/unsupervised-learning/blob/master/k-means%20clustering/kmeans.ipynb) on k-means clustering,the k value for number of clusters have to be specified earlier.This property is not required in hierarchical clustering.This particular type of clustering method creates a tree based representation of observations called a _dendrogram_.Hierarchical clustering algorithms build a hierachy of clusters where each node is a cluster consists of the clusters of its daughter nodes.Hierachical clustering strategies generaly fall into teo types:1. Divisive (_Top down approach_)Start with all observations and break it down into smaller pieces.1. Agglomerative(_Bottom up approach_)Each observations starts the cluster and pair up as they move the hierachy.Agglomeration simply means to collect things.Agglomerative approach is very popular and works in a similar way which humans think. Understanding a dendrogram and the agglomerative approach.So how does it work?This method builds a hierachy from individual elements by progressively merging clusters.In a dendrogram,each leaf represents one of the datapoints.As we move up the tree some observations fuse together to form branches.These observations that fuse together are very similar to each other or _close_ to one another.Further up the tree ,branchesf fuse with other other branches or observations.The height of the fusions indicates how the two observations or clusters are different.Let me plot a simple plot of face data here to see how this comes about.<jupyter_code>#this code is here to help me hide some cells input
from IPython.display import HTML
import random

def hide_toggle(for_next=False):
    this_cell = """$('div.cell.code_cell.rendered.selected')"""
    next_cell = this_cell + '.next()'

    toggle_text = 'Toggle show/hide'  # text shown on toggle link
    target_cell = this_cell  # target cell to control with toggle
    js_hide_current = ''  # bit of JS to permanently hide code in current cell (only when toggling next cell)

    if for_next:
        target_cell = next_cell
        toggle_text += ' next cell'
        js_hide_current = this_cell + '.find("div.input").hide();'

    js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))

    html = """
        <script>
            function {f_name}() {{
                {cell_selector}.find('div.input').toggle();
            }}

            {js_hide_current}
        </script>

        <a href="javascript:{f_name}()">{toggle_text}</a>
    """.format(
        f_name=js_f_name,
        cell_selector=target_cell,
        js_hide_current=js_hide_current, 
        toggle_text=toggle_text
    )

    return HTML(html)

hide_toggle()


import seaborn as sns
import matplotlib.pyplot as plt
import scipy.cluster.hierarchy as sch
import pandas as pd

points1 = [[3,9],[4,8],[5,4]]
points = pd.DataFrame({"x":[-1.4,-0.9,-0.4,-0.1,0,0.3,1.2,1.4],
                  "y":[-1.6,-0.8,-1,-.4,-.2,.1,0,1]}).values
plt.figure(figsize=(17,4))
plt.subplot(1,2,1)
for i in range(len(points)):
    x = points[i][0]
    y = points[i][1]
    axes[1]=plt.plot(x, y, 'bo')
    plt.text(x * (1 + 0.01), y * (1 + 0.01) , i, fontsize=12)
plt.title("Scatter plot")
plt.xlabel("x")
plt.ylabel("y")

plt.subplot(1,2,2)
axes[0] =dendrogram = sch.dendrogram(sch.linkage(df.values,method='ward'))
plt.title("Dendrogram")
plt.xlabel("Datapoints")
plt.ylabel("Eucledian Distance")
plt.tight_layout(pad=2)
plt.show()
hide_toggle()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10379492334757477, 'fraction_numerical': 0.018094998743402865, 'mean_word_length': 4.1755526657997395, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '56000', 'license': 'MIT', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1204, 'n_words': 388, 'path': 'hierarchical clustering/hierachical clustering.ipynb', 'repo_name': 'GeorgeOduor/unsupervised-learning', 'size': '3979'}