-
Notifications
You must be signed in to change notification settings - Fork 0
/
hierarchical.py
79 lines (67 loc) · 2.45 KB
/
hierarchical.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
73
74
75
76
77
78
79
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import time
import scipy.cluster.hierarchy as shc
from matplotlib.pyplot import figure #scale the view
from sklearn.preprocessing import StandardScaler, normalize
from sklearn import datasets
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import f1_score
from warnings import filterwarnings
from datetime import date
dataImport = pd.read_csv('winequality-red.csv')
print("successfully added data!")
dataImport.head()
# print(dataImport.head())
# next step is normalize all the data to be the same scale
scale = normalize(dataImport)
scale = pd.DataFrame(scale, columns=dataImport.columns)
scale.head()
print(scale.head())
# SINGLE INTER-CLUSTER DISTANCE
#create dendograms with SINGLE inter-cluster distance
plt.figure(figsize=(16, 8))
plt.title("Dendograms")
dendogram = shc.dendrogram(shc.linkage(scale, method='single'))
plt.axhline(y=6, color='r', linestyle='--')
plt.show()
#create clusters COMPLETE inter-cluster distance
cluster = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='single')
cluster.fit_predict(scale)
print(cluster.fit_predict(scale))
# cluster dots
plt.figure(figsize=(16, 8))
plt.scatter(scale['quality'], scale['alcohol'], c=cluster.labels_)
plt.show()
# COMPLETE INTER-CLUSTER DISTANCE
#create dendograms with COMPLETE inter-cluster distance
plt.figure(figsize=(16, 8))
plt.title("Dendograms")
dendogram = shc.dendrogram(shc.linkage(scale, method='complete'))
plt.axhline(y=0.5, color='r', linestyle='--')
plt.show()
#create clusters SINGLE inter-cluster distance
cluster = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='complete')
cluster.fit_predict(scale)
print(cluster.fit_predict(scale))
# cluster dots
plt.figure(figsize=(16, 8))
plt.scatter(scale['quality'], scale['alcohol'], c=cluster.labels_)
plt.show()
# AVERAGE INTER-CLUSTER DISTANCE
#create dendograms with AVERAGE inter-cluster distance
plt.figure(figsize=(16, 8))
plt.title("Dendograms")
dendogram = shc.dendrogram(shc.linkage(scale, method='average'))
plt.axhline(y=0.5, color='r', linestyle='--')
plt.show()
#create clusters AVERAGE inter-cluster distance
cluster = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='average')
cluster.fit_predict(scale)
print(cluster.fit_predict(scale))
# cluster dots
plt.figure(figsize=(16, 8))
plt.scatter(scale['quality'], scale['alcohol'], c=cluster.labels_)
plt.show()