Python:在 Queen 连续的情况下,如何解释棋盘的 Pysal 结果?

Python: how to interpret Pysal results for a checkerboard in the case of the Queen contiguity?

我正在努力掌握 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]])

现在让我们使用加入计数统计(我的 cb 中的值是 01),使用 Queen 连续性规则(又名 Moore neighborhood) :

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.04+4+12。鉴于矩阵的大小和结构,我预计会有更多的连接(更改)。为什么我得到的数字与预期相差甚远?

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