我正试着抓住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 contiguity规则(又名Moore neighborhood)来使用Join Count Statistics(cb
中的值为0
或1
):< / p>
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
我的问题:
20.0
,4+4+12
为var codeExample = $(".stuff").html();
$(codeExample).attr("id").val("2412");
$(codeExample).find("div #foo").attr("rel").val("bar");
$("#element").append(codeExample);
。鉴于矩阵的大小和结构,我预计会有更多的连接(变化)。为什么我的数字与预期相差甚远?答案 0 :(得分: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。