1

我正在尝试使用 PySAL(具有 HH/HL/LH/LL 象限的那个)创建一个 Moran 散点图,并且认为我已经到了那里,但想检查我的理解/解释/代码。下面的代码使用内置的 North Carolina SIDS 数据集和行标准化。

import numpy as np
import pysal as ps
import matplotlib.pyplot as plt
import matplotlib.cm as cos

# shpdir is wherever the PySAL example data are installed
col = 'SIDR74' 
w = ps.open(os.path.join(shpdir,"sids2.gal")).read()
f = ps.open(os.path.join(shpdir,"sids2.dbf"))
y = np.array(f.by_col(col))

w.transform = 'r'

### Are these next three steps right? ###

# Calculate the spatial lag
yl  = ps.lag_spatial(w, y)

# Z-Score standardisation
yt   = (y - y.mean())/y.std()
ylt  = (yl - yl.mean())/yl.std()

# Elements of a Moran's I Scatterplot
# X-axis = z-standardised attribute values
# Y-axis = z-standardised lagged attribute values
# Quadrants = HH=1, LH=2, LL=3, HL=4
#
# So from that it follows that:
# HH == ylt > 0 and yt > 0 = 1
# LH == ylt > 0 and yt < 0 = 2
# LL == ylt < 0 and yt < 0 = 3
# HL == ylt < 0 and yt > 0 = 4

# Initialise an array with a default
# value to hold the quadrant information
quad = np.zeros(yt.shape)
quad[np.bitwise_and(ylt > 0, yt > 0)]=1 # HH
quad[np.bitwise_and(ylt > 0, yt < 0)]=2 # LH
quad[np.bitwise_and(ylt < 0, yt < 0)]=3 # LL
quad[np.bitwise_and(ylt < 0, yt > 0)]=4 # HL

plt.scatter(yt, ylt, c=quad, cmap=cms.summer)
plt.suptitle("Moran Scatterplot?")
plt.show()

这产生了一些看似合理的东西,但我认为我已经认为自己陷入了困境,因为我还没有实际计算出 Moran 的 I (通过ps.Moran_Local(...)),这被称为 Moran 散点图......

4

0 回答 0