【问题标题】:Python - How do I compute interactive spatial autocorrelation (Moran I) using PySAL?Python - 如何使用 PySAL 计算交互式空间自相关 (Moran I)?
【发布时间】:2023-04-04 11:05:01
【问题描述】:

我在 PostgreSQL 数据库中有一个积分表 my_table my ,其中包含 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

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

我是 Python 新手。有人可以建议我如何使用 PySAL 做到这一点吗?

【问题讨论】:

    标签: python spatial pysal


    【解决方案1】:

    我的项目中一直在使用 pysal 计算 Moran,并不是很困难。

    你必须有两个对象:

    1. 权重矩阵,其中包含您尝试验证自相关的单元格之间的关系程度。

    2. 数据

    在我的项目中,验证自相关的单元格就像一个马赛克,所以我可以使用Contiguity Based Weights的方法计算权重矩阵,这是有效的。

    为二维数据矩阵 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])
    
     # Crate the pysal Moran object 
     mi = Moran(Z, w)
    
     # Verify Moran's I results 
     print(mi.I) 
     print(mi.p_norm)
    

    我建议最初使用随机矩阵 Z,因为在这种情况下,Moran 的 I 的结果应该在 0 左右,这是一个很好的测试。在对随机 Z 数据进行所有操作之后,您可以将 Z 替换为您的真实数据。

    【讨论】:

    • 我现在看到的是 'pysal' has no attribute 'lat2W' 。 API 可能已更改。 from libpysal.weights import lat2W from esda.moran import Moran import numpy as np Z = np.array([[0,1,0],[1,0,1],[0,1,0]]) # Create the matrix of weigthts w = lat2W(Z.shape[0], Z.shape[1]) # Crate the pysal Moran object mi = Moran(Z, w) # Verify Moran's I results print(mi.I) print(mi.p_norm)
    • 如果我想使用自定义的 w 权重矩阵怎么办?我该如何创建它?它需要是一个 libpysal.weights.weights 对象才能运行......编辑:算了,已经找到答案了,我必须用 libpysal.weights.W 构建一个 w 对象
    【解决方案2】:

    我猜你的意思是相关而不是自相关?

    https://en.wikipedia.org/wiki/Autocorrelation

    你能用 Pandas 代替吗?

    https://pandas.pydata.org/pandas-docs/stable/visualization.html

    Pandas correlate

    import pandas
    import matplotlib.pyplot as plt
    
    data = pandas.read_csv("C:\\Users\\4Sight\\Desktop\\test.csv", sep=" +", usecols=("val1", "val2"))
    
    print data
    
    print data.columns.values
    
    print data["val1"].corr(data["val2"])
    
    plt.figure()
    data.plot()
    plt.show()
    

    【讨论】:

    • 你得到了我的支持。不幸的是,不完全是我正在寻找的。请参阅我添加了更多详细信息以使事情变得清晰的编辑。我的目标是使用 Python 获得预期的结果。列值是我的 Postgres 数据库中 shapefile 的属性,因此我想调查它们是否是空间自相关的。请看:gisgeography.com/spatial-autocorrelation-moran-i-gis
    • Pandas 是一个 Python 库。看起来该链接包含您需要的信息。您可以将 CSV 文件读入 numpy,然后调用该指南中的函数。
    • 这个帖子应该被删除。问题是关于空间自相关。
    猜你喜欢
    • 1970-01-01
    • 2017-11-16
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多