Or is there something else I should be using for this problem that would be better?
为了同时考虑中心之间的地理距离和产生的功率,您应该定义适当的度量.下面的函数计算地球表面上两个点之间的距离,从纬度和经度到haversine formula,并将生成的功率差的绝对值乘以加权因子.权重值决定了聚类过程中距离和功率差异的相对影响.
import numpy as np
def custom_metric(central_1, central_2, weight=1):
lat1, lng1, pow1 = central_1
lat2, lng2, pow2 = central_2
lat1, lat2, lng1, lng2 = np.deg2rad(np.asarray([lat1, lat2, lng1, lng2]))
dlat = lat2 - lat1
dlng = lng2 - lng1
h = (1 - np.cos(dlat))/2. + np.cos(lat1)*np.cos(lat2)*(1 - np.cos(dlng))/2.
km = 2*6371*np.arcsin(np.sqrt(h))
MW = np.abs(pow2 - pow1)
return km + weight*MW
Should I be doing this with kmeans (or some other method)?
不幸的是,SciPy的kmeans2和scikit-learn的KMeans的当前实现仅支持欧几里德距离.另一种方法是通过SciPy的聚类包执行hierarchical clustering,根据刚定义的度量对中心进行分组.
演示
<