-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHierarchicalClustering.py
More file actions
26 lines (20 loc) · 902 Bytes
/
HierarchicalClustering.py
File metadata and controls
26 lines (20 loc) · 902 Bytes
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
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram,linkage
X,_=make_blobs(n_samples=300,centers=4,cluster_std=1,random_state=42)
# plt.figure()
# plt.scatter(X[:,0],X[:,1])
linkage_methods=["ward","single","average","complete"]
for i , linkage_method in enumerate(linkage_methods,1):
model = AgglomerativeClustering(n_clusters=4,linkage=linkage_method)
cluster_labels= model.fit_predict(X)
plt.subplot(2,4,i)
plt.title(f"{linkage_method.capitalize()}Linkage Dendrogram")
dendrogram(linkage(X,method=linkage_method),no_labels=True)
plt.xlabel("veri noktaları")
plt.ylabel("uzaklık")
plt.subplot(2,4,i+4)
plt.scatter(X[:,0],X[:,1],c=cluster_labels,cmap="viridis")
plt.title(f"{linkage_method.capitalize()}Linkage Clustering")
plt.show()