I'm computing silhouette_score from sklearn.metrics library in python, for hierarchical clustering. I'm computing this metric for few cuts of the tree (few options of number of clusters, K). For some cuts of the tree, the silhouette_score returns nan result. Why?

here is my code:



  • dist_matrix is the distance matrix nXn (symmetric)
  • tree - one option for cut of the tree (using cut_tree(Z, n_clusters=K))
  • metric - precomputed as the distance matrix is already calculated based on some distance metric I calculated.

NaN result only happens for large amount of clusters (large K).here is the error I'm getting:

> /usr/local/lib64/python3.6/site-packages/sklearn/metrics/cluster/unsupervised.py:205:
> RuntimeWarning:
> invalid value encountered in true_divide

Any idea why? Thanks


Maybe you have many duplicate distances.

If a(i)=b(i)=0 in Silhouette, you can get a division by 0.


Not the answer you're looking for? Browse other questions tagged or ask your own question.