I’ve been trying to determine the silhouette scores for each object in an array, which contains 3 vectors that represent 3 different objects and cluster labels for them are [1, 0, 0].
import numpy as np
from sklearn_extra.cluster import KMedoids
from sklearn.metrics.pairwise import euclidean_distances
X2=np.array([[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957,
-0.07310339, -0.31181501, 0.02696586],
[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ],
[ 5.93081714, -1.52272427, 0.40706477, 8.56256569, -3.216366 ,
-0.0108426 , -0.57434619, -0.18952662]])
model1 = KMedoids(n_clusters=2, random_state=0).fit(X2)
medoid=np.squeeze(X2[model1.medoid_indices_])
medoid
for i in range(len(X2)):
print("object", i)
s = []
for j in range(len(medoid)):
b = []
print('S',j, ':')
a1 = ( X2[i].reshape(-1, 1))
b1 = (X2[model1.medoid_indices_][j].reshape(-1, 1))
a =euclidean_distances(a1, b1)
print('a', a)
for k in range(len(medoid)):
if k != j:
a2 = ( X2[i].reshape(-1, 1))
b2 = (X2[model1.medoid_indices_][k].reshape(-1, 1))
b =euclidean_distances(a2, b2)
print('b', b)
bmin=min(b)
print('miminum b', min(b))
print('bi-ai', bmin-a)
print('max{a_i, b_i)', max(a, bmin))
s.append((bmin-a)/(max(a, bmin)))
print('SI', s)
max_value = max(s)
print("SI, max value:", max_value)
I want to calculate the silhouette score for each object by the following equation
S(i) = ( b(i) – a(i) ) / ( max { ( a(i), b(i) ) }
but my code is not working, any suggestions even with modified the code?