I am using silhouette_score to find the optimal k value. So I am running a for loop with a range of possible k values. I have added my code below. this program takes a very long time to run. Could you suggest some improvements for a more efficient run time?
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from sklearn.cluster import KMeans from sklearn import metrics data=np.load(filename) coeffs= for i in range(2,8): clusters=KMeans(n_clusters=i) clusters.fit(data) labels = clusters.labels_ sil_coeff = metrics.silhouette_score(data, labels,metric='euclidean') coeffs.append(sil_coeff) coeffs=np.array(coeffs) k=np.argmax(coeffs)+2