使用 python 在 DBSCAN 中分离每个簇的坐标(3D 坐标)

Separating the coordinates(3D coordinates) for each cluster in DBSCAN using python

我正在使用 DBSCAN(使用 python)对一些 3D 坐标进行聚类。我能够将它聚类成不同的聚类。但现在我想分离属于这些集群中的每一个的坐标并将这些坐标打印到不同的文本文件中。谁能帮我怎么做。

我已经尝试过聚类成单独的簇并获取每个簇的坐标。完全不同的代码也可以

由于某些问题,我无法 post 我的代码。但是我会 post 我用来制作我的命令的命令。 (这个是二维的)

from sklearn.cluster import DBSCAN
import numpy as np
data = np.random.rand(500,3)

db = DBSCAN(eps=0.12, min_samples=1).fit(data)
labels = db.labels_
from collections import Counter
Counter(labels)

要将样本 (numpy.array) 存储在 .txt 文件中,您应该首先根据簇分配将样本划分为多个分区,然后保存生成的分区。

from collections import defaultdict

clusters = defaultdict(list)

for i,c in enumerate(db.labels_):
    clusters[c].append(data[i])

for k,v in clusters.items():
    np.savetxt('cluster{}.txt'.format(k), v, delimiter=",", fmt='%s')

你会得到 68 个 txt 文件,每个文件都包含数据集中一个或多个样本的坐标。