如何使用最近的邻居使用地球界的平均值?

发布于 2025-02-03 05:40:14 字数 453 浏览 4 评论 0原文

我有一个GeodataFrame(“ GDF”),其中一个列为“值”,另一列为“几何”(实际上是实际的地理区域),因此每行代表一个区域。

“值”列在许多行中为零,在某些行中为大数字。

我需要使用最近的邻居进行“移动平均值”或平均值,直到某个“ max_distance”(我们可以假设GDF具有本地投射的CRS,因此MAX_DISTANCE具有实际含义)。 因此,在大多数区域中,平均值既不为零或大值,而是平均值。

一种方法是

for region in GDF:
    averaged_values=sjoin_nearest(GDF,GDF,maxdistance=1000).values.mean()

,但实际上我不知道该如何进行。

预期的输出将是带有3列的GeodataFrame: “值”,“平均_VALUES”和“几何”。

有什么想法吗?

I have a geodataframe ("GDF") with one column as "values", and another column as "geometry" (that in fact are actual geographical regions), so each row represents a region.

The "values" column is zero in many rows, and a large number in some rows.

I need to make a "moving average" or rolling average, using the nearest neighbors up to a certain "max_distance" (we can assume that the GDF has a locally projected CRS, so the max_distance has real meaning).
Thus, the averaged_values would have neither zero or large values in most of the regions, but an average value.

One way to do it would be

for region in GDF:
    averaged_values=sjoin_nearest(GDF,GDF,maxdistance=1000).values.mean()

But really I don't know how to proceed.

The expected output would be a geodataframe with 3 columns:
"values", "averaged_values", and "geometry".

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2025-02-10 05:40:14

您要做的也称为A空间滞后。最好的方法是使用libpysal库来创建基于设定距离的空间权重矩阵,这是Geopandas生态系统的一部分。

import libpysal

# create weights
W = libpysal.weights.DistanceBand.from_dataframe(gdf, threshold=1000)

# row-normalise weights
W.transform = "r"

# create lag
gdf["averaged_values"] = libpysal.weights.lag_spatial(W, gdf["values"])

What you are trying to do is also called a spatial lag. The best way is to create spatial weights matrix based on a set distance and compute the lag, both using libpysal library, which is a part of the geopandas ecosystem.

import libpysal

# create weights
W = libpysal.weights.DistanceBand.from_dataframe(gdf, threshold=1000)

# row-normalise weights
W.transform = "r"

# create lag
gdf["averaged_values"] = libpysal.weights.lag_spatial(W, gdf["values"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文