Lar*_*son 7 python cluster-analysis latitude-longitude spatial-query
我有一个点列表 P=[p1,...pN] 其中 pi=(latitudeI,longitudeI)。
使用 Python 3,我想找到最小的集群集(P 的不相交子集),使得集群中的每个成员都在集群中每个其他成员的 20 公里以内。
使用Vincenty 方法计算两点之间的距离。
为了使这更具体一点,假设我有一组要点,例如
from numpy import *
points = array([[33. , 41. ],
[33.9693, 41.3923],
[33.6074, 41.277 ],
[34.4823, 41.919 ],
[34.3702, 41.1424],
[34.3931, 41.078 ],
[34.2377, 41.0576],
[34.2395, 41.0211],
[34.4443, 41.3499],
[34.3812, 40.9793]])
Run Code Online (Sandbox Code Playgroud)
然后我试图定义这个函数:
from geopy.distance import vincenty
def clusters(points, distance):
"""Returns smallest list of clusters [C1,C2...Cn] such that
for x,y in Ci, vincenty(x,y).km <= distance """
return [points] # Incorrect but gives the form of the output
Run Code Online (Sandbox Code Playgroud)
注意:许多问题集中在地理位置和属性上。我的问题仅针对位置。这是用于纬度/经度,而不是欧几里得距离。还有其他问题可以给出一些答案,但不是这个问题的答案(许多未回答):
这是一个看起来正确的解决方案,根据数据,最坏情况下的表现会是 O(N^2),更好的是:
def my_cluster(S,distance):
coords=set(S)
C=[]
while len(coords):
locus=coords.pop()
cluster = [x for x in coords if vincenty(locus,x).km <= distance]
C.append(cluster+[locus])
for x in cluster:
coords.remove(x)
return C
Run Code Online (Sandbox Code Playgroud)
注意:我不会将此标记为答案,因为我的要求之一是它是最小的集群集。我的第一遍很好,但我还没有证明它是最小的一组。
结果(在更大的点集上)可以可视化如下:
这可能是一个开始。该算法试图通过将 k 从 2 迭代到验证每个解决方案的点数来对点进行聚类。你应该选择最低的数字。
它的工作原理是对点进行聚类,然后检查每个聚类是否遵守约束。如果任何集群不合规,则解决方案将被标记为 ,False然后我们继续进行下一个集群数量。
因为 sklearn 中使用的 K-means 算法属于局部最小值,证明这是否是您正在寻找的解决方案是最好的一个仍有待建立,但它可能是一个
import numpy as np
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
import math
points = np.array([[33. , 41. ],
[33.9693, 41.3923],
[33.6074, 41.277 ],
[34.4823, 41.919 ],
[34.3702, 41.1424],
[34.3931, 41.078 ],
[34.2377, 41.0576],
[34.2395, 41.0211],
[34.4443, 41.3499],
[34.3812, 40.9793]])
def distance(origin, destination): #found here https://gist.github.com/rochacbruno/2883505
lat1, lon1 = origin[0],origin[1]
lat2, lon2 = destination[0],destination[1]
radius = 6371 # km
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c
return d
def create_clusters(number_of_clusters,points):
kmeans = KMeans(n_clusters=number_of_clusters, random_state=0).fit(points)
l_array = np.array([[label] for label in kmeans.labels_])
clusters = np.append(points,l_array,axis=1)
return clusters
def validate_solution(max_dist,clusters):
_, __, n_clust = clusters.max(axis=0)
n_clust = int(n_clust)
for i in range(n_clust):
two_d_cluster=clusters[clusters[:,2] == i][:,np.array([True, True, False])]
if not validate_cluster(max_dist,two_d_cluster):
return False
else:
continue
return True
def validate_cluster(max_dist,cluster):
distances = cdist(cluster,cluster, lambda ori,des: int(round(distance(ori,des))))
print(distances)
print(30*'-')
for item in distances.flatten():
if item > max_dist:
return False
return True
if __name__ == '__main__':
for i in range(2,len(points)):
print(i)
print(validate_solution(20,create_clusters(i,points)))
Run Code Online (Sandbox Code Playgroud)
一旦建立了基准,就必须对每个集群多关注一个,以确定是否可以在不违反距离约束的情况下将其点分配给其他人。
你可以用你选择的任何距离度量替换 cdist 中的 lambda 函数,我在我提到的 repo 中找到了大圆距离。
| 归档时间: |
|
| 查看次数: |
11050 次 |
| 最近记录: |