[英]Clustering the 3D points when given the x,y,z coordinates using DBSCAN algorithm using python
I'm trying to cluster some 3D points with the help of some given coordinates using DBSCAN
algorithm with python.我试图在一些给定坐标的帮助下使用
DBSCAN
算法和 python 对一些 3D 点进行聚类。
ex:- given coordinates will be like follows例如:- 给定的坐标如下
X Y Z
[-37.530 3.109 -16.452]
[40.247 5.483 -15.209]
[-31.920 12.584 -12.916]
[-32.760 14.072 -13.749]
[-37.100 1.953 -15.720]
[-32.143 12.990 -13.488]
[-41.077 4.651 -15.651]
[-34.219 13.611 -13.090]
[-33.117 15.875 -13.738] e.t.c
I'm kind of new to programming and searching for an example script how to write the codes.我对编程和搜索如何编写代码的示例脚本有点陌生。 Can some one give a suggestion or an example?
有人可以给出建议或例子吗? Thanks a lot in advance.
非常感谢。
You can use sklearn.cluster.DBSCAN
.您可以使用
sklearn.cluster.DBSCAN
。 In your case:在你的情况下:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import DBSCAN
data = np.array([[-37.530, 3.109, -16.452],
[40.247, 5.483, -15.209],
[-31.920, 12.584, -12.916],
[-32.760, 14.072, -13.749],
[-37.100, 1.953, -15.720],
[-32.143, 12.990, -13.488],
[-41.077, 4.651, -15.651],
[-34.219, 13.611, -13.090],
[-33.117, 15.875, -13.738]])
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(data[:,0], data[:,1], data[:,2], s=300)
ax.view_init(azim=200)
plt.show()
model = DBSCAN(eps=2.5, min_samples=2)
model.fit_predict(data)
pred = model.fit_predict(data)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(data[:,0], data[:,1], data[:,2], c=model.labels_, s=300)
ax.view_init(azim=200)
plt.show()
print("number of cluster found: {}".format(len(set(model.labels_))))
print('cluster for each point: ', model.labels_)
ouput输出
number of cluster found: 3
cluster for each point: [ 0 -1 1 1 0 1 -1 1 1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.