-
Notifications
You must be signed in to change notification settings - Fork 0
/
22ID60R20_OCHC-DS.py
348 lines (244 loc) · 12.1 KB
/
22ID60R20_OCHC-DS.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# 22ID60R20: Gajendra Singh
# Project Code: OCHC-DS
# Project Title: Obesity Data Categorization using Single Linkage Divisive (Top-Down) Clustering Technique
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy.linalg import norm
import random
# Import the data
df = pd.read_csv('obesity.csv')
######################## Data Preprocessing ########################
# df.head()
# df.info()
# df.shape
# df.describe()
#print(df.isna().sum().sort_values(ascending = False)) # No NA value found after printing
df=df.drop_duplicates() # Delete all the duplicate rows considering all column wise.
print("Final shape of data after preprocessing", df.shape, '\n') # final shape of datadata
df['CAEC'].value_counts() # converting categorical data to numeric data
df = pd.get_dummies(df, columns=['CAEC'])
df['Gender'].value_counts() # converting categorical data to numeric data
df['Gender'].replace(['Male', 'Female'],[1, 0], inplace=True)
df['family_history_with_overweight'].value_counts() # converting categorical data to numeric data
df['family_history_with_overweight'].replace(['yes', 'no'],[1, 0], inplace=True)
df['FAVC'].value_counts() # converting categorical data to numeric data
df['FAVC'].replace(['yes', 'no'],[1, 0], inplace=True)
df['SMOKE'].value_counts() # converting categorical data to numeric data
df['SMOKE'].replace(['yes', 'no'],[1, 0], inplace=True)
df['SCC'].value_counts() # converting categorical data to numeric data
df['SCC'].replace(['yes', 'no'],[1, 0], inplace=True)
df['CALC'].value_counts() # converting categorical data to numeric data
df = pd.get_dummies(df, columns=['CALC'])
df['MTRANS'].value_counts() # converting categorical data to numeric data
df = pd.get_dummies(df, columns=['MTRANS'])
############################### K-Mean Clustering ##################################
# define the class for KMean clustering
class KMeans:
# variable assign
def __init__(self, n_clusters, max_iter = 20):
self.n_clusters = n_clusters
self.max_iter = max_iter
self.centroids = None
# function to make cluster of data
def fit_predict(self,X):
random_index = random.sample(range(0,X.shape[0]), self.n_clusters)
self.centroids = X[random_index]
for i in range(self.max_iter):
#assign cluster
cluster_group = self.assign_cluster(X)
old_centroids = self.centroids
#move centroid
self.centroids = self.move_centroids(X,cluster_group)
#check finish
if (old_centroids == self.centroids).all():
break
return cluster_group
# Assign cluster according to cosine similarity
def assign_cluster(self,X):
cluster_group = []
similarity = []
for row in X:
for centroid in self.centroids:
similarity.append(np.dot(row,centroid)/(norm(row)*norm(centroid)))
max_similarity = max(similarity)
index_pos = similarity.index(max_similarity)
cluster_group.append(index_pos)
similarity.clear()
return np.array(cluster_group)
# Assign new centroid after calculating mean of clustered data
def move_centroids(self,X,cluster_group):
new_centroids = []
cluster_type = np.unique(cluster_group)
for type in cluster_type:
new_centroids.append(X[cluster_group == type].mean(axis=0))
return np.array(new_centroids)
# function to calculate euclidean distance
def euclidean_distance(self,x1, x2):
return np.sqrt(np.sum((x1 - x2)**2))
# function to calculate silhouete coefficient of clustered data
def silhouette_coefficient(self,X, cluster_group):
# Initialize empty arrays to store the silhouette coefficients and distances
silhouette_coeffs = np.zeros(X.shape[0])
distances = np.zeros((X.shape[0], X.shape[0]))
# Calculate the distances between all pairs of samples
for i in range(X.shape[0]):
for j in range(i+1, X.shape[0]):
distances[i, j] = distances[j, i] = self.euclidean_distance(X[i], X[j])
# Calculate the silhouette coefficient for each sample
for i in range(X.shape[0]):
# Calculate the mean distance to other samples in the same cluster
a = np.mean(distances[i, cluster_group == cluster_group[i]])
# Calculate the mean distance to other samples in the closest other cluster
b = np.min([np.mean(distances[i, cluster_group == j]) for j in range(cluster_group.max()+1) if j != cluster_group[i]])
# Calculate the silhouette coefficient
if a < b:
silhouette_coeffs[i] = 1 - a/b
elif a > b:
silhouette_coeffs[i] = b/a - 1
else:
silhouette_coeffs[i] = 0
# Return the mean silhouette coefficient for all samples
return np.mean(silhouette_coeffs)
# X from the given dataset
X = df.iloc[:,:].values
# Taking different k for different no of cluster
different_k = [3,4,5,6]
# list to store Silhouette cofficient for different k
different_sil = []
for t in different_k:
km = KMeans(t, max_iter = 20)
cls = km.fit_predict(X)
clusters_KMean = [[] for _ in range(t)]
for i in range(len(cls)):
clusters_KMean[cls[i]].append(i)
sil_coeff = km.silhouette_coefficient(X, cls)
print(f"Silhouette coefficient for k = {t} after KMean clustering, is {sil_coeff}")
different_sil.append(sil_coeff)
fp = open(f"kmeans_k{t}.txt", "w") # file to store the clustered data
for cluster in clusters_KMean:
s = ",".join(str(i) for i in cluster) + "\n"
fp.write(s) # store the clustered data into file
fp.close()
# optimum k for which silhouette coeff is maximum
max_sil=0
a = 2
for s in different_sil:
a = a + 1
if s>max_sil:
optimum_k = a
max_sil = s
print("\n")
print(f"Optimum k (no of cluster) is {optimum_k}, having silhouette coefficient {max_sil}" )
# call the class KMeans
km = KMeans(n_clusters = optimum_k, max_iter = 20)
cls_k3 = km.fit_predict(X)
# Makes the separate list for individual clustered data
clusters_KM_k3 = [[] for _ in range(optimum_k)]
for i in range(len(cls_k3)):
clusters_KM_k3[cls_k3[i]].append(i)
############################ Single Linkage Divisive (Top-Down) Clustering #################################
# Perform divisive clustering on the input data using cosine similarity.
import numpy as np
def divisive_clustering(data, k):
# Convert data to unit vectors
norms = np.linalg.norm(data, axis=1, keepdims=True)
data = data / norms
# Initialize with all data points in one cluster
clusters_Hi = [list(range(data.shape[0]))]
while len(clusters_Hi) < k:
# Find the cluster with the largest SSE
sses = [np.sum(np.var(data[cluster], axis=0)) for cluster in clusters_Hi]
largest_sse_idx = np.argmax(sses)
# Split the largest cluster in two using cosine similarity
largest_cluster = np.array(clusters_Hi.pop(largest_sse_idx))
similarities = np.dot(data[largest_cluster], data[largest_cluster].T)
np.fill_diagonal(similarities, -1)
max_similarity_idx = np.argmax(similarities)
cluster1_idx, cluster2_idx = np.unravel_index(max_similarity_idx, similarities.shape)
cluster1 = [largest_cluster[cluster1_idx]]
cluster2 = [largest_cluster[cluster2_idx]]
for i, point in enumerate(largest_cluster):
if i not in {cluster1_idx, cluster2_idx}:
if similarities[i, cluster1_idx] > similarities[i, cluster2_idx]:
cluster1.append(point)
else:
cluster2.append(point)
# Add the new clusters to the list of clusters
clusters_Hi.append(cluster1)
clusters_Hi.append(cluster2)
return clusters_Hi # Returns a list of k clusters, each represented as a list of indices into the data array.
# call a function as cluster_Hi
cluster_Hi = divisive_clustering(X, optimum_k)
def silhouette_coefficient(data, clusters):
"""
Calculates the silhouette coefficient for the given data and clustering.
Args:
data (np.ndarray): The data points to cluster.
clusters (list): A list of lists, where each inner list contains the indices of the data points
in the corresponding cluster.
Returns:
float: The average silhouette coefficient for the given clustering.
"""
# Calculate the similarity matrix
similarities = np.dot(data, data.T)
# Iterate over each data point
num_points = data.shape[0]
silhouette_coeffs = np.zeros(num_points)
for i in range(num_points):
# Calculate the average distance to other points in the same cluster
cluster = None
for j, c in enumerate(clusters):
if i in c:
cluster = c
break
if cluster is None:
continue
avg_intra_dist = np.mean(similarities[i, cluster])
# Calculate the average distance to other points in the nearest neighboring cluster
nearest_cluster = None
nearest_dist = np.inf
for j, c in enumerate(clusters):
if c == cluster:
continue
dist = np.mean(similarities[i, c])
if dist < nearest_dist:
nearest_dist = dist
nearest_cluster = c
# Calculate the silhouette coefficient for this point
if nearest_cluster is None:
silhouette_coeffs[i] = 0
else:
silhouette_coeffs[i] = (nearest_dist - avg_intra_dist) / max(avg_intra_dist, nearest_dist)
# Return the average silhouette coefficient for all points
return np.mean(silhouette_coeffs)
sil_Hi = silhouette_coefficient(X, cluster_Hi)
print(f"\n silhouette coefficient after divisive clustering for optimum cluseters is {sil_Hi}.")
# store the final cluster information considering the optimal number of clusters
fp = open(f"divisive_K{optimum_k}.txt", "w") # file to store the clustered data
for cluster in cluster_Hi:
s = ",".join(str(i) for i in cluster) + "\n"
fp.write(s) # store the clustered data into file
fp.close()
################################## Jaccard similarity #######################################
# function for finding the similarity between two binary vectors
def jaccard_binary(x,y):
li = []
li1 = []
for i in range(len(x)):
for j in range(len(y)):
b = 0
# intersection = np.logical_and(x[i], y[j])
# union = np.logical_or(x[i], y[j])
# similarity = intersection.sum() / float(union.sum())
intersection = len(list(set(x[i]).intersection(y[j])))
union = (len(x[i]) + len(y[j])) - intersection
similarity = float(intersection) / union
li.append(similarity)
b = max(li)
li1.append(b)
return li1
# call the function as out
out = jaccard_binary(clusters_KM_k3,cluster_Hi)
print('\n')
print(f"Jaccard similarity between corresponding sets of both the cases is {out}.")