I am stuck on finding a way to determine the elbow point (the optimal number of clusters to be used) programmaticaly. I need to run k-means on a set of 2D points obtained from an image and group points in regions of interest. There are a different number of region of interest in different images and I have to process a lot of data so manually going and assigning k for every image is not a good solution.
For now I have this code:
from sklearn.cluster import KMeans from keras import backend as keras results = model.predict(img, verbose=1, steps=1) for x in range(0,results.shape): for y in range(0,results.shape): if(results[x][y] >= 0.48): k_means_coords.append([x,y]) else: results[x][y] = 0 k_means_coords = np.asarray(k_means_coords) print(k_means_coords) Sum_of_squared_distances =  K = range(1, 15) for k in K: km = KMeans(n_clusters=k) km = km.fit(k_means_coords) Sum_of_squared_distances.append(km.inertia_) k = get_min_k(Sum_of_squared_distances, K) plt.plot(K, Sum_of_squared_distances, 'bx-') plt.xlabel('k') plt.ylabel('Sum_of_squared_distances') plt.title('Elbow Method For Optimal k') plt.show()
It generates this kind of plots:
and here it would be 3
and this is how results images(matrix) looks