-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustering_optimal_algos.py
177 lines (130 loc) · 5.02 KB
/
clustering_optimal_algos.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from numpy.lib import nanfunctions
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#import plotly_express as px
import plotly.graph_objs as go
#import plotly.plotly as py
from sklearn.decomposition import PCA
import numpy as np
import networkx as nx
from tqdm.notebook import tqdm
import pickle
import time
from numpy import unique
from numpy import where
from matplotlib import pyplot
from sklearn import metrics
#from clusteval import clusteval
#--------------------------------------------------------------------------------------
def cluster_with_affinity_propagation(X, damping):
from sklearn.cluster import AffinityPropagation
model = AffinityPropagation(damping=damping)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def agglomerative_clustering(X, n):
from sklearn.cluster import AgglomerativeClustering
model = AgglomerativeClustering(n_clusters=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def cluster_with_birch(X, n):
from sklearn.cluster import Birch
model = Birch(threshold=0.01, n_clusters=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def cluster_with_kmeans(X, n):
from sklearn.cluster import KMeans
model = KMeans(n_clusters=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def cluster_with_mini_batch_kmeans(X, n):
from sklearn.cluster import MiniBatchKMeans
model = MiniBatchKMeans(n_clusters=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def spectral_clustering(X, n):
from sklearn.cluster import SpectralClustering
model = SpectralClustering(n_clusters=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def mean_shift_clustering(X):
from sklearn.cluster import MeanShift
model = MeanShift()
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def plot_with_annotations(graph):
import numpy as np
import matplotlib.pyplot as plt
df = apply_pca(make_df(graph))
df = list(df)
x = [i[0] for i in df]
y = [i[1] for i in df]
graph = sa_la_graph
df = pd.DataFrame(list(graph.items()))
df.rename(columns = {0:'word', 1:'vector'}, inplace = True)
annotations = list(df['word'])
plt.figure(figsize=(30,15))
plt.scatter(x, y,s=100,color="red")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Scatter Plot with annotations",fontsize=15)
for i, label in enumerate(annotations):
plt.annotate(label, (x[i], y[i]))
plt.show()
def cluster_with_optics(X, n):
from sklearn.cluster import OPTICS
model = OPTICS(eps=0.8, min_samples=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def cluster_with_dbscan(X, n):
from sklearn.cluster import DBSCAN
model = DBSCAN(eps=0.30, min_samples=n)
model.fit(X)
labels = model.labels_
sil_score = metrics.silhouette_score(X, labels, metric="sqeuclidean")
return sil_score, (sil_score+1)/2
def plot_clusters(df):
'''not in use'''
pca = PCA().fit(df)
pcaratio = pca.explained_variance_ratio_
trace = go.Scatter(x=np.arange(len(pcaratio)),y=np.cumsum(pcaratio))
data = [trace]
layout = dict(title="Results")
fig = dict(data=data, layout=layout)
pca = PCA(n_components=5)
sPCA = pca.fit_transform(df)
print("info retained: ", pca.explained_variance_ratio_)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=6)
sPCA_labels = kmeans.fit_predict(sPCA)
dfPCA = pd.DataFrame(sPCA)
dfPCA['cluster'] = sPCA_labels
from sklearn.manifold import TSNE
X = dfPCA.iloc[:,:-1]
Xtsne = TSNE(n_components=2).fit_transform(X)
dftsne = pd.DataFrame(Xtsne)
dftsne['cluster'] = sPCA_labels
dftsne.columns = ['x1','x2','cluster']
fig = plt.plot(figsize=(10, 6))
sns.scatterplot(data=dftsne,x='x1',y='x2',hue='cluster',legend="full",alpha=0.5)#,ax=ax[0])
#fig.title('Hindi-German')
#sns.scatterplot(data=dfsPCA2,x='x1',y='x2',hue='cluster',legend="full",alpha=0.5,ax=ax[1])
#ax[1].set_title('Visualized on PCA 2D')
#fig.suptitle('Comparing clustering result when visualized using TSNE2D vs. PCA2D')
display(fig)