from sklearn datasets samples_generator import make_blobs xy_true make_blobs(n_samples= centers= cluster_std= random_state= initialize the variables as shownscores [values np arange( we need to iterate the -means model through all the values and also need to train it with the input data for num_clusters in valueskmeans kmeans(init=' -means++'n_clusters=num_clustersn_init= kmeans fit(xnowestimate the silhouette score for the current clustering model using the euclidean distance metricscore metrics silhouette_score(xkmeans labels_metric='euclidean'sample_size=len( )the following line of code will help in displaying the number of clusters as well as silhouette score print("\nnumber of clusters ="num_clustersprint("silhouette score ="scorescores append(scoreyou will receive the following outputnumber of clusters silhouette score num_clusters np argmax(scoresvalues[ print('\noptimal number of clusters ='num_clustersnowthe output for optimal number of clusters would be as followsoptimal number of clusters