Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/deprecated/scripts/agglomDemo.py
1192 views
1
# Agglomerative Clustering Demo
2
# Author: Animesh Gupta
3
4
import superimport
5
6
import numpy as np
7
from scipy.cluster.hierarchy import dendrogram, linkage
8
import matplotlib.pyplot as plt
9
import pyprobml_utils as pml
10
11
X = np.array([[1,2],
12
[2.5,4.5],
13
[2,2],
14
[4,1.5],
15
[4,2.5],])
16
17
labels = range(1, 6)
18
plt.figure(figsize=(10, 6))
19
plt.yticks(np.linspace(0,5,11))
20
plt.ylim(0,5)
21
plt.grid(color='gray', linestyle='dashed')
22
plt.scatter(X[:,0],X[:,1], label='True Position')
23
24
for label, x, y in zip(labels, X[:, 0], X[:, 1]):
25
plt.annotate(
26
label,
27
xy=(x, y), xytext=(-3, 3),
28
textcoords='offset points', ha='right', va='bottom', fontsize=25, color="red")
29
pml.savefig("agglom_demo_data.pdf", dpi=300)
30
31
linked = linkage(X, 'single')
32
33
labelList = range(1, 6)
34
35
plt.figure(figsize=(10, 7))
36
dendrogram(linked,
37
orientation='top',
38
labels=labelList,
39
distance_sort='descending',
40
show_leaf_counts=True)
41
pml.savefig("agglom_demo_dendrogram.pdf", dpi=300)
42
43
plt.show()
44