Python-坐标密度平均值

问题描述 投票:0回答:1

我的分数(X,Y)

id,X,Y
1,11.60388710,48.10862135
2,11.60420372,48.10865659
3,11.60424066,48.10864778
4,11.60425121,48.10867597
5,11.60471031,48.10872354
6,11.60428551,48.10917455
7,11.60563380,48.10921331
8,11.60422219,48.10867773
9,11.60434356,48.10870064
10,11.60460214,48.10843284

enter image description here

并且希望找到一个受彼此靠近的点严重影响的中心点(非质心)。

例如,我可以在QGIS中创建一个热图,并具有如下所示的内容:

enter image description here

也许有人知道如何编写Python脚本来计算此X,Y“密度”中心?

谢谢您的帮助!

python geospatial spatial qgis
1个回答
2
投票

您可以尝试使用scipy的内核密度估计。例如(受code here启发):

from scipy.stats import gaussian_kde
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

names = ["id","X","Y"]
data = [[1,11.60388710, 48.10862135],
        [2,11.60420372, 48.10865659],
        [3,11.60424066, 48.10864778],
        [4,11.60425121, 48.10867597],
        [5,11.60471031, 48.10872354],
        [6,11.60428551, 48.10917455],
        [7,11.60563380, 48.10921331],
        [8,11.60422219, 48.10867773],
        [9,11.60434356, 48.10870064],
        [10,11.60460214, 48.10843284]]
df = pd.DataFrame(data, columns=names).set_index("id")
min_x = df["X"].min()
max_x = df["X"].max()
min_y = df["Y"].min()
max_y = df["Y"].max()

kernel = gaussian_kde(df.values.T)   # get gaussian kernel density estimate

# set up grid
grid_xs, grid_ys = np.mgrid[min_x:max_x:50j, min_y:max_y:50j]
positions = np.vstack([grid_xs.ravel(), 
                       grid_ys.ravel()])            # 2-dim array with X, Y-coordinates from xs, ys
Z = np.reshape(kernel(positions).T, grid_xs.shape)  # get densities

fig, ax = plt.subplots(1)
ax.imshow(np.rot90(Z), cmap=plt.cm.jet, extent=[min_x, max_x, min_y, max_y])
ax.set_xlim(min_x, max_x)
ax.set_ylim(min_y, max_y)
ax.scatter(df["X"], df["Y"], color="white", marker="x")

enter image description here

[当然,color maps中提供了附加的matplotlib和格式选项,可以根据需要调整输出以使其看起来像。

获取中心的位置(将中心定义为具有最高估计密度的网格位置:

idx = np.unravel_index(np.argmax(Z), Z.shape)
center = grid_xs[idx], grid_ys[idx]  # (11.604243569387755, 48.108671759387754)
© www.soinside.com 2019 - 2024. All rights reserved.