Python - 如何使用 PySAL 计算交互式空间自相关 (Moran I)?

kha*_*jlk 2 python spatial pysal

my_table我在 PostgreSQL 数据库中有一个点表,其中包含geometry列和其他属性。我有一些my_table如下的示例数据( 的属性my_table)。

id  val1
1   72.54513286
2   73.67371014
3   74.204424
4   73.76017279
5   77.7912762
6   77.78789496
7   65.51822878
8   65.5182287
9   74.65885753
10  74.65885753
11  61.18084042
12  60.75827621
13  64.27716322
14  63.69432836
15  75.790405
16  60.95270235
17  79.12399503
18  62.9667706
19  78.1265630
Run Code Online (Sandbox Code Playgroud)

使用 Python PySAL包,我想分析列中的值是否val1空间自相关(Moran I)(通过交互绘制它们)。我的交互式空间自相关的预期输出可能如下(图像来源,此处):

预期输出

我是Python新手。有人可以建议我如何使用 PySAL 执行此操作吗?

Jos*_*roz 6

我一直在项目中使用 PySAL 来计算 Moran's I,我发现它相对简单。

要计算 Moran's I,您需要两个对象:

  1. 权重矩阵,表示要验证自相关的像元之间的关系程度。
  2. 数据本身。

在我的项目中,我检查自相关的单元格排列得像马赛克一样。因此,我使用基于连续性的权重方法来计算权重矩阵,这是相当有效的。

以下是如何计算二维数据矩阵 Z 的 Moran's I 的示例:

 from libpysal.weights import lat2W
 from esda.moran import Moran
 import numpy as np

 # Use your matrix here, instead of this random one
 Z = np.random.rand(200,150)

 # Create the matrix of weigthts 
 w = lat2W(Z.shape[0], Z.shape[1])

 # Create the pysal Moran object 
 mi = Moran(Z, w)

 # Verify Moran's I results 
 print(mi.I) 
 print(mi.p_norm)
Run Code Online (Sandbox Code Playgroud)

我建议从随机矩阵 Z 开始。在这种情况下,Moran's I 的结果应该接近 0,作为有效的测试。一旦您验证了它适用于随机 Z 数据,您就可以替换为您的实际数据。