Module Expr Str ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias alias Import alias identifier Import alias identifier Import alias identifier Expr Call NameLoad NameLoad Assign TupleStore NameStore NameStore Call NameLoad keyword Num keyword Num keyword Num keyword Num keyword TupleLoad Num Num keyword NameLoad keyword Num Assign NameStore ListLoad Num Num Num Num Num For NameStore NameLoad body Assign TupleStore NameStore TupleStore NameStore NameStore Call AttributeLoad NameLoad attr Num Num Expr Call AttributeLoad NameLoad attr Num Num Expr Call AttributeLoad NameLoad attr ListLoad Num Num Expr Call AttributeLoad NameLoad attr ListLoad Num BinOpAdd Call NameLoad NameLoad BinOpMult BinOpAdd NameLoad Num Num Assign NameStore Call NameLoad keyword NameLoad keyword Num Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore Call NameLoad NameLoad NameLoad Expr Call NameLoad Str NameLoad Str NameLoad Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore Num For NameStore Call NameLoad NameLoad body Assign NameStore SubscriptLoad NameLoad Index CompareEq NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Assign NameStore SubscriptLoad AttributeLoad NameLoad attr Index Num Assign NameStore BinOpAdd NameLoad NameLoad Assign NameStore Call AttributeLoad NameLoad attr BinOpDiv Call NameLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call AttributeLoad NameLoad attr NameLoad NameLoad Num NameLoad keyword NameLoad keyword NameLoad keyword Num Expr Call AttributeLoad NameLoad attr Num BinOpAdd NameLoad BinOpMult Num NameLoad Call NameLoad NameLoad Assign NameStore BinOpAdd NameLoad Num Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr keyword NameLoad keyword Str keyword Str Expr Call AttributeLoad NameLoad attr ListLoad Expr Call AttributeLoad NameLoad attr ListLoad Num Num Num Num Num Num Num Assign NameStore Call AttributeLoad NameLoad attr BinOpDiv Call AttributeLoad NameLoad attr NameLoad NameLoad Expr Call AttributeLoad NameLoad attr SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num keyword Str keyword Num keyword Num keyword Num keyword NameLoad Assign NameStore AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr SubscriptLoad NameLoad ExtSlice Slice Index Num SubscriptLoad NameLoad ExtSlice Slice Index Num keyword Str keyword Str keyword Num keyword Num For TupleStore NameStore NameStore Call NameLoad NameLoad body Expr Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Index Num SubscriptLoad NameLoad Index Num keyword BinOpMod Str NameLoad keyword Num keyword Num Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr Str Expr Call AttributeLoad NameLoad attr BinOpMod Str NameLoad keyword Num keyword Str Expr Call AttributeLoad NameLoad attr
N/A N/A 
===============================================================================
Selecting the number of clusters with silhouette analysis on KMeans clustering
===============================================================================

Silhouette analysis can be used to study the separation distance between the
resulting clusters. The silhouette plot displays a measure of how close each
point in one cluster is to points in the neighboring clusters and thus provides
a way to assess parameters like number of clusters visually. This measure has a
range of [-1, 1].

Silhouette coefficients (as these values are referred to as) near +1 indicate
that the sample is far away from the neighboring clusters. A value of 0
indicates that the sample is on or very close to the decision boundary between
two neighboring clusters and negative values indicate that those samples might
have been assigned to the wrong cluster.

In this example the silhouette analysis is used to choose an optimal value for
``n_clusters``. The silhouette plot shows that the ``n_clusters`` value of 3, 5
and 6 are a bad pick for the given data due to the presence of clusters with
below average silhouette scores and also due to wide fluctuations in the size
of the silhouette plots. Silhouette analysis is more ambivalent in deciding
between 2 and 4.

Also from the thickness of the silhouette plot the cluster size can be
visualized. The silhouette plot for cluster 0 when ``n_clusters`` is equal to
2, is bigger in size owing to the grouping of the 3 sub clusters into one big
cluster. However when the ``n_clusters`` is equal to 4, all the plots are more
or less of similar thickness and hence are of similar sizes as can be also
verified from the labelled scatter plot on the right.
 __future__ print_function sklearn.datasets make_blobs sklearn.cluster KMeans sklearn.metrics silhouette_samples silhouette_score N/A matplotlib.pyplot plt N/A matplotlib.cm cm N/A numpy np N/A N/A print __doc__ N/A N/A X y N/A make_blobs n_samples 500 n_features 2 centers 4 cluster_std 1 center_box N/A -10.0 10.0 shuffle True random_state 1 N/A range_n_clusters N/A 2 3 4 5 6 N/A n_clusters range_n_clusters N/A N/A N/A fig N/A ax1 ax2 N/A N/A plt subplots 1 2 N/A N/A N/A fig set_size_inches 18 7 N/A N/A N/A ax1 set_xlim N/A -0.1 1 N/A N/A N/A ax1 set_ylim N/A 0 N/A N/A len X N/A N/A n_clusters 1 10 N/A clusterer N/A KMeans n_clusters n_clusters random_state 10 N/A cluster_labels N/A N/A clusterer fit_predict X N/A silhouette_avg N/A silhouette_score X cluster_labels N/A N/A print For n_clusters = n_clusters The average silhouette_score is : silhouette_avg N/A sample_silhouette_values N/A silhouette_samples X cluster_labels N/A y_lower 10 N/A i N/A range n_clusters N/A N/A ith_cluster_silhouette_values N/A sample_silhouette_values N/A N/A cluster_labels i N/A N/A N/A ith_cluster_silhouette_values sort N/A size_cluster_i N/A N/A ith_cluster_silhouette_values shape N/A 0 N/A y_upper N/A y_lower size_cluster_i N/A color N/A N/A cm spectral N/A N/A float i n_clusters N/A N/A N/A ax1 fill_betweenx N/A N/A np arange y_lower y_upper 0 ith_cluster_silhouette_values facecolor color edgecolor color alpha 0.7 N/A N/A N/A ax1 text -0.05 N/A y_lower N/A 0.5 size_cluster_i N/A str i N/A y_lower N/A y_upper 10 N/A N/A N/A ax1 set_title The silhouette plot for the various clusters. N/A N/A N/A ax1 set_xlabel The silhouette coefficient values N/A N/A N/A ax1 set_ylabel Cluster label N/A N/A N/A ax1 axvline x silhouette_avg color red linestyle -- N/A N/A N/A ax1 set_yticks N/A N/A N/A N/A ax1 set_xticks N/A -0.1 0 0.2 0.4 0.6 0.8 1 N/A colors N/A N/A cm spectral N/A N/A N/A cluster_labels astype float n_clusters N/A N/A N/A ax2 scatter N/A X N/A N/A N/A 0 N/A X N/A N/A N/A 1 marker . s 30 lw 0 alpha 0.7 c colors N/A centers N/A clusterer cluster_centers_ N/A N/A N/A ax2 scatter N/A centers N/A N/A N/A 0 N/A centers N/A N/A N/A 1 marker o c white alpha 1 s 200 N/A N/A i c N/A enumerate centers N/A N/A N/A N/A ax2 scatter N/A c N/A 0 N/A c N/A 1 marker N/A $%d$ i alpha 1 s 50 N/A N/A N/A ax2 set_title The visualization of the clustered data. N/A N/A N/A ax2 set_xlabel Feature space for the 1st feature N/A N/A N/A ax2 set_ylabel Feature space for the 2nd feature N/A N/A N/A plt suptitle N/A Silhouette analysis for KMeans clustering on sample data with n_clusters = %d n_clusters fontsize 14 fontweight bold N/A N/A N/A plt show