在PySal中创建单变量Moran散点图

时间:2016-04-02 10:30:57

标签: python geospatial pysal

我正在尝试使用PySAL创建一个Moran的散点图 - 带有HH / HL / LH / LL象限的那个 - 并且我已经到了那里但是想检查我的理解/解释/代码。下面的代码使用内置的北卡罗来纳州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's I(通过ps.Moran_Local(...))这个被称为莫兰散点图......

0 个答案:

没有答案
相关问题