Spatial Weights
Spatial Weights 空间矩阵
- 
Spatial Weights, site 
1. 概念
空间权重矩阵(Spatial Weight): \([w_{ij}]\) 用于描述(地理)空间中位置之间的的邻近关系或相似性(similarity)
- 
二元矩阵:\(w_{ij}=1\) 表示位置 \(i\) 和位置 \(j\) 相邻,或者之间存在影响 
- 
实数矩阵:数值越大表示位置 \(i\) 和位置 \(j\) 越相似(或者之间影响越大) 
2. Spatial Weight Matrix in PySAL.libpysal.weights
2.1 libpysal.weights module in PySAL library
PySAL library spatial weight module : libpysal.weights
- import weight module from PySAL
from pysal.lib import weights
2.2 The common Class Object: libpysal.weights.W()
libpysal.weights.W() 类为所有 libpysal.weights 模块下构建 spatial weight 的返回类型:
- 
均用于构建 contiguity weight, i.e., binary weight matrix Observations are usually either considered "linked" (non-zero or one) or "not linked" (zero). 
(1) 初始化方法
- 直接初始化
weights.W(neighbors, weights=None, id_order=None, silence_warnings=False, ids=None)
- 
通过 geopandas.GeoDataFrame() 初始化 
- 
.from_dataframe(df, geom_col=None, idVariable=None)- 
df (DataFrame): 也可为GeoDataFrame类型
- 
geom_col (str, default=Nonw):
- 
idVariable (str, default=Nonw), if None,df.indexis used
 
- 
(2) 属性和方法
- 
Attributes - 
w.neighbors: return a dictionary recording the neighbor relationships (by id)
- 
w.s0: return the number of join, represented by a nonzero weight in a weight matrix. See this equation
- 
w.nreturn the number of units (nodes, polygon, or some other geometry object)
- 
w.nonzero: return the number of nonzero weights.
- 
w.pct_nonzero: return the percentage of nonzero weights, i.e., the density (compliment of sparsity) of the spatial weights matrix, equal to100 * w.s0 / (w.n * w.n)
- 
w.cardinalities: return a dictionary reporting the number of neighbors for each observation
- 
w.histogram: return a tuple list reporting the distribution of the cardinalities
- 
w.transform: same tow.set_transform()
 
- 
- 
Functions: - 
w.set_transform(value='B'):- 
B: Binary
- 
R: Row-standardization (global sum = \(n\), for each row sum = \(1\))
- 
D: Double-standardization (global sum = \(1\))
- 
V: Variance stabilizing
- 
O: Restore original transformation (from instantiation)
 
- 
- 
w.full(): generate a full numpy array. Return a tuple:- 
first element being the full numpy array 
- 
second element keys being the ids associated with each row in the array. 
 
- 
 
- 
- 
实例代码 
# 获得权重矩阵,转换为 pandas.DataFrame 类型
pd.DataFrame(w.full()[0], index=w.full()[1], columns=w.full()[1])
# Matrix normalization
w.transform = "R"	# Row normalization
# or
w.set_transform('R')
2.3 Contiguity weights
w = weights.contiguity.Rook.from_dataframe()
w = weights.contiguity.Queen.from_dataframe()
- 
Rook: Construct a weights object from a collection of pysal polygons that share at least one edge. 
- 
Queen: Construct a weights object from a collection of pysal polygons that share at least one vertex. 
2.4 Distance based weights
A matrix expressing the distances between all pairs of observations
2.4.1 K-nearest neighbor weights
- 
The distance based weights defines the neighbor set of a particular observation as containing its nearest observations 
- 
Return a binary matrix 
weights.distance.KNN(data, k=2, p=2, 
                     ids=None, radius=None, 
                     distance_metric='euclidean')
- 
parameters - 
k (int): number of nearest neighbors
- 
p (float): Minkowski p-norm distance metric parameter
 
- 
2.4.2 Kernel weights
weights.distance.Kernel(bandwidth=None, fixed=True, k=2, 
                        function='triangular', diagonal=False,
                        distance_metric='euclidean', **kwargs)
- 
Parameters: - 
bandwidth (float or or array-like),array-likeindicates a specific bandwidth \(h_i\) for each \(i\) geometry
- 
fixed (bool):Truefor fixed bandwidth,Falsefor adaptive bandwidth
- 
k (int): the number of nearest neighbors to use for determining bandwidth.- 
For fixed bandwidth, \({h_i = \max_k (d_{KNN}) \forall i}\), where \({d_{KNN}}\) is a vector of k-nearest neighbor distances, i.e., \(h_i\) is the distance to the \(k\)th nearest neighbor for each observation 
- 
For adaptive bandwidths, \(h_i=d_{KNN,i}\) 
 
- 
- 
diagonal (bool, default=False):Trueset diagonalweights = 1.0,Falsediagonals weights are set to value according to kernel function.
- 
function (str in {'triangular','uniform', 'quadratic', 'quartic', 'gaussian'}): kernel function
 
- 
2.4.3 Distance bands and hybrid Weights
weights.DistanceBand(data, threshold, p=2, alpha=-1.0,
                     binary=True, ids=None,
                     distance_metric='euclidean', radius=None)
- 
Parameter: - 
threshold (float): distance band
- 
p (float): Minkowski p-norm distance metric parameter
- 
binary (bool):- 
If true \(w_{ij}=1\) if \(d_{ij}<=\text{threshold}\), otherwise \(w_{ij}=0\) 
- 
If false \(w_{ij}=d_{ij}^{\alpha}\) 
 
- 
- 
alpha (float, default -1.0):- 
distance decay parameter for weight 
- 
if alphais positive the weights will not decline with distance
- 
If binary=True,alphais ignored
 
- 
 
- 
3.3 Great circle distances
4 Block weights
weights.block_weights(regimes, ids=None, **kwargs)
Construct spatial weights for regime neighbors.
Block contiguity structures are relevant when defining neighbor relations based on membership in a regime. For example, all counties belonging to the same state could be defined as neighbors, in an analysis of all counties in the US.
- 
Parameters: - 
regimes (list or array-like), ids of which regime an observation belongs to
- 
ids (list, array-like), ordered sequence of IDs for the observations
 
- 
5 Set operations on weights
Reference
Sergio Rey, Dani Arribas-Bel, Levi John Wolf, 2020, "Spatial Weights" in Geographic Data Science with Python, site
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号