pysal / esda

statistics and classes for exploratory spatial data analysis
https://pysal.org/esda
BSD 3-Clause "New" or "Revised" License
206 stars 53 forks source link

Spatial_Pearson issue #268

Open rudyarthur opened 9 months ago

rudyarthur commented 9 months ago
from scipy import sparse
import libpysal
import esda

print(libpysal.__version__)
print(esda.__version__)

x = np.array([[1,1],
              [0,1]])
y = np.array([[0,1],
              [1,1]])
w = np.array([[0,1,1,0],
              [1,0,0,1],
              [1,0,0,1],
              [0,1,1,0]])
sw = sparse.csr_matrix(w)

from esda.lee import Spatial_Pearson

sp = Spatial_Pearson()
sp.fit(x,y)
print(sp.association_)

sp = Spatial_Pearson(sw)
sp.fit(x,y)
print(sp.association_)

The above with pysal version 4.7.0 and esda version 2.5.0 doesn't produce what it ought to. The first fit returns a 4x4 array where the docs say to expect a 2x2. The second crashes with "dimension mismatch" error.

Looking at the code in lee.py I think the x and y inputs need to be flattened. The default connectivity is the unit matrix which is probably never what is required in practice. I'd suggest requiring the user to input the spatial weight matrix as in the moran code.

Cheers, Rudy

ljwolf commented 9 months ago

Thanks for the report!

The input x and y arrays must be of shape (n,1). So, I think the flattening should happen in the user code.

We can definitely improve documentation and force the validation step to catch this, however!