/sklearn/metrics/cluster/unsupervised.py

https://github.com/bvtrach/scikit-learn
Python | 193 lines | 104 code | 11 blank | 78 comment | 9 complexity | b08dd315ac851e445e906e03bb10e0a5 MD5 | raw file
  1. """ Unsupervised evaluation metrics. """
  2. # Authors: Robert Layton <robertlayton@gmail.com>
  3. #
  4. # License: BSD Style.
  5. import numpy as np
  6. from ...utils import check_random_state
  7. from ..pairwise import pairwise_distances
  8. def silhouette_score(X, labels, metric='euclidean',
  9. sample_size=None, random_state=None, **kwds):
  10. """Compute the mean Silhouette Coefficient of all samples.
  11. The Silhouette Coefficient is calculated using the mean intra-cluster
  12. distance (a) and the mean nearest-cluster distance (b) for each sample.
  13. The Silhouette Coefficient for a sample is (b - a) / max(a, b).
  14. To clarrify, b is the distance between a sample and the nearest cluster
  15. that b is not a part of.
  16. This function returns the mean Silhoeutte Coefficient over all samples.
  17. To obtain the values for each sample, use silhouette_samples
  18. The best value is 1 and the worst value is -1. Values near 0 indicate
  19. overlapping clusters. Negative values generally indicate that a sample has
  20. been assigned to the wrong cluster, as a different cluster is more similar.
  21. Parameters
  22. ----------
  23. X: array [n_samples_a, n_samples_a] if metric == "precomputed", or,
  24. [n_samples_a, n_features] otherwise
  25. Array of pairwise distances between samples, or a feature array.
  26. labels : array, shape = [n_samples]
  27. label values for each sample
  28. metric: string, or callable
  29. The metric to use when calculating distance between instances in a
  30. feature array. If metric is a string, it must be one of the options
  31. allowed by metrics.pairwise.pairwise_distances. If X is the distance
  32. array itself, use "precomputed" as the metric.
  33. sample_size: int or None
  34. The size of the sample to use when computing the Silhouette
  35. Coefficient. If sample_size is None, no sampling is used.
  36. random_state: integer or numpy.RandomState, optional
  37. The generator used to initialize the centers. If an integer is
  38. given, it fixes the seed. Defaults to the global numpy random
  39. number generator.
  40. **kwds: optional keyword parameters
  41. Any further parameters are passed directly to the distance function.
  42. If using a scipy.spatial.distance metric, the parameters are still
  43. metric dependent. See the scipy docs for usage examples.
  44. Returns
  45. -------
  46. silhouette : float
  47. Mean Silhouette Coefficient for all samples.
  48. References
  49. ----------
  50. Peter J. Rousseeuw (1987). "Silhouettes: a Graphical Aid to the
  51. Interpretation and Validation of Cluster Analysis". Computational
  52. and Applied Mathematics 20: 53-65. doi:10.1016/0377-0427(87)90125-7.
  53. http://en.wikipedia.org/wiki/Silhouette_(clustering)
  54. """
  55. if sample_size is not None:
  56. random_state = check_random_state(random_state)
  57. indices = random_state.permutation(X.shape[0])[:sample_size]
  58. if metric == "precomputed":
  59. X, labels = X[indices].T[indices].T, labels[indices]
  60. else:
  61. X, labels = X[indices], labels[indices]
  62. return np.mean(silhouette_samples(X, labels, metric=metric, **kwds))
  63. def silhouette_samples(X, labels, metric='euclidean', **kwds):
  64. """Compute the Silhouette Coefficient for each sample.
  65. The Silhoeutte Coefficient is a measure of how well samples are clustered
  66. with samples that are similar to themselves. Clustering models with a high
  67. Silhouette Coefficient are said to be dense, where samples in the same
  68. cluster are similar to each other, and well separated, where samples in
  69. different clusters are not very similar to each other.
  70. The Silhouette Coefficient is calculated using the mean intra-cluster
  71. distance (a) and the mean nearest-cluster distance (b) for each sample.
  72. The Silhouette Coefficient for a sample is (b - a) / max(a, b).
  73. This function returns the Silhoeutte Coefficient for each sample.
  74. The best value is 1 and the worst value is -1. Values near 0 indicate
  75. overlapping clusters.
  76. Parameters
  77. ----------
  78. X: array [n_samples_a, n_samples_a] if metric == "precomputed", or,
  79. [n_samples_a, n_features] otherwise
  80. Array of pairwise distances between samples, or a feature array.
  81. labels : array, shape = [n_samples]
  82. label values for each sample
  83. metric: string, or callable
  84. The metric to use when calculating distance between instances in a
  85. feature array. If metric is a string, it must be one of the options
  86. allowed by metrics.pairwise.pairwise_distances. If X is the distance
  87. array itself, use "precomputed" as the metric.
  88. **kwds: optional keyword parameters
  89. Any further parameters are passed directly to the distance function.
  90. If using a scipy.spatial.distance metric, the parameters are still
  91. metric dependent. See the scipy docs for usage examples.
  92. Returns
  93. -------
  94. silhouette : array, shape = [n_samples]
  95. Silhouette Coefficient for each samples.
  96. References
  97. ----------
  98. Peter J. Rousseeuw (1987). "Silhouettes: a Graphical Aid to the
  99. Interpretation and Validation of Cluster Analysis". Computational
  100. and Applied Mathematics 20: 53-65. doi:10.1016/0377-0427(87)90125-7.
  101. http://en.wikipedia.org/wiki/Silhouette_(clustering)
  102. """
  103. distances = pairwise_distances(X, metric=metric, **kwds)
  104. n = labels.shape[0]
  105. A = np.array([_intra_cluster_distance(distances[i], labels, i)
  106. for i in range(n)])
  107. B = np.array([_nearest_cluster_distance(distances[i], labels, i)
  108. for i in range(n)])
  109. return (B - A) / np.maximum(A, B)
  110. def _intra_cluster_distance(distances_row, labels, i):
  111. """Calculate the mean intra-cluster distance for sample i.
  112. Parameters
  113. ----------
  114. distances_row : array, shape = [n_samples]
  115. Pairwise distance matrix between sample i and each sample.
  116. labels : array, shape = [n_samples]
  117. label values for each sample
  118. i : int
  119. Sample index being calculated. It is excluded from calculation and
  120. used to determine the current label
  121. Returns
  122. -------
  123. a : float
  124. Mean intra-cluster distance for sample i
  125. """
  126. mask = labels == labels[i]
  127. mask[i] = False
  128. a = np.mean(distances_row[mask])
  129. return a
  130. def _nearest_cluster_distance(distances_row, labels, i):
  131. """Calculate the mean nearest-cluster distance for sample i.
  132. Parameters
  133. ----------
  134. distances_row : array, shape = [n_samples]
  135. Pairwise distance matrix between sample i and each sample.
  136. labels : array, shape = [n_samples]
  137. label values for each sample
  138. i : int
  139. Sample index being calculated. It is used to determine the current
  140. label.
  141. Returns
  142. -------
  143. b : float
  144. Mean nearest-cluster distance for sample i
  145. """
  146. label = labels[i]
  147. b = np.min([np.mean(distances_row[labels == cur_label])
  148. for cur_label in set(labels) if not cur_label == label])
  149. return b