用sklearn在弧度距离矩阵上进行DBSCAN?

2024-09-27 22:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望对几个时间戳(以分钟为单位)进行聚类。 到目前为止我所做的是:

1)将点转换为弧度

#points containing time value in minutes
points = [100, 200, 600, 659, 700]

def convert_to_radian(x):
    return((x / (24 * 60)) * 2 * pi)

rad_function = np.vectorize(convert_to_radian)
points_rad = rad_function(points)

2)生成距离矩阵

^{pr2}$

3)指定距每个点的最短距离

dist[((dist > pi) & (dist <= (2*pi)))] = dist[((dist > pi) & (dist <= (2*pi)))] -(2*pi)
dist[((dist > (-2*pi)) & (dist <= (-1*pi)))] = dist[((dist > (-2*pi)) & (dist <= (-1*pi)))] + (2*pi) 
dist = abs(dist)

现在我想在距离矩阵上使用DBSCAN,如何将其聚类到弧度距离?在

谢谢你!在


Tags: to距离convertdist时间pi单位function
1条回答
网友
1楼 · 发布于 2024-09-27 22:22:32

好吧,经过大量的挖掘之后,我意识到我可以简单地将DBSCAN metric设置为“precomputed”,使用.fit()方法并传入我的距离矩阵。对于那些感兴趣的人,这里是来源:

import numpy as np
from math import pi
from sklearn.cluster import DBSCAN

#points containing time value in minutes
points = [100, 200, 600, 659, 700]

def convert_to_radian(x):
    return((x / (24 * 60)) * 2 * pi)

rad_function = np.vectorize(convert_to_radian)
points_rad = rad_function(points)

#generate distance matrix from each point
dist = points_rad[None,:] - points_rad[:, None]

#Assign shortest distances from each point
dist[((dist > pi) & (dist <= (2*pi)))] = dist[((dist > pi) & (dist <= (2*pi)))] -(2*pi)
dist[((dist > (-2*pi)) & (dist <= (-1*pi)))] = dist[((dist > (-2*pi)) & (dist <= (-1*pi)))] + (2*pi) 
dist = abs(dist)

#check dist
print(dist)

#using default values, set metric to 'precomputed'
db = DBSCAN(eps=((100 / (24*60)) * 2 * pi ), min_samples = 2, metric='precomputed')

#check db
print(db)

db.fit(dist)

#get labels
labels = db.labels_

#get number of clusters
no_clusters = len(set(labels)) - (1 if -1 in labels else 0)

print('No of clusters:', no_clusters)
print('Cluster 0 : ', np.nonzero(labels == 0)[0])
print('Cluster 1 : ', np.nonzero(labels == 1)[0])

输出:

^{pr2}$

相关问题 更多 >

    热门问题