2

I have a points table my_table my in PostgreSQL database with geometry column and other attributes. I have some sample data of my_table like follows (attributes of my_table).

id  val1
1   72.54513286
2   73.67371014
3   74.204424
4   73.76017279
5   77.7912762
6   77.78789496
7   65.51822878
8   65.5182287
9   74.65885753
10  74.65885753
11  61.18084042
12  60.75827621
13  64.27716322
14  63.69432836
15  75.790405
16  60.95270235
17  79.12399503
18  62.9667706
19  78.1265630

Using Python PySAL package, I would like to analyse that whether values in column val1 are sptially autocorrelated (Moran I) (by interatively plotting them). My expected output of interactive spatial autocorrelation could be like (image source, here):

expected_output

I am new to Python. Can someone suggest me how to do this using PySAL?

khajlk
  • 791
  • 1
  • 12
  • 32

2 Answers2

5

I have been using PySAL to compute Moran's I in my projects, and I find it relatively straightforward.

To compute Moran's I, you need two objects:

  1. A weight matrix that represents the degree of relationship between the cells for which you want to verify autocorrelation.
  2. The data itself.

In my projects, the cells I examine for autocorrelation are arranged like a mosaic. Therefore, I compute the weight matrix using the Contiguity Based Weights method, which is quite effective.

Here's an example of how to compute Moran's I for a two-dimensional data matrix, Z:

 from libpysal.weights import lat2W
 from esda.moran import Moran
 import numpy as np

 # Use your matrix here, instead of this random one
 Z = np.random.rand(200,150)

 # Create the matrix of weigthts 
 w = lat2W(Z.shape[0], Z.shape[1])

 # Create the pysal Moran object 
 mi = Moran(Z, w)

 # Verify Moran's I results 
 print(mi.I) 
 print(mi.p_norm)

I suggest starting with a random matrix, Z. In this instance, the outcome of Moran's I should approximate 0, serving as an effective test. Once you've verified it works with the random Z data, you can then substitute in your actual data.

  • 1
    What I'm seeing now is `'pysal' has no attribute 'lat2W'` . the API may have changed. ```from libpysal.weights import lat2W from esda.moran import Moran import numpy as np Z = np.array([[0,1,0],[1,0,1],[0,1,0]]) # Create the matrix of weigthts w = lat2W(Z.shape[0], Z.shape[1]) # Crate the pysal Moran object mi = Moran(Z, w) # Verify Moran's I results print(mi.I) print(mi.p_norm)``` – user3450049 May 13 '21 at 18:30
  • What if I want to use a custom w matrix of weights? How do I create that? It needs to be a libpysal.weights.weights object to function... EDIT: forget it, found the answer already, I have to build a w object with libpysal.weights.W – Ricardo Guerreiro Sep 23 '21 at 11:37
-3

I guess you mean correlated not auto-correlated?

https://en.wikipedia.org/wiki/Autocorrelation

Can you use Pandas instead?

https://pandas.pydata.org/pandas-docs/stable/visualization.html

Pandas correlate

import pandas
import matplotlib.pyplot as plt

data = pandas.read_csv("C:\\Users\\4Sight\\Desktop\\test.csv", sep=" +", usecols=("val1", "val2"))

print data

print data.columns.values

print data["val1"].corr(data["val2"])

plt.figure()
data.plot()
plt.show()
ziggy jones
  • 401
  • 2
  • 7
  • You get my upvote. Unforturnately, not exactly I am looking for. Please see the edit I have added more details to make things clear. My aim is to get my expected outcome using Python. The column values are attributes of a shapefile in my Postgres db thus I want to investigate whether they are spatial auto-correlated. Please see: http://gisgeography.com/spatial-autocorrelation-moran-i-gis/ – khajlk Aug 23 '17 at 13:05
  • Pandas is a Python library. Looks like that link has the information you need. You can read in the CSV into [numpy](https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html) and then call the functions in that guide. – ziggy jones Aug 23 '17 at 13:26
  • 2
    This post should be deleted. The question is about spatial autocorrelation. – Ollie Perkins Feb 01 '22 at 17:39