python - 如何在 python 中执行具有权重/密度的集群?有权重的 kmeans 之类的东西?

标签 python algorithm scipy scikit-learn cluster-analysis

我的数据是这样的:

powerplantname, latitude, longitude, powergenerated
A, -92.3232, 100.99, 50
B, <lat>, <long>, 10
C, <lat>, <long>, 20
D, <lat>, <long>, 40
E, <lat>, <long>, 5

我希望能够将数据聚类成 N 个聚类(比如 3 个)。通常我会使用 kmeans:

import numpy as np

import matplotlib.pyplot as plt
from scipy.cluster.vq import kmeans2, whiten
coordinates= np.array([
           [lat, long],
           [lat, long],
            ...
           [lat, long]
           ])
x, y = kmeans2(whiten(coordinates), 3, iter = 20)  
plt.scatter(coordinates[:,0], coordinates[:,1], c=y);
plt.show()

这个问题是它没有考虑任何权重(在这种情况下,我的 powergenerated 值)我希望理想情况下让我的集群考虑“powergenerated”值,试图让集群不仅在空间上接近,但也有接近相对相等的总发电量。

我应该使用 kmeans(或其他方法)来执行此操作吗?还是我应该使用其他更好的方法来解决这个问题?

最佳答案

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 的当前实现仅支持欧氏距离。另一种方法是执行 hierarchical clustering通过 SciPy 的聚类包根据刚刚定义的指标对中心进行分组。

演示

让我们首先生成模拟数据,即具有随机值的 8 个中心的特征向量:

N = 8
np.random.seed(0)
lat = np.random.uniform(low=-90, high=90, size=N)
lng = np.random.uniform(low=-180, high=180, size=N)
power = np.random.randint(low=5, high=50, size=N)
data = np.vstack([lat, lng, power]).T

上面代码片段生成的变量 data 的内容如下所示:

array([[   8.7864,  166.9186,   21.    ],
       [  38.7341,  -41.9611,   10.    ],
       [  18.4974,  105.021 ,   20.    ],
       [   8.079 ,   10.4022,    5.    ],
       [ -13.7421,   24.496 ,   23.    ],
       [  26.2609,  153.2148,   40.    ],
       [ -11.2343, -154.427 ,   29.    ],
       [  70.5191, -148.6335,   34.    ]])

要将这些数据分成三个不同的组,我们必须将 datacustom_metric 传递给 linkage 函数(检查 docs 以详细了解参数 method),然后将返回的链接矩阵传递给 n_clusters=3cut_tree 函数。

from scipy.cluster.hierarchy import linkage, cut_tree
Z = linkage(data, method='average', metric=custom_metric)
y = cut_tree(Z, 3).flatten()

因此,我们得到了每个中心的组成员(数组 y):

array([0, 1, 0, 2, 2, 0, 0, 1])

以上结果取决于weight的值。如果您希望使用不同于 1 的值(例如 250),您可以像这样更改默认值:

def custom_metric(central_1, central_2, weight=250):

或者,您可以将 linkage 调用中的参数 metric 设置为 lambda 表达式,如下所示:metric=lambda x, y: custom_metric(x, y, 250).

最后,为了更深入地了解层次/凝聚聚类,您可以将其绘制为树状图:

from scipy.cluster.hierarchy import dendrogram
dendrogram(Z)

dendrogram

关于python - 如何在 python 中执行具有权重/密度的集群?有权重的 kmeans 之类的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45025056/

相关文章:

python - 如何将 'or' 应用于 Python 中列表的所有值?

python - 用于迭代 numpy 数组列表的行的生成器

python - spaCy - 将扩展函数添加到管道导致堆栈溢出

algorithm - 最大化矩阵中的特殊元素

mysql - 将目录中的数据同步到数据库的实用方法是什么?

python日志n选择k

python - SciPy - from_rotvec() 不带关键字参数

python - marshmallow-sqlalchemy 给出语法错误

algorithm - 计算向量中的抖动字符串

python - 估计由一组点(Alpha 形状??)生成的图像区域