Search code examples
pythondbscansilhouette

Is there a way to solve this error when using silhouette_score with DBSCAN


I'm trying to find the best eps values for DBSCAN. I set min_samples to 24 as I have 2 * 12 features passed through in x_set but I get the following error:

ValueError: Number of labels is 1. Valid values are 2 to n_samples - 1 (inclusive)

I know that the silhouette_score requires more than 1 cluster labels. Which might be causing this error based on similar error.

How do I solve this issue?

x_setcontains the following data:

x_set = [6.67933536e+00 1.65097236e+00 1.24573705e+00 1.01693195e+00
 9.28128921e-01 7.82497904e-01 5.98319768e-01 1.13439548e-01
 9.05382510e-04 5.42710767e-04 2.87522799e-04 1.90924073e-04]
def best_eps(x_set):
    
    X = x_set

    range_eps = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
    for i in range_eps:
        print("eps value is: "+str(i))
        db = DBSCAN(eps=i, min_samples=24, metric='euclidean').fit(X)
        core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
        core_samples_mask[db.core_sample_indices_] = True
        labels = db.labels_
        print(set(labels))
        silhouette_avg = silhouette_score(X, labels)
        print("For eps value ="+str(i), labels, "The average silhouette_score is :", silhouette_avg)
    return

Solution

  • Choose parameters so that you get more than one cluster.

    Also, Silhouette is not sensible to use on clusterings with Noise.

    It will treat noise as a cluster, and assign it a very poor Silhouette.