<jupyter_start><jupyter_text>AGNES worked exampleWe use the distance data that was presented in class. Note that the scipy hierarchical clustering has more options and can be used to draw a dendrogram.It has the feature that it expects a condensed (1-d) version of the distance matrix.<jupyter_code>dataDir = "data"
# Make sure the outputDir subdirectory exists
outputDir = "output/Practical_E_AGNES"
import os, errno
try:
    os.makedirs(outputDir)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise
import numpy as np

distMatrix = np.array([
    (0,  9,  3, 6, 11),
    (9,  0,  7, 5, 10),
    (3,  7,  0, 9, 2),
    (6,  5,  9, 0, 8),
    (11, 10, 2, 8, 0)
])
condensed = [9, 3, 6, 11, 7, 5, 10, 9, 2, 8]<jupyter_output><empty_output><jupyter_text>We now generate the different clustering an their associated dendrograms for this data.<jupyter_code>import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
import time

%matplotlib inline

algName = "AgglomerativeClustering"
labels = ('A','B','C','D','E')
for link in ['complete', 'single', 'average', 'ward']:
    plt.figure()
    start_time = time.time()
    Z = linkage(condensed, method=link)
    end_time = time.time()
    plt.figure(figsize=(6, 4))
    plt.title(algName+" : "+link)
    R = dendrogram(Z, labels=labels, truncate_mode=None)
    plt.savefig(outputDir + '/AGNES'+link+'.pdf')
    elapsed_time = end_time-start_time
#    print(elapsed_time)<jupyter_output><empty_output><jupyter_text>Because there is relatively little data, the choice of linkage has less dramatic effects.We can choose to "cut" the tree at different heights. For example, cutting the single linkage at a distance of 4, we see an ACE cluster and two singleton clusters B and D.As we did in the class notes, we also consider how the distance matrix can be used to define some points. We use MultiDimensional Scaling, mapping the points into the x-y plane. Note that this embedding is _not unique_. You can shift (translate) and rotate the points to get a different embedding which still has the same between-point distances.The sklearn MDS class can work with observation-feature data or distance-distance data. We use the latter here (i.e., the distance matrix has been `precomputed`). Note that we can transform into any number of dimensions, but 2 is handy for putting a scatter plot on the screen!<jupyter_code>from sklearn.manifold import MDS

embedding = MDS(n_components=2, dissimilarity='precomputed')
X = embedding.fit_transform(distMatrix)

plt.scatter(X[:,0], X[:,1], s=100, c='orange')
plt.axis('square')
plt.title('MDS: Embedded points in 2-D')
plt.savefig(outputDir + '/AGNES_MDS.pdf')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10313075506445672, 'fraction_numerical': 0.019521178637200737, 'mean_word_length': 4.6, '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': '119952', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 930, 'n_tokens_neox': 862, 'n_words': 331, 'path': 'topics/08-Clustering/02-Practical/files/Practical_E_AGNES.ipynb', 'repo_name': 'DataMining-2021S2/live', 'size': '2715'}