【问题标题】:Python: how to interpret Pysal results for a checkerboard in the case of the Queen contiguity?Python:在皇后邻接的情况下,如何解释棋盘的 Pysal 结果?
【发布时间】:2017-03-14 10:49:56
【问题描述】:

我正在努力掌握pysal。假设我有一个这样创建的棋盘:

import numpy as np
import pysal

def build_checkerboard(w, h) :
    re = np.r_[ w*[0,1] ]        # even-numbered rows
    ro = np.r_[ w*[1,0] ]        # odd-numbered rows
    return np.row_stack(h*(re, ro))

cb = build_checkerboard(5, 5)

现在我删除最后一行和最后一列以匹配pysal 的权重矩阵中可用的维度:

cb = np.delete(cb, (9), axis=0)
cb = np.delete(cb, (9), axis=1)

In[1]: cb
Out[1]:
array
  ([[0, 1, 0, 1, 0, 1, 0, 1, 0],
   [1, 0, 1, 0, 1, 0, 1, 0, 1],
   [0, 1, 0, 1, 0, 1, 0, 1, 0],
   [1, 0, 1, 0, 1, 0, 1, 0, 1],
   [0, 1, 0, 1, 0, 1, 0, 1, 0],
   [1, 0, 1, 0, 1, 0, 1, 0, 1],
   [0, 1, 0, 1, 0, 1, 0, 1, 0],
   [1, 0, 1, 0, 1, 0, 1, 0, 1],
   [0, 1, 0, 1, 0, 1, 0, 1, 0]])

现在让我们使用 Queen 邻接规则(又名 Moore neighborhood)使用 Join Count Statistics(我的 cb 中的值是 01):

w=pysal.lat2W(3,3, rook=False) #This yields a 9x9 matrix
jc=pysal.Join_Counts(cb,w) #The Join Counts

现在,结果:

In[2]: jc.bb #The 1-to-1 joins
Out[2]: array([ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.])

In[3]: jc.bw #The 1-to-0 joins
Out[3]: array([ 12.,  12.,  12.,  12.,  12.,  12.,  12.,  12.,  12.])

In[4]: jc.ww #The 0-to-0 joins
Out[5]: array([ 4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.,  4.])

In[5]: jc.J #The total number of joins
Out[5]: 20.0

我的问题:

  1. 为什么我没有得到不同连接的单个值,而是一个数组?此外,数组的每个值似乎都指向一个矩阵单元,但我没有得到总和。
  2. 20.0 的连接总数为4+4+12。考虑到矩阵的大小和结构,我预计会有更多的连接(更改)。为什么我得到的数字与预期的相差甚远?

【问题讨论】:

    标签: python statistics spatial correlation pysal


    【解决方案1】:

    pysal.JoinCounts 的第一个参数是维度为 (n,) 的数组 对于你的棋盘格,我认为你想要这样的东西:

    >>> import numpy as np
    >>> import pysal as ps
    >>> w = ps.lat2W(3, 3, rook=False) # n=9, so W is 9x9
    >>> y = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0]) # attribute array n elements
    >>> jc = ps.Join_Counts(y, w)
    >>> jc.bb  # number of bb joins
    4.0
    >>> jc.ww  # number of ww joins
    4.0
    >>> jc.bw  # number of bw (wb) joins
    12.0
    >>> w.s0   # 2x total number of joins
    40.0
    >>> w.s0 == (jc.bb + jc.ww + jc.bw) * 2
    True
    

    更多详情请见guide

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2016-02-21
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多