我的 DBscan 集群出错,请寻求解决方案

Error for my DBscan clustering, seek for a resolution

提问人:Creasic Dang 提问时间:1/18/2023 更新时间:1/18/2023 访问量:81

问:

所以我一直在尝试应用 DBSCAN 方法来聚类我的数据集,我想让silhouette_Score与我的其他模型进行比较,但是,在我执行 DBSCAN 后,它返回错误:

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

有谁知道是什么导致了我这个错误?

我的代码:

epsilon = [1,1.25,1.5,1.75, 2,2.25,2.5,2.75, 3,3.25,3.5,3.75, 4]
min_samples = [58]


sil_avg = []
max_value = [0,0,0,0]

for i in range(len(epsilon)):
for j in range(len(min_samples)):

    db = DBSCAN(min_samples = min_samples[j], eps =epsilon[i]).fit(dff)
    #cluster_labels=dbscan.fit_predict(data) 
    core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
    core_samples_mask[db.core_sample_indices_] = True
    labels = db.labels_

    # Number of clusters in labels, ignoring noise if present.
    n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
    n_noise_ = list(labels).count(-1)


    silhouette_avg = metrics.silhouette_score(dff, labels)
    if silhouette_avg > max_value[3]:
        max_value=(epsilon[i], min_samples[j], n_clusters_, silhouette_avg)
    sil_avg.append(silhouette_avg)

print("epsilon=", max_value[0], 
  "\nmin_sample=", max_value[1],
  "\nnumber of clusters=", max_value[2],
  "\naverage silhouette score= %.4f" % max_value[3])
python jupyter-notebook cluster-analysis dbscan epsilon

评论


答: 暂无答案