使用DBSCAN在Python中查找每个集群中心,半径?

时间:2018-04-05 15:29:28

标签: python dbscan

我想找到每个群集的中心和半径。我能做什么?请帮我。这是我的代码,它包含dbscan和meanshifts两个结果。这些点是随机的,现在我想找到每个簇的中心和半径。

import numpy as np
import matplotlib.pyplot as plt
from sklearn import cluster


def cluster_plots(set, colours1='gray', colours2='gray',
                  title1='Plot 1', title2='Plot 2'):
    fig, (ax1, ax2) = plt.subplots(1, 2)
    fig.set_size_inches(6, 3)
    ax1.set_title(title1, fontsize=14)
    ax1.set_xlim(min(set[:, 0]), max(set[:, 0]))
    ax1.set_ylim(min(set[:, 1]), max(set[:, 1]))
    ax1.scatter(set[:, 0], set[:, 1], s=8, lw=0, c=colours1)

    ax2.set_title(title2, fontsize=14)
    ax2.set_xlim(min(set[:, 0]), max(set[:, 0]))
    ax2.set_ylim(min(set[:, 1]), max(set[:, 1]))
    ax2.scatter(set[:, 0], set[:, 1], s=8, lw=0, c=colours2)
    fig.tight_layout()
    plt.show()

def data_generator():
    clust1 = np.random.normal(5, 2, (1000, 2))
    clust2 = np.random.normal(15, 3, (1000, 2))
    clust3 = np.random.multivariate_normal([17, 3], [[1, 0], [0, 1]], 1000)
    clust4 = np.random.multivariate_normal([2, 16], [[1, 0], [0, 1]], 1000)
    return np.concatenate((clust1, clust2, clust3, clust4))

datapoints  = data_generator()
bandwidths  = [cluster.estimate_bandwidth(dataset, quantile=0.1) for dataset in [datapoints]]
meanshifts  = [cluster.MeanShift(bandwidth=band, bin_seeding=True).fit(dataset) for dataset, band in zip([datapoints], bandwidths)]
dbscan      = cluster.DBSCAN(eps=1, min_samples=10, metric='euclidean').fit_predict(datapoints)

cluster_plots(datapoints, dbscan,meanshifts[0].predict(datapoints),title1='DBScan', title2='Meanshifts')

我不知道我应该添加什么可以得到我想要的东西。

1 个答案:

答案 0 :(得分:0)

DBSCAN群集不使用中心或半径的概念。

群集可以是任何形状,如果您考虑维基百科文章中的示例,群集“中心”可以从群集中支付,如果您有这样的香蕉形群集。

无论哪种方式,都没有API来获得“中心”。无论你使用什么,你都可以让我自己找到它。而不是算术平均值,考虑一些基于图形的图形,如图形中心性。

相关问题