I'm using PySAL for work and I have encounter a problem. I want to calculate Moran’s I for serval fields, I can open and create a weight matrix from my shapefile (w) however I don't know how to select a specific field (y).

mir = pysal.Moran(y, w, permutations = 9999)


I just want something like :

f = "Path to my shapefile"
y = array from f

I don't have arcpy to read my shapefile, which I guess would make think easier.


Suppose you have a shapefile named cities.shp. In order to read the attribute table, you need to read .dbf where the attributes are stored.

import pysal as ps
dbf = ps.open(r"C:\GIS\Temp\cities.dbf")

You can then call:

ps.Moran(dbf.by_col('POP1990'), w)

Look for more examples on reading a shapefile here.

  • Thank you. That's more or less what I end up doing : shp = ps.open(r"C:\GIS\Temp\cities.shp") w = ps.weights.Queen.from_shapefile(shp) dbf = ps.open(r"C:\GIS\Temp\cities.dbf") y = f.by_col_array('POP1990') I = (y, w). It works but I don't understand what unique id is used to match the shapefile and the dbf. Obviously they share have an unique and shared id for each row, but how do Pysal know witch column stores the id? I also came to the conclusion that the results are quite different between PySAL ard ArcGis, but they use the same formula so I'm wondering what cause the differences. – Victor A Aug 10 '17 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.