Why does KMeans' silhouette_score change after restarting runtime in Google Colab?

Viewed 227

I'm trying to get reproducible results from sklearn's KMeans running on a Google Colab notebook. The Kmeans algorithm is being fit on an array generated by a Principal Component Analysis (PCA). Every time I restart the notebook's runtime, fit, predict and generate the silhouette_score of a K-means algorithm, the silhouette_score changes!

Here's the code that I run to fit and predict using Kmeans and to generate the silhouette_score:

for n_clusters in range(3,9):
    kmeans = KMeans(init= 'k-means++', n_clusters = n_clusters, n_init= 25, random_state = 0)
    kmeans.fit(pca_mat_products)
    clusters = kmeans.predict(pca_mat_products)
    silhouette_avg = silhouette_score(mp_matrix, clusters, random_state= 0)
    print("For n_clusters =", n_clusters, "The average silhouette_score is :", silhouette_avg)

Here's an example of the silhouette_scores generated:

For n_clusters = 3 The average silhouette_score is : 0.08689747798228342
For n_clusters = 4 The average silhouette_score is : 0.11513524544540599
For n_clusters = 5 The average silhouette_score is : 0.13225896257848024
For n_clusters = 6 The average silhouette_score is : 0.13390795741576195
For n_clusters = 7 The average silhouette_score is : 0.11262045164741093
For n_clusters = 8 The average silhouette_score is : 0.12179451798486395

When I restart the notebook's runtime while maintaining everything the same on the notebook (including random_state =0), and run the cells from scratch, new silhouette_scores appear each time I restart the notebook.

Here's the silhouette_scores generated by the same code in a different run:

For n_clusters = 3 The average silhouette_score is : 0.09181951382862036
For n_clusters = 4 The average silhouette_score is : 0.11539863985647045
For n_clusters = 5 The average silhouette_score is : 0.13363229313208771
For n_clusters = 6 The average silhouette_score is : 0.13428788881085452
For n_clusters = 7 The average silhouette_score is : 0.13187306014661757
For n_clusters = 8 The average silhouette_score is : 0.13252806332855294

In further runtimes the silhouette_scores keep changing.

mp_matrix is a one-hot encoded array, looking as follows:

array([[0, 0, 0, ..., 0, 1, 0],
       [0, 0, 0, ..., 0, 1, 0],
       [0, 0, 0, ..., 0, 1, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [1, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

Is it normal for the silhouette_scores to changes after restarting runtime in Google Colab? Is there any way to obtain reproducible silhouette_scores?

I've searched here and in other places online and didn't find this issue being discussed.

Thank you!

Your help is appreciated.

1 Answers
Related