-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathagglomerative.py
73 lines (55 loc) · 1.67 KB
/
agglomerative.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""Agglomerative.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XOAMbRJuXMwgCJqKwBwwfCi8GoxrJNCS
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
from google.colab import files
uploaded = files.upload()
df = pd.read_csv('agglodata.csv', sep=',', header=None)
print(df.values)
arr = np.array(df.values)
#X = 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],])
X = np.array(df.values)
import matplotlib.pyplot as plt
labels = range(1, 6)
plt.figure(figsize=(10, 3))
plt.subplots_adjust(bottom=0.1)
plt.scatter(X[:,0],X[:,1], label='True Position')
for label, x, y in zip(labels, X[:, 0], X[:, 1]):
plt.annotate(
label,
xy=(x, y), xytext=(-3, 3),
textcoords='offset points', ha='right', va='bottom')
plt.show()
linked = linkage(X, 'single')
labelList = range(1, 6)
plt.figure(figsize=(10, 3))
dendrogram(linked,
orientation='top',
labels=labelList,
distance_sort='descending',
show_leaf_counts=True)
plt.show()
linked = linkage(X, 'complete')
labelList = range(1, 6)
plt.figure(figsize=(10, 3))
dendrogram(linked,
orientation='top',
labels=labelList,
distance_sort='descending',
show_leaf_counts=True)
plt.show()
linked = linkage(X, 'average')
labelList = range(1, 6)
plt.figure(figsize=(10, 3))
dendrogram(linked,
orientation='top',
labels=labelList,
distance_sort='descending',
show_leaf_counts=True)
plt.show()