Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to calculate Global Moran Index

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Python to calculate the global Moran index. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

Global spatial autocorrelation

Spatial autocorrelation (spatial autocorrelation) refers to the potential interdependence of observed data of some variables in the same distribution area. Tobler (1970) pointed out that "the first law of geography is that everything is related to something else, but what is near is more relevant than what is far away."

Global Moran Index (Global Moran's I) is the most commonly used spatial autocorrelation index, which is used to reflect the global spatial correlation.

$I =\ frac N W\ frac {\ sum_i\ sum_j w _ {ij} (xfanti-\ bar x) (xfantj -\ bar x)} {\ sum_i (xannii-\ bar x) ^ 2} $$

In the formula, $N$ represents the number of spatial units, $i$ and $j$ represent the location index of spatial units, $x$ is the analytical variable, $\ bar x$ represents the mean of the analytical variable, $w{ ij} $represents the spatial weight matrix, and $W$ represents the sum of the spatial weight matrix.

When Moran's I is greater than 0, the data shows positive spatial correlation, and the larger the value, the more obvious the spatial correlation; when Moran's I is less than 0, the data shows negative spatial correlation, and the smaller the value, the greater the spatial difference; when Moran's I is 0, the spatial correlation is random.

When interpreting Moran index, it needs to be judged by P value and Z score. P value is less than 0.05 (passing 95% confidence test), and Z score exceeds the critical value 1.65 (rejects the threshold set by zero hypothesis).

Tool introduction

PySAL is an open source library for exploratory spatial data analysis based on Python. It is a community project sponsored by Arizona State University GeoDa Center for Geospatial Analysis and Computation.

Libpysal: is the core library of Python spatial analysis library, which provides four modules and constitutes the spatial analysis tools of the PySAL series:

Spatial weights: libpysal.weights

Input and output: libpysal.io

Computational Geometry: libpysal.cg

Built-in sample dataset libpysal.examples

Esda is an open source Python library for exploratory analysis of spatial data. It is a subpackage of PySAL (Python Spatial Analysis Library), including global and local spatial autocorrelation analysis methods.

Import esda import pandas as pdimport geopandas as gpdfrom geopandas import GeoDataFrameimport libpysal as lpsimport numpy as npimport matplotlib.pyplot as pltfrom shapely.geometry import Pointimport contextily as ctxfrom pylab import figure, scatter, show%matplotlib inlineroot_dir= "/ root/learning/pysal/ Lab 1 /" gdf = gpd.read_file (root_dir+'data/econ90040212.shp') # read data profile gdf.columns.values # Field name 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) # View a record

Calculate the global spatial autocorrelation index Moran's I

The proportion of primary industry in GDP FSTGDPRATE is a variable

Ax=gdf.plot (figsize= (8), column= "FSTGDPRATE") ax.set_axis_off ()

It can be seen that there is a certain degree of spatial aggregation, but the degree of aggregation needs to be further measured by the calculation of Moran's I.

Calculate the spatial weight matrix df = gdfwq = lps.weights.Queen.from_dataframe (df) # using the Quuen adjacency matrix wq.transform ='r' # normalized matrix ('WARNING:', 98,'is an island (no neighbors)') ('WARNING:', 108,'is an island (no neighbors)') ('WARNING:', 137,137,'is an island (no neighbors)') ('WARNING:', 138 'is an island (no neighbors)') ('WARNING:', 139,'is an island (no neighbors)') / usr/local/lib/python3.6/dist-packages/libpysal/weights/weights.py:172: UserWarning: The weights matrix is not fully connected: There are 6 disconnected components. There are 5 islands with ids: 98, 108, 137, 138, 139. Warnings.warn (message)

You can see that there are five units that do not have adjacent objects.

Next, the calculated weight matrix is drawn for observation.

Centroids = gdf.geometry.centroid # Compute Polygon Geometry Center fig = figure (figsize= (8)) plt.plot (centroids.x, centroids.y,'.') for KLY in wq.neighbors.items (): # print (KLN) origin = centroids [k] for neigh in neighs: segment = centroids [KNeigh]] plt.plot (segment.x, segment.y) '-' plt.title ('Queen Neighbor Graph') plt.axis (' off') plt.show ()

Wr = lps.weights.Rook.from_dataframe (df) # use Rook adjacency matrix # wr.transform ='r'# Normalized matrix fig = figure (figsize= (8)) plt.plot (centroids.x, centroids.y,'.') for Knowles in wr.neighbors.items (): # print (KMH Nighes) origin = centroids [k] for neigh in neighs: segment = centroids [[KMN Neigh]] plt.plot (segment.x Segment.y,'-') plt.title ('Rook Neighbor Graph') plt.axis (' off') plt.show ()

Calculate Moran'Iy=gdf ["FSTGDPRATE"] mi = esda.moran.Moran (y, wq) print ("Moran's I value is:", mi.I) print ("Z test value is:", mi.z_rand) print ("Z test value under random distribution hypothesis is:", mi.p_rand) print ("Z test value under normal distribution hypothesis is:", mi.z_norm) print ("Z test value under normal distribution hypothesis is:" Mi.p_norm) Moran's I is: 0.7133088262106428 under the random distribution hypothesis: 16.7858372659958 under the random distribution hypothesis: 0.0 under the normal distribution hypothesis: 16.826039087303325 under the normal distribution hypothesis, the Z test P is: 0.0

P

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report