局部空间自相关
import esda
import numpy as np
import pandas as pd
import libpysal as lps
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
from geopandas import GeoDataFrame
from shapely.geometry import Point
from pylab import figure, scatter, show
from splot.esda import moran_scatterplot
from esda.moran import Moran_Local
from splot.esda import plot_moran
from splot.esda import lisa_cluster
from splot.esda import plot_local_autocorrelation
%matplotlib inline
root_dir="/home/lighthouse/Learning/pysal/"
gdf = gpd.read_file(root_dir+'data/.shp')
数据概况
gdf.columns.values
array(['CODE', 'COUNT', 'SUM_AREA', 'FIRST_ANAM', 'OID_', 'CODE_1',
'DATAFLAG', 'TOTPOP', 'TOTPOP_10K', 'RURPOP_10K', 'TOWNPOP_10',
'AGRPRODUCT', 'AGRLBR_10K', 'AGRSTOTGDP', 'FSTGDPRATE',
'SCNDGDPRAT', 'THRDGDPRAT', 'Province', 'geometry'], dtype=object)
gdf.head(1)
计算局部空间自相关LISA
ax=gdf.plot(figsize=(8,8),column="FSTGDPRATE",scheme='Quantiles', k=5, cmap='GnBu', legend=True,)
ax.set_axis_off()
计算空间权重矩阵
- pysal中的局部自相关计算中,要求权重矩阵的每一个元素都有邻接元素
y = gdf['FSTGDPRATE'].values
w = lps.weights.distance.Kernel.from_dataframe(gdf, fixed=False, k=15)
w.transform = 'r'
moran_loc = Moran_Local(y, w)
fig, ax = moran_scatterplot(moran_loc, p=0.05)
ax.set_xlabel('FSTGDPRATE')
ax.set_ylabel('Spatial Lag of FSTGDPRATE')
plt.show()
lisa_cluster(moran_loc, gdf, p=0.05, figsize = (9,9))
plt.show()
plot_local_autocorrelation(moran_loc, gdf, 'FSTGDPRATE')
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vs9999Lu-1676603398739)(null)]
参考链接
- https://pysal.org/libpysal/
- https://pysal.org/esda/index.html
- https://github.com/pysal/esda
- http://pysal.org/notebooks/viz/splot/esda_morans_viz.html
- http://darribas.org/gds_scipy16/ipynb_md/04_esda.html
- https://splot.readthedocs.io/en/stable/users/tutorials/autocorrelation.html