I am trying to get a grip on pysal. Say I have a checkerboard created like this:
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)
Now I delete the last row and column to match the dimensions available in the weight matrix of 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]])
Now let's use the Join Count Statistics (the values in my cb
are either 0
or 1
) using the Queen contiguity rule (aka the Moore neighborhood):
w=pysal.lat2W(3,3, rook=False) #This yields a 9x9 matrix
jc=pysal.Join_Counts(cb,w) #The Join Counts
Now, the results:
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
My questions:
20.0
in terms of total number of joins is 4+4+12
. Given the size and structure of the matrix I expected a higher number of joins (changes). Why is the number I get a far cry from what is expected?The first argument to pysal.JoinCounts
is an array of dimension (n,)
For your checkerboard case I think you want something like:
>>> 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
For more details see the guide.