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

2024-05-19 14:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用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()

这就产生了一些看起来合理的东西,但我认为我已经把自己想成了结,因为我还没有真正计算过莫兰的I(通过ps.Moran_Local(...)),这就是莫兰散射图。。。在


Tags: andimportashhnpplthlps