python - Calculate silhouette score for each object with cluster medoids
问题描述
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?
解决方案
推荐阅读
- javascript - 如何将时间戳转换为日期字符串?
- reactjs - JSX 元素的 HOC
- html - 是否可以编译运行在 github 上的 Jekyll 站点?
- laravel - 是否可以提交对象数组?
- java - Vavr Set 字段应该是 volatile、atomic 还是以其他方式声明?
- c# - C#:“在异步函数上具有多个等待的函数”和“在包含多个同步函数的任务上等待”之间的性能差异
- node.js - 如何使用猫鼬正确过滤每个对象键每个时间段的 1 个文档
- php - 如何将php输出显示到特定的div中?
- cluster-computing - Corda 4.0 - 集群中的 Notary 以实现高可用性
- javascript - 在赛普拉斯中提取部分文本