In [1]:
indiaMapsLink = 'https://github.com/FabioGM-02/Tarea-5/raw/refs/heads/main/Maps/indiaMaps_7755.gpkg'

import geopandas as gpd

gpd.list_layers(indiaMapsLink)
Out[1]:
name geometry_type
0 country MultiPolygon
1 cities Point
2 rivers MultiLineString
3 centroid Point
4 airports Point
5 states MultiPolygon
In [2]:
states=gpd.read_file(indiaMapsLink,layer='states')
In [3]:
countries = gpd.read_file(indiaMapsLink, layer='country')

# Reprojecting the states
states_fixed = states.copy()
states_fixed = states_fixed.set_crs(epsg=32643, allow_override=True)
states_fixed = states_fixed.to_crs(countries.crs)

print("Fixed States bounds:", states_fixed.total_bounds)
print("Countries bounds:", countries.total_bounds)

states = states_fixed
Fixed States bounds: [2819242.64544075 2177754.67403902 5679168.18314899 5260289.41141887]
Countries bounds: [2814964.32964461 2176798.65900902 5676285.14421876 5260475.76432007]
In [4]:
import pandas as pd

# Loading an excel where all our variables are at district-level
hdi_url = 'https://github.com/alonso-mendoza/Tarea-6/raw/refs/heads/main/data/india%20HDI.xlsx'
hdi = pd.read_excel(hdi_url)
In [5]:
# Merging our geometry with the data
districts=hdi.merge(states,left_on='District', right_on='NAME_2')
districts.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 633 entries, 0 to 632
Data columns (total 23 columns):
 #   Column                  Non-Null Count  Dtype   
---  ------                  --------------  -----   
 0   State/Union Territory   633 non-null    object  
 1   District                633 non-null    object  
 2   L                       633 non-null    float64 
 3   E                       633 non-null    float64 
 4   H                       633 non-null    float64 
 5   HDIa                    633 non-null    float64 
 6   Rank in HDIa            633 non-null    int64   
 7   Inequality in Progress  633 non-null    float64 
 8   ID_0                    633 non-null    int64   
 9   ISO                     633 non-null    object  
 10  NAME_0                  633 non-null    object  
 11  ID_1                    633 non-null    int64   
 12  NAME_1                  633 non-null    object  
 13  ID_2                    633 non-null    int64   
 14  NAME_2                  633 non-null    object  
 15  HASC_2                  600 non-null    object  
 16  CCN_2                   633 non-null    int64   
 17  CCA_2                   0 non-null      object  
 18  TYPE_2                  633 non-null    object  
 19  ENGTYPE_2               633 non-null    object  
 20  NL_NAME_2               0 non-null      object  
 21  VARNAME_2               184 non-null    object  
 22  geometry                633 non-null    geometry
dtypes: float64(5), geometry(1), int64(5), object(12)
memory usage: 113.9+ KB
In [6]:
# We'll keep only the districts and the variables
districts = districts[['NAME_1','District', 'L', 'E', 'H', 'HDIa', 'geometry']]

# Creating the GeoDataFrame
districts = gpd.GeoDataFrame(districts, geometry='geometry', crs=7755)
districts.head()
Out[6]:
NAME_1 District L E H HDIa geometry
0 Andaman and Nicobar South Andaman 0.919 0.952 0.979 0.950 MULTIPOLYGON (((5503734.054 2797754.016, 55059...
1 Andaman and Nicobar North and Middle Andaman 0.526 0.909 0.993 0.788 MULTIPOLYGON (((5455144.764 3082535.015, 54552...
2 Andhra Pradesh West Godavari 0.867 0.871 0.981 0.905 MULTIPOLYGON (((4164885.629 3285520.16, 416540...
3 Andhra Pradesh Y.S.R. 0.864 0.872 0.928 0.888 MULTIPOLYGON (((3887677.643 3040941.348, 38881...
4 Andhra Pradesh Chittoor 0.768 0.897 0.970 0.875 MULTIPOLYGON (((3836090.177 2903126.194, 38367...
In [7]:
districts.rename(columns={'NAME_1':'State/Union','L':'Wealth_Index_Above_2nd_Quintile','E':'Secondary_School_Attendance','H':'Prob_Survival_First5Yrs','HDIa':'HDI'},inplace=True)
districts.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 633 entries, 0 to 632
Data columns (total 7 columns):
 #   Column                           Non-Null Count  Dtype   
---  ------                           --------------  -----   
 0   State/Union                      633 non-null    object  
 1   District                         633 non-null    object  
 2   Wealth_Index_Above_2nd_Quintile  633 non-null    float64 
 3   Secondary_School_Attendance      633 non-null    float64 
 4   Prob_Survival_First5Yrs          633 non-null    float64 
 5   HDI                              633 non-null    float64 
 6   geometry                         633 non-null    geometry
dtypes: float64(4), geometry(1), object(2)
memory usage: 34.7+ KB

Exercise 6

In [9]:
!pip install mapclassify
Requirement already satisfied: mapclassify in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (2.8.1)
Requirement already satisfied: networkx>=2.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify) (3.4.2)
Requirement already satisfied: numpy>=1.23 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify) (2.1.3)
Requirement already satisfied: pandas!=1.5.0,>=1.4 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify) (2.2.3)
Requirement already satisfied: scikit-learn>=1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify) (1.6.1)
Requirement already satisfied: scipy>=1.8 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify) (1.15.2)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas!=1.5.0,>=1.4->mapclassify) (2023.3)
Requirement already satisfied: joblib>=1.2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.0->mapclassify) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.0->mapclassify) (3.6.0)
Requirement already satisfied: six>=1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from python-dateutil>=2.8.2->pandas!=1.5.0,>=1.4->mapclassify) (1.16.0)
In [10]:
# statistics
districts.HDI.describe()
Out[10]:
count    633.000000
mean       0.760378
std        0.135514
min        0.429000
25%        0.658000
50%        0.773000
75%        0.879000
max        1.000000
Name: HDI, dtype: float64
In [11]:
import seaborn as sea

sea.histplot(districts.HDI, color='yellow')
Out[11]:
<Axes: xlabel='HDI', ylabel='Count'>
No description has been provided for this image
In [12]:
districts.explore(
    column="HDI",
    scheme="fisherjenks",
    legend=True,
    tooltip=False,
    popup=['State/Union', 'District'],  # show popup (on-click)
    legend_kwds=dict(colorbar=False)
)
Out[12]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [13]:
!pip install libpysal
Requirement already satisfied: libpysal in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (4.13.0)
Requirement already satisfied: beautifulsoup4>=4.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (4.12.3)
Requirement already satisfied: geopandas>=0.10.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (1.0.1)
Requirement already satisfied: numpy>=1.22 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (2.1.3)
Requirement already satisfied: packaging>=22 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (24.2)
Requirement already satisfied: pandas>=1.4 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (2.2.3)
Requirement already satisfied: platformdirs>=2.0.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (3.10.0)
Requirement already satisfied: requests>=2.27 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (2.32.3)
Requirement already satisfied: scipy>=1.8 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (1.15.2)
Requirement already satisfied: shapely>=2.0.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (2.0.6)
Requirement already satisfied: scikit-learn>=1.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal) (1.6.1)
Requirement already satisfied: soupsieve>1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from beautifulsoup4>=4.10->libpysal) (2.5)
Requirement already satisfied: pyogrio>=0.7.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.10.0->libpysal) (0.10.0)
Requirement already satisfied: pyproj>=3.3.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.10.0->libpysal) (3.6.1)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->libpysal) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->libpysal) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->libpysal) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal) (2025.4.26)
Requirement already satisfied: joblib>=1.2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.1->libpysal) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.1->libpysal) (3.6.0)
Requirement already satisfied: six>=1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.4->libpysal) (1.16.0)
In [14]:
from libpysal.weights import Queen, Rook, KNN

# rook
w_rook = Rook.from_dataframe(districts,use_index=False)
C:\Users\mendo\anaconda3\envs\dataespacial__31111\Lib\site-packages\libpysal\weights\contiguity.py:61: UserWarning: The weights matrix is not fully connected: 
 There are 6 disconnected components.
 There are 4 islands with ids: 0, 1, 127, 282.
  W.__init__(self, neighbors, ids=ids, **kw)
In [15]:
w_rook.islands
Out[15]:
[0, 1, 127, 282]
In [16]:
# queen
w_queen = Queen.from_dataframe(districts,use_index=False)
C:\Users\mendo\anaconda3\envs\dataespacial__31111\Lib\site-packages\libpysal\weights\contiguity.py:347: UserWarning: The weights matrix is not fully connected: 
 There are 5 disconnected components.
 There are 3 islands with ids: 0, 1, 282.
  W.__init__(self, neighbors, ids=ids, **kw)
In [17]:
w_queen.islands
Out[17]:
[0, 1, 282]
In [18]:
districts.iloc[w_queen.islands,:].explore()
Out[18]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [19]:
# k=8 nearest neighbors
w_knn8 = KNN.from_dataframe(districts, k=8)
In [20]:
w_knn8.islands
Out[20]:
[]
In [21]:
capital = districts[districts['District'] == 'West']
display(capital)
State/Union District Wealth_Index_Above_2nd_Quintile Secondary_School_Attendance Prob_Survival_First5Yrs HDI geometry
126 NCT of Delhi West 0.969 0.886 0.963 0.939 MULTIPOLYGON (((3743007.249 4511991.338, 37428...
In [22]:
# amount of neighbors of that district
len(w_rook.neighbors[126]),len(w_queen.neighbors[126])
Out[22]:
(7, 7)
In [23]:
districts.iloc[w_rook.neighbors[126],]
Out[23]:
State/Union District Wealth_Index_Above_2nd_Quintile Secondary_School_Attendance Prob_Survival_First5Yrs HDI geometry
162 Haryana Jhajjar 0.954 0.971 0.991 0.972 MULTIPOLYGON (((3703135.5 4528351.619, 3703663...
164 Haryana Sonipat 0.940 0.937 0.965 0.947 MULTIPOLYGON (((3679488.454 4577392.127, 36807...
167 Haryana Gurgaon 0.940 0.911 0.966 0.939 MULTIPOLYGON (((3715858.418 4494004.054, 37166...
539 Uttar Pradesh Baghpat 0.824 0.814 0.951 0.861 MULTIPOLYGON (((3725134.787 4557017.481, 37244...
537 Uttar Pradesh Ghaziabad 0.968 0.844 0.970 0.926 MULTIPOLYGON (((3777001.971 4520467.486, 37758...
538 Uttar Pradesh Gautam Buddha Nagar 0.929 0.850 0.947 0.908 MULTIPOLYGON (((3747063.972 4512105.936, 37474...
171 Haryana Faridabad 0.949 0.873 0.967 0.929 MULTIPOLYGON (((3726361.466 4458097.746, 37271...
In [24]:
# see the neighbor
districts.iloc[w_rook.neighbors[126],].plot(facecolor="yellow")
Out[24]:
<Axes: >
No description has been provided for this image
In [25]:
# see whole area
base=districts.iloc[w_rook.neighbors[126] ,].plot(facecolor="yellow",edgecolor='k')
districts.loc[districts.District=='West'].plot(ax=base,facecolor="red")
Out[25]:
<Axes: >
No description has been provided for this image
In [26]:
# details
districts.iloc[w_queen.neighbors[126],]
Out[26]:
State/Union District Wealth_Index_Above_2nd_Quintile Secondary_School_Attendance Prob_Survival_First5Yrs HDI geometry
162 Haryana Jhajjar 0.954 0.971 0.991 0.972 MULTIPOLYGON (((3703135.5 4528351.619, 3703663...
164 Haryana Sonipat 0.940 0.937 0.965 0.947 MULTIPOLYGON (((3679488.454 4577392.127, 36807...
167 Haryana Gurgaon 0.940 0.911 0.966 0.939 MULTIPOLYGON (((3715858.418 4494004.054, 37166...
539 Uttar Pradesh Baghpat 0.824 0.814 0.951 0.861 MULTIPOLYGON (((3725134.787 4557017.481, 37244...
537 Uttar Pradesh Ghaziabad 0.968 0.844 0.970 0.926 MULTIPOLYGON (((3777001.971 4520467.486, 37758...
538 Uttar Pradesh Gautam Buddha Nagar 0.929 0.850 0.947 0.908 MULTIPOLYGON (((3747063.972 4512105.936, 37474...
171 Haryana Faridabad 0.949 0.873 0.967 0.929 MULTIPOLYGON (((3726361.466 4458097.746, 37271...
In [27]:
# whole area
# see whole area
base=districts.iloc[w_queen.neighbors[126] ,].plot(facecolor="yellow",edgecolor='k')
districts.loc[districts.District=='West'].plot(ax=base,facecolor="red")
Out[27]:
<Axes: >
No description has been provided for this image
In [28]:
w_knn8.neighbors[126]
Out[28]:
[np.int64(167),
 np.int64(537),
 np.int64(162),
 np.int64(171),
 np.int64(539),
 np.int64(538),
 np.int64(164),
 np.int64(166)]
In [29]:
base=districts.iloc[w_knn8.neighbors[126] ,].plot(facecolor="yellow",edgecolor='k')
districts.loc[districts.District=='West'].plot(ax=base,facecolor="red")
Out[29]:
<Axes: >
No description has been provided for this image

Exercise 7

In [31]:
!pip install esda
Collecting esda
  Downloading esda-2.7.0-py3-none-any.whl.metadata (2.0 kB)
Requirement already satisfied: geopandas>=0.12 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (1.0.1)
Requirement already satisfied: libpysal>=4.12 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (4.13.0)
Requirement already satisfied: numpy>=1.24 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (2.1.3)
Requirement already satisfied: pandas>1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (2.2.3)
Requirement already satisfied: scikit-learn>=1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (1.6.1)
Requirement already satisfied: scipy>=1.9 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (1.15.2)
Requirement already satisfied: shapely>=2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda) (2.0.6)
Requirement already satisfied: pyogrio>=0.7.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.12->esda) (0.10.0)
Requirement already satisfied: packaging in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.12->esda) (24.2)
Requirement already satisfied: pyproj>=3.3.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.12->esda) (3.6.1)
Requirement already satisfied: beautifulsoup4>=4.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal>=4.12->esda) (4.12.3)
Requirement already satisfied: platformdirs>=2.0.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal>=4.12->esda) (3.10.0)
Requirement already satisfied: requests>=2.27 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal>=4.12->esda) (2.32.3)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>1.5->esda) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>1.5->esda) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>1.5->esda) (2023.3)
Requirement already satisfied: joblib>=1.2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.2->esda) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.2->esda) (3.6.0)
Requirement already satisfied: soupsieve>1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from beautifulsoup4>=4.10->libpysal>=4.12->esda) (2.5)
Requirement already satisfied: certifi in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pyogrio>=0.7.2->geopandas>=0.12->esda) (2025.4.26)
Requirement already satisfied: six>=1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from python-dateutil>=2.8.2->pandas>1.5->esda) (1.16.0)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal>=4.12->esda) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal>=4.12->esda) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal>=4.12->esda) (2.3.0)
Downloading esda-2.7.0-py3-none-any.whl (142 kB)
Installing collected packages: esda
Successfully installed esda-2.7.0
In [32]:
pd.DataFrame(*w_knn8.full()) # 1 means both are neighbors
Out[32]:
0 1 2 3 4 5 6 7 8 9 ... 623 624 625 626 627 628 629 630 631 632
0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
628 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
629 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
630 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0
631 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
632 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0

633 rows × 633 columns

In [33]:
# needed for spatial correlation
w_knn8.transform = 'R'
In [34]:
# after transformation
pd.DataFrame(*w_knn8.full()).sum(axis=1)
Out[34]:
0      1.0
1      1.0
2      1.0
3      1.0
4      1.0
      ... 
628    1.0
629    1.0
630    1.0
631    1.0
632    1.0
Length: 633, dtype: float64
In [35]:
from esda.moran import Moran

moranHDI = Moran(districts['HDI'], w_knn8)
moranHDI.I,moranHDI.p_sim
Out[35]:
(np.float64(0.6434254521432141), np.float64(0.001))
In [36]:
!pip install splot
Collecting splot
  Downloading splot-1.1.7-py3-none-any.whl.metadata (8.9 kB)
Requirement already satisfied: esda in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (2.7.0)
Requirement already satisfied: geopandas>=0.9.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (1.0.1)
Collecting giddy (from splot)
  Downloading giddy-2.3.6-py3-none-any.whl.metadata (6.3 kB)
Requirement already satisfied: libpysal in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (4.13.0)
Requirement already satisfied: mapclassify in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (2.8.1)
Requirement already satisfied: matplotlib>=3.3.3 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (3.10.1)
Requirement already satisfied: numpy in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (2.1.3)
Requirement already satisfied: packaging in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (24.2)
Requirement already satisfied: seaborn>=0.11.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from splot) (0.13.2)
Collecting spreg (from splot)
  Downloading spreg-1.8.3-py3-none-any.whl.metadata (1.7 kB)
Requirement already satisfied: pyogrio>=0.7.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.9.0->splot) (0.10.0)
Requirement already satisfied: pandas>=1.4.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.9.0->splot) (2.2.3)
Requirement already satisfied: pyproj>=3.3.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.9.0->splot) (3.6.1)
Requirement already satisfied: shapely>=2.0.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.9.0->splot) (2.0.6)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (1.3.1)
Requirement already satisfied: cycler>=0.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (4.56.0)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (1.4.7)
Requirement already satisfied: pillow>=8 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (11.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (3.2.3)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.3.3->splot) (2.9.0.post0)
Requirement already satisfied: scikit-learn>=1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda->splot) (1.6.1)
Requirement already satisfied: scipy>=1.9 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from esda->splot) (1.15.2)
Requirement already satisfied: beautifulsoup4>=4.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal->splot) (4.12.3)
Requirement already satisfied: platformdirs>=2.0.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal->splot) (3.10.0)
Requirement already satisfied: requests>=2.27 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from libpysal->splot) (2.32.3)
Collecting quantecon>=0.7 (from giddy->splot)
  Downloading quantecon-0.8.1-py3-none-any.whl.metadata (5.2 kB)
Requirement already satisfied: networkx>=2.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify->splot) (3.4.2)
Requirement already satisfied: soupsieve>1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from beautifulsoup4>=4.10->libpysal->splot) (2.5)
Requirement already satisfied: pytz>=2020.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4.0->geopandas>=0.9.0->splot) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4.0->geopandas>=0.9.0->splot) (2023.3)
Requirement already satisfied: certifi in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pyogrio>=0.7.2->geopandas>=0.9.0->splot) (2025.4.26)
Requirement already satisfied: six>=1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from python-dateutil>=2.7->matplotlib>=3.3.3->splot) (1.16.0)
Requirement already satisfied: numba>=0.49.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from quantecon>=0.7->giddy->splot) (0.61.0)
Collecting sympy (from quantecon>=0.7->giddy->splot)
  Downloading sympy-1.14.0-py3-none-any.whl.metadata (12 kB)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal->splot) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal->splot) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->libpysal->splot) (2.3.0)
Requirement already satisfied: joblib>=1.2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.2->esda->splot) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.2->esda->splot) (3.6.0)
Requirement already satisfied: llvmlite<0.45,>=0.44.0dev0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from numba>=0.49.0->quantecon>=0.7->giddy->splot) (0.44.0)
Collecting mpmath<1.4,>=1.1.0 (from sympy->quantecon>=0.7->giddy->splot)
  Downloading mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)
Downloading splot-1.1.7-py3-none-any.whl (39 kB)
Downloading giddy-2.3.6-py3-none-any.whl (61 kB)
Downloading spreg-1.8.3-py3-none-any.whl (389 kB)
Downloading quantecon-0.8.1-py3-none-any.whl (322 kB)
Downloading sympy-1.14.0-py3-none-any.whl (6.3 MB)
   ---------------------------------------- 0.0/6.3 MB ? eta -:--:--
   - -------------------------------------- 0.3/6.3 MB ? eta -:--:--
   ---- ----------------------------------- 0.8/6.3 MB 2.6 MB/s eta 0:00:03
   -------- ------------------------------- 1.3/6.3 MB 2.6 MB/s eta 0:00:02
   ----------- ---------------------------- 1.8/6.3 MB 2.5 MB/s eta 0:00:02
   ------------- -------------------------- 2.1/6.3 MB 2.4 MB/s eta 0:00:02
   ---------------- ----------------------- 2.6/6.3 MB 2.3 MB/s eta 0:00:02
   ---------------- ----------------------- 2.6/6.3 MB 2.3 MB/s eta 0:00:02
   ---------------- ----------------------- 2.6/6.3 MB 2.3 MB/s eta 0:00:02
   ------------------- -------------------- 3.1/6.3 MB 1.8 MB/s eta 0:00:02
   ------------------------ --------------- 3.9/6.3 MB 1.9 MB/s eta 0:00:02
   ---------------------------- ----------- 4.5/6.3 MB 2.1 MB/s eta 0:00:01
   ------------------------------- -------- 5.0/6.3 MB 2.1 MB/s eta 0:00:01
   ---------------------------------- ----- 5.5/6.3 MB 2.1 MB/s eta 0:00:01
   -------------------------------------- - 6.0/6.3 MB 2.1 MB/s eta 0:00:01
   ---------------------------------------- 6.3/6.3 MB 2.1 MB/s eta 0:00:00
Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)
   ---------------------------------------- 0.0/536.2 kB ? eta -:--:--
   ---------------------------------------  524.3/536.2 kB 2.8 MB/s eta 0:00:01
   ---------------------------------------- 536.2/536.2 kB 2.0 MB/s eta 0:00:00
Installing collected packages: mpmath, sympy, quantecon, spreg, giddy, splot
Successfully installed giddy-2.3.6 mpmath-1.3.0 quantecon-0.8.1 splot-1.1.7 spreg-1.8.3 sympy-1.14.0
In [37]:
from splot.esda import moran_scatterplot

fig, ax = moran_scatterplot(moranHDI, aspect_equal=True)
ax.set_xlabel('HDI_std')
ax.set_ylabel('SpatialLag_HDI_std')
Out[37]:
Text(0, 0.5, 'SpatialLag_HDI_std')
No description has been provided for this image
In [38]:
from splot.esda import moran_scatterplot

fig, ax = moran_scatterplot(moranHDI, aspect_equal=True)
ax.set_xlabel('HDI_std')
ax.set_ylabel('SpatialLag_HDI_std')
Out[38]:
Text(0, 0.5, 'SpatialLag_HDI_std')
No description has been provided for this image
In [39]:
moranSSA = Moran(districts['Secondary_School_Attendance'], w_knn8)

fig, ax = moran_scatterplot(moranSSA, aspect_equal=True)
ax.set_xlabel('Secondary_School_Attendance_std')
ax.set_ylabel('SpatialLag_SSA_std')
Out[39]:
Text(0, 0.5, 'SpatialLag_SSA_std')
No description has been provided for this image
In [40]:
moranPSF = Moran(districts['Prob_Survival_First5Yrs'], w_knn8)

fig, ax = moran_scatterplot(moranPSF, aspect_equal=True)
ax.set_xlabel('Prob_Survival_First5Yrs_std')
ax.set_ylabel('SpatialLag_PSF_std')
Out[40]:
Text(0, 0.5, 'SpatialLag_PSF_std')
No description has been provided for this image

Exercise 8

In [42]:
# A LISA for each district using IDH2019
from esda.moran import Moran_Local
lisaHDI = Moran_Local(y=districts['HDI'], w=w_knn8,seed=2022)
In [43]:
fig, ax = moran_scatterplot(lisaHDI,p=0.05)
ax.set_xlabel('HDI_std')
ax.set_ylabel('SpatialLag_HDI_std');
No description has been provided for this image
In [44]:
# quadrant, # significance
lisaHDI.q, lisaHDI.p_sim
Out[44]:
(array([1, 1, 1, 1, 1, 4, 1, 1, 4, 4, 1, 1, 4, 1, 4, 4, 4, 4, 4, 4, 4, 4,
        3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3,
        3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 4, 1, 1, 1, 4, 3, 2, 2, 3, 2,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 4, 4, 1, 4, 1, 1, 1, 4, 1, 1, 1, 1, 4, 1, 4, 3, 2, 3, 2,
        2, 3, 2, 3, 3, 3, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 2, 4, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4, 4, 4, 4,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 3, 3,
        3, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4,
        4, 4, 4, 1, 4, 1, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 2, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3,
        3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 4, 1, 1, 1, 4, 1,
        1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 4, 3, 4, 4, 3, 3, 2,
        3, 3, 3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 1, 4, 1, 1, 1, 1, 4, 4, 4, 4,
        4, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 4, 4, 1, 4, 1, 2, 2,
        2, 2, 2, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4,
        1, 1, 2, 4, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 4, 1, 1, 4, 4, 1, 4, 4,
        1, 4, 4, 1, 4, 1, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]),
 array([0.207, 0.107, 0.432, 0.002, 0.002, 0.317, 0.005, 0.002, 0.022,
        0.064, 0.003, 0.056, 0.004, 0.182, 0.022, 0.062, 0.096, 0.119,
        0.116, 0.11 , 0.062, 0.121, 0.197, 0.144, 0.037, 0.005, 0.332,
        0.455, 0.214, 0.179, 0.213, 0.01 , 0.004, 0.046, 0.005, 0.009,
        0.056, 0.009, 0.01 , 0.021, 0.011, 0.039, 0.066, 0.185, 0.045,
        0.015, 0.354, 0.023, 0.008, 0.096, 0.015, 0.182, 0.083, 0.029,
        0.035, 0.017, 0.024, 0.163, 0.257, 0.024, 0.156, 0.005, 0.248,
        0.269, 0.001, 0.126, 0.033, 0.127, 0.099, 0.033, 0.174, 0.03 ,
        0.011, 0.024, 0.074, 0.004, 0.011, 0.274, 0.094, 0.005, 0.005,
        0.001, 0.006, 0.032, 0.003, 0.001, 0.005, 0.002, 0.005, 0.032,
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.331,
        0.491, 0.199, 0.373, 0.222, 0.223, 0.221, 0.158, 0.002, 0.34 ,
        0.092, 0.008, 0.182, 0.18 , 0.311, 0.182, 0.02 , 0.011, 0.003,
        0.001, 0.001, 0.011, 0.004, 0.001, 0.011, 0.005, 0.467, 0.054,
        0.001, 0.071, 0.366, 0.01 , 0.009, 0.115, 0.435, 0.131, 0.069,
        0.29 , 0.225, 0.475, 0.039, 0.053, 0.13 , 0.043, 0.311, 0.272,
        0.113, 0.066, 0.121, 0.246, 0.132, 0.229, 0.417, 0.406, 0.21 ,
        0.054, 0.49 , 0.032, 0.437, 0.027, 0.21 , 0.029, 0.011, 0.382,
        0.001, 0.001, 0.002, 0.003, 0.001, 0.002, 0.001, 0.001, 0.001,
        0.006, 0.002, 0.003, 0.002, 0.002, 0.002, 0.001, 0.001, 0.002,
        0.002, 0.031, 0.002, 0.072, 0.007, 0.005, 0.002, 0.007, 0.08 ,
        0.003, 0.016, 0.001, 0.002, 0.003, 0.017, 0.027, 0.099, 0.016,
        0.012, 0.025, 0.002, 0.025, 0.023, 0.007, 0.04 , 0.017, 0.006,
        0.025, 0.005, 0.011, 0.005, 0.103, 0.034, 0.017, 0.058, 0.012,
        0.001, 0.008, 0.005, 0.102, 0.016, 0.098, 0.036, 0.047, 0.001,
        0.013, 0.006, 0.036, 0.006, 0.001, 0.001, 0.001, 0.002, 0.001,
        0.001, 0.003, 0.001, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002,
        0.002, 0.002, 0.001, 0.002, 0.001, 0.002, 0.002, 0.001, 0.006,
        0.042, 0.006, 0.069, 0.01 , 0.003, 0.188, 0.122, 0.042, 0.055,
        0.457, 0.398, 0.001, 0.24 , 0.176, 0.292, 0.248, 0.001, 0.001,
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
        0.001, 0.001, 0.001, 0.001, 0.119, 0.255, 0.157, 0.298, 0.435,
        0.437, 0.446, 0.328, 0.347, 0.494, 0.073, 0.401, 0.387, 0.235,
        0.34 , 0.48 , 0.256, 0.067, 0.298, 0.036, 0.02 , 0.446, 0.051,
        0.205, 0.004, 0.122, 0.265, 0.114, 0.001, 0.361, 0.16 , 0.199,
        0.009, 0.159, 0.007, 0.004, 0.05 , 0.38 , 0.024, 0.004, 0.004,
        0.044, 0.462, 0.009, 0.007, 0.009, 0.094, 0.018, 0.009, 0.48 ,
        0.036, 0.058, 0.007, 0.334, 0.026, 0.103, 0.032, 0.182, 0.044,
        0.03 , 0.024, 0.242, 0.002, 0.06 , 0.23 , 0.063, 0.218, 0.393,
        0.174, 0.208, 0.342, 0.356, 0.159, 0.014, 0.066, 0.126, 0.126,
        0.129, 0.137, 0.104, 0.411, 0.195, 0.214, 0.137, 0.009, 0.006,
        0.09 , 0.112, 0.145, 0.148, 0.308, 0.159, 0.247, 0.196, 0.259,
        0.014, 0.009, 0.008, 0.006, 0.008, 0.019, 0.02 , 0.017, 0.276,
        0.207, 0.126, 0.087, 0.301, 0.277, 0.479, 0.046, 0.033, 0.007,
        0.13 , 0.445, 0.069, 0.059, 0.002, 0.016, 0.017, 0.019, 0.34 ,
        0.446, 0.178, 0.359, 0.374, 0.057, 0.317, 0.365, 0.033, 0.041,
        0.157, 0.218, 0.057, 0.194, 0.231, 0.001, 0.15 , 0.008, 0.01 ,
        0.031, 0.054, 0.049, 0.022, 0.001, 0.016, 0.015, 0.019, 0.008,
        0.001, 0.001, 0.01 , 0.493, 0.026, 0.002, 0.002, 0.002, 0.001,
        0.001, 0.001, 0.002, 0.001, 0.001, 0.002, 0.001, 0.001, 0.002,
        0.001, 0.002, 0.002, 0.001, 0.002, 0.002, 0.002, 0.002, 0.005,
        0.139, 0.198, 0.002, 0.313, 0.003, 0.097, 0.002, 0.011, 0.01 ,
        0.004, 0.011, 0.062, 0.293, 0.021, 0.481, 0.434, 0.089, 0.351,
        0.168, 0.463, 0.207, 0.101, 0.137, 0.394, 0.459, 0.452, 0.391,
        0.341, 0.41 , 0.444, 0.025, 0.296, 0.059, 0.041, 0.003, 0.001,
        0.002, 0.003, 0.001, 0.002, 0.001, 0.005, 0.001, 0.001, 0.008,
        0.001, 0.002, 0.006, 0.002, 0.002, 0.007, 0.002, 0.021, 0.024,
        0.014, 0.01 , 0.008, 0.003, 0.014, 0.021, 0.002, 0.006, 0.021,
        0.014, 0.075, 0.008, 0.064, 0.277, 0.039, 0.053, 0.021, 0.053,
        0.016, 0.215, 0.315, 0.129, 0.133, 0.338, 0.004, 0.012, 0.008,
        0.032, 0.014, 0.002, 0.027, 0.054, 0.148, 0.388, 0.142, 0.162,
        0.156, 0.008, 0.073, 0.39 , 0.105, 0.487, 0.17 , 0.139, 0.491,
        0.006, 0.126, 0.234, 0.189, 0.241, 0.042, 0.034, 0.212, 0.215,
        0.308, 0.357, 0.459, 0.038, 0.417, 0.231, 0.042, 0.045, 0.019,
        0.058, 0.002, 0.193, 0.111, 0.275, 0.278, 0.423, 0.095, 0.191,
        0.013, 0.313, 0.219, 0.003, 0.058, 0.1  , 0.412, 0.105, 0.027,
        0.049, 0.052, 0.01 , 0.19 , 0.016, 0.008, 0.001, 0.015, 0.043,
        0.041, 0.015, 0.005, 0.011, 0.021, 0.265, 0.026, 0.026, 0.041,
        0.438, 0.049, 0.006, 0.047, 0.027, 0.352, 0.076, 0.07 , 0.192,
        0.074, 0.049, 0.001, 0.001, 0.001, 0.072, 0.001, 0.001, 0.169,
        0.226, 0.001, 0.027]))
In [45]:
# quadrant: 1 HH,  2 LH,  3 LL,  4 HL
pd.Series(lisaHDI.q).value_counts()
Out[45]:
1    263
3    263
4     76
2     31
Name: count, dtype: int64
In [46]:
districts['HDI_quadrant']=[l if p <0.05 else 0 for l,p in zip(lisaHDI.q,lisaHDI.p_sim)  ]
districts['HDI_quadrant'].value_counts()
Out[46]:
HDI_quadrant
0    279
1    180
3    151
4     18
2      5
Name: count, dtype: int64
In [47]:
labels = [ '0 no_sig', '1 hotSpot', '2 coldOutlier', '3 coldSpot', '4 hotOutlier']

districts['HDI_quadrant_names']=[labels[i] for i in districts['HDI_quadrant']]

districts['HDI_quadrant_names'].value_counts()
Out[47]:
HDI_quadrant_names
0 no_sig         279
1 hotSpot        180
3 coldSpot       151
4 hotOutlier      18
2 coldOutlier      5
Name: count, dtype: int64
In [48]:
# custom colors
import matplotlib.pyplot as plt
from matplotlib import colors
myColMap = colors.ListedColormap([ 'white', 'pink', 'cyan', 'azure','red'])

# Set up figure and ax
f, ax = plt.subplots(1, figsize=(12,12))
# Plot unique values choropleth including
# a legend and with no boundary lines

plt.title('Spots and Outliers')

districts.plot(column='HDI_quadrant_names',
                categorical=True,
                cmap=myColMap,
                linewidth=0.1,
                edgecolor='k',
                legend=True,
                legend_kwds={'loc': 'center left',
                             'bbox_to_anchor': (0.7, 0.6)},
                ax=ax)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
No description has been provided for this image
In [49]:
from esda.moran import Moran_Local
lisaSSA = Moran_Local(y=districts['Secondary_School_Attendance'], w=w_knn8,seed=2022)
In [50]:
fig, ax = moran_scatterplot(lisaHDI,p=0.05)
ax.set_xlabel('Secondary_School_Attendance_std')
ax.set_ylabel('SpatialLag_SSA_std');
No description has been provided for this image
In [51]:
# quadrant, # significance
lisaSSA.q, lisaSSA.p_sim
Out[51]:
(array([1, 1, 4, 1, 1, 4, 1, 2, 4, 4, 1, 2, 4, 2, 4, 1, 1, 1, 1, 1, 1, 2,
        1, 1, 4, 3, 1, 1, 1, 2, 2, 3, 4, 1, 3, 4, 2, 1, 3, 1, 1, 3, 4, 1,
        3, 1, 2, 2, 1, 2, 3, 4, 3, 2, 1, 3, 3, 2, 2, 3, 1, 3, 1, 2, 3, 4,
        3, 1, 1, 3, 4, 4, 3, 3, 3, 4, 3, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 2, 4, 1,
        4, 2, 2, 4, 4, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 1, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 4, 2, 4, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 1,
        1, 1, 4, 3, 4, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 3, 3,
        3, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 3,
        3, 2, 4, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, 3, 3, 3, 2, 3,
        3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1,
        1, 3, 1, 1, 1, 1, 3, 3, 1, 1, 1, 3, 1, 2, 1, 3, 3, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 3, 1, 4, 1, 2, 2, 2, 1, 4, 1, 1, 1, 1, 1, 1, 1, 4,
        1, 2, 2, 1, 2, 2, 3, 2, 4, 1, 3, 2, 1, 4, 4, 1, 3, 1, 4, 2, 4, 3,
        2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 1, 1, 3, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 1,
        4, 1, 1, 1, 4, 1, 1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 2, 3, 4, 1, 3, 4,
        3, 2, 4, 4, 3, 4, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4,
        2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
        3, 4, 4, 3, 3, 3, 4, 3, 3, 3, 2, 4, 4, 4, 3, 4, 3, 4, 3, 2, 3, 1,
        4, 4, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 4, 1, 1, 1, 3, 1, 1, 1,
        3, 4, 1, 1, 2, 1, 4, 4, 3, 3, 1, 4, 3, 1, 1, 3, 1]),
 array([0.006, 0.015, 0.155, 0.02 , 0.005, 0.088, 0.139, 0.078, 0.013,
        0.046, 0.019, 0.435, 0.001, 0.471, 0.429, 0.409, 0.471, 0.034,
        0.23 , 0.095, 0.21 , 0.034, 0.053, 0.116, 0.5  , 0.151, 0.264,
        0.119, 0.145, 0.161, 0.054, 0.168, 0.176, 0.323, 0.415, 0.368,
        0.267, 0.303, 0.301, 0.408, 0.183, 0.401, 0.397, 0.346, 0.437,
        0.263, 0.029, 0.131, 0.31 , 0.257, 0.297, 0.419, 0.434, 0.126,
        0.404, 0.314, 0.341, 0.322, 0.344, 0.383, 0.304, 0.073, 0.355,
        0.191, 0.002, 0.275, 0.309, 0.326, 0.218, 0.269, 0.37 , 0.169,
        0.111, 0.097, 0.268, 0.035, 0.13 , 0.413, 0.375, 0.04 , 0.055,
        0.028, 0.062, 0.299, 0.046, 0.003, 0.05 , 0.062, 0.079, 0.093,
        0.002, 0.001, 0.008, 0.003, 0.001, 0.001, 0.001, 0.016, 0.035,
        0.17 , 0.489, 0.142, 0.017, 0.226, 0.371, 0.495, 0.021, 0.449,
        0.409, 0.097, 0.152, 0.44 , 0.467, 0.483, 0.186, 0.122, 0.024,
        0.001, 0.001, 0.033, 0.076, 0.041, 0.001, 0.009, 0.38 , 0.018,
        0.038, 0.001, 0.025, 0.01 , 0.007, 0.001, 0.001, 0.001, 0.001,
        0.001, 0.001, 0.013, 0.001, 0.001, 0.001, 0.001, 0.003, 0.001,
        0.001, 0.002, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.007,
        0.001, 0.001, 0.001, 0.031, 0.001, 0.002, 0.001, 0.001, 0.026,
        0.007, 0.023, 0.173, 0.211, 0.007, 0.376, 0.018, 0.01 , 0.007,
        0.436, 0.157, 0.173, 0.029, 0.327, 0.121, 0.029, 0.004, 0.09 ,
        0.153, 0.135, 0.29 , 0.41 , 0.037, 0.032, 0.024, 0.039, 0.117,
        0.009, 0.051, 0.018, 0.007, 0.003, 0.023, 0.01 , 0.01 , 0.017,
        0.004, 0.018, 0.004, 0.013, 0.009, 0.014, 0.004, 0.004, 0.007,
        0.007, 0.009, 0.007, 0.007, 0.004, 0.007, 0.007, 0.016, 0.004,
        0.418, 0.475, 0.404, 0.225, 0.444, 0.422, 0.212, 0.462, 0.315,
        0.5  , 0.326, 0.41 , 0.081, 0.006, 0.051, 0.014, 0.396, 0.004,
        0.227, 0.472, 0.122, 0.185, 0.035, 0.004, 0.001, 0.004, 0.004,
        0.004, 0.009, 0.001, 0.004, 0.002, 0.007, 0.005, 0.001, 0.014,
        0.059, 0.014, 0.074, 0.018, 0.004, 0.277, 0.153, 0.153, 0.08 ,
        0.454, 0.227, 0.001, 0.132, 0.078, 0.412, 0.147, 0.001, 0.001,
        0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,
        0.001, 0.001, 0.001, 0.001, 0.008, 0.105, 0.078, 0.066, 0.419,
        0.304, 0.233, 0.227, 0.303, 0.248, 0.105, 0.28 , 0.338, 0.449,
        0.108, 0.347, 0.086, 0.139, 0.08 , 0.026, 0.001, 0.479, 0.114,
        0.41 , 0.024, 0.138, 0.126, 0.338, 0.002, 0.104, 0.102, 0.188,
        0.118, 0.061, 0.103, 0.032, 0.441, 0.358, 0.036, 0.047, 0.054,
        0.002, 0.438, 0.089, 0.004, 0.02 , 0.382, 0.001, 0.001, 0.182,
        0.463, 0.399, 0.006, 0.306, 0.045, 0.068, 0.062, 0.374, 0.419,
        0.098, 0.013, 0.276, 0.004, 0.419, 0.293, 0.173, 0.376, 0.353,
        0.014, 0.287, 0.118, 0.484, 0.021, 0.014, 0.207, 0.225, 0.225,
        0.122, 0.112, 0.103, 0.033, 0.113, 0.402, 0.093, 0.001, 0.001,
        0.029, 0.022, 0.025, 0.02 , 0.019, 0.014, 0.034, 0.138, 0.014,
        0.191, 0.251, 0.354, 0.48 , 0.424, 0.098, 0.394, 0.319, 0.279,
        0.477, 0.177, 0.041, 0.116, 0.037, 0.172, 0.152, 0.199, 0.395,
        0.294, 0.015, 0.179, 0.087, 0.489, 0.347, 0.191, 0.463, 0.451,
        0.345, 0.263, 0.317, 0.171, 0.438, 0.394, 0.202, 0.217, 0.429,
        0.392, 0.302, 0.359, 0.393, 0.481, 0.459, 0.438, 0.023, 0.117,
        0.234, 0.062, 0.089, 0.006, 0.001, 0.007, 0.004, 0.481, 0.002,
        0.001, 0.001, 0.003, 0.34 , 0.003, 0.02 , 0.002, 0.035, 0.024,
        0.014, 0.027, 0.027, 0.006, 0.027, 0.221, 0.018, 0.015, 0.049,
        0.018, 0.3  , 0.049, 0.086, 0.42 , 0.421, 0.115, 0.42 , 0.489,
        0.165, 0.445, 0.014, 0.325, 0.022, 0.166, 0.137, 0.484, 0.131,
        0.072, 0.198, 0.482, 0.362, 0.364, 0.04 , 0.372, 0.201, 0.033,
        0.202, 0.33 , 0.142, 0.07 , 0.095, 0.073, 0.418, 0.028, 0.324,
        0.151, 0.163, 0.294, 0.009, 0.427, 0.145, 0.047, 0.004, 0.001,
        0.005, 0.005, 0.001, 0.003, 0.001, 0.005, 0.001, 0.001, 0.004,
        0.001, 0.002, 0.005, 0.006, 0.001, 0.011, 0.001, 0.004, 0.005,
        0.002, 0.005, 0.006, 0.005, 0.004, 0.005, 0.004, 0.002, 0.004,
        0.001, 0.199, 0.122, 0.179, 0.466, 0.011, 0.245, 0.14 , 0.103,
        0.422, 0.28 , 0.156, 0.279, 0.228, 0.182, 0.457, 0.275, 0.235,
        0.059, 0.23 , 0.001, 0.13 , 0.011, 0.067, 0.157, 0.008, 0.486,
        0.133, 0.199, 0.099, 0.433, 0.052, 0.001, 0.076, 0.432, 0.147,
        0.39 , 0.211, 0.451, 0.483, 0.481, 0.078, 0.085, 0.391, 0.001,
        0.087, 0.001, 0.471, 0.004, 0.395, 0.383, 0.127, 0.102, 0.017,
        0.09 , 0.023, 0.163, 0.149, 0.006, 0.199, 0.001, 0.361, 0.327,
        0.001, 0.205, 0.299, 0.001, 0.03 , 0.035, 0.002, 0.071, 0.003,
        0.086, 0.03 , 0.014, 0.001, 0.088, 0.009, 0.157, 0.006, 0.051,
        0.003, 0.001, 0.001, 0.165, 0.09 , 0.364, 0.067, 0.001, 0.001,
        0.101, 0.003, 0.001, 0.002, 0.31 , 0.152, 0.071, 0.213, 0.116,
        0.295, 0.339, 0.032, 0.006, 0.041, 0.497, 0.001, 0.138, 0.243,
        0.25 , 0.002, 0.308]))
In [52]:
# quadrant: 1 HH,  2 LH,  3 LL,  4 HL
pd.Series(lisaSSA.q).value_counts()
Out[52]:
1    273
3    231
4     73
2     56
Name: count, dtype: int64
In [53]:
districts['SSA_quadrant']=[l if p <0.05 else 0 for l,p in zip(lisaSSA.q,lisaSSA.p_sim)  ]
districts['SSA_quadrant'].value_counts()
Out[53]:
SSA_quadrant
0    355
1    153
3    109
4     11
2      5
Name: count, dtype: int64
In [54]:
labels = [ '0 no_sig', '1 hotSpot', '2 coldOutlier', '3 coldSpot', '4 hotOutlier']

districts['SSA_quadrant_names']=[labels[i] for i in districts['SSA_quadrant']]

districts['SSA_quadrant_names'].value_counts()
Out[54]:
SSA_quadrant_names
0 no_sig         355
1 hotSpot        153
3 coldSpot       109
4 hotOutlier      11
2 coldOutlier      5
Name: count, dtype: int64
In [55]:
# custom colors
import matplotlib.pyplot as plt
from matplotlib import colors
myColMap = colors.ListedColormap([ 'white', 'pink', 'cyan', 'azure','red'])

# Set up figure and ax
f, ax = plt.subplots(1, figsize=(12,12))
# Plot unique values choropleth including
# a legend and with no boundary lines

plt.title('Spots and Outliers')

districts.plot(column='SSA_quadrant_names',
                categorical=True,
                cmap=myColMap,
                linewidth=0.1,
                edgecolor='k',
                legend=True,
                legend_kwds={'loc': 'center left',
                             'bbox_to_anchor': (0.7, 0.6)},
                ax=ax)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
No description has been provided for this image

Exercise 9

In [57]:
selected_variables = ['Wealth_Index_Above_2nd_Quintile',
                     'Secondary_School_Attendance',
                     'Prob_Survival_First5Yrs']
In [58]:
# see distribution
sea.boxplot(districts[selected_variables])
Out[58]:
<Axes: >
No description has been provided for this image
In [59]:
districts[selected_variables].corr()
Out[59]:
Wealth_Index_Above_2nd_Quintile Secondary_School_Attendance Prob_Survival_First5Yrs
Wealth_Index_Above_2nd_Quintile 1.000000 0.485996 0.396964
Secondary_School_Attendance 0.485996 1.000000 0.454648
Prob_Survival_First5Yrs 0.396964 0.454648 1.000000
In [60]:
sea.pairplot(
    districts[selected_variables], kind="reg", diag_kind="kde"
)
Out[60]:
<seaborn.axisgrid.PairGrid at 0x25891ba8790>
No description has been provided for this image
In [61]:
from sklearn.preprocessing import StandardScaler


scaler = StandardScaler()
normalized_data = scaler.fit_transform(districts[selected_variables])
sea.displot(pd.melt(pd.DataFrame(normalized_data,columns=selected_variables)),
            x="value", hue="variable",kind="kde",
            log_scale=(False,False))
Out[61]:
<seaborn.axisgrid.FacetGrid at 0x25892ee6e50>
No description has been provided for this image
In [62]:
# new names
selected_variables_new_std=[s+'_std' for s in selected_variables]

# add colunms
districts[selected_variables_new_std]=normalized_data
In [63]:
# as a result:
selected_variables_new_std = ['Wealth_Index_Above_2nd_Quintile_std',
                     'Secondary_School_Attendance_std',
                     'Prob_Survival_First5Yrs_std']
sea.pairplot(
    districts[selected_variables_new_std], kind="reg", diag_kind="kde"
)
Out[63]:
<seaborn.axisgrid.PairGrid at 0x2588f415dd0>
No description has been provided for this image
In [64]:
from scipy.cluster import hierarchy as hc

Z = hc.linkage(districts[selected_variables_new_std], 'ward')
# calculate full dendrogram
plt.figure(figsize=(25, 10))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('cases')
plt.ylabel('distance')
hc.dendrogram(
    Z,
    leaf_rotation=90.,  # rotates the x axis labels
    leaf_font_size=1,  # font size for the x axis labels
)
plt.show()
No description has been provided for this image
In [65]:
from sklearn.cluster import AgglomerativeClustering as agnes

import numpy as np
np.random.seed(12345)# Set seed for reproducibility

# Initialize the algorithm, requesting 6 clusters
model = agnes(linkage="ward", n_clusters=6).fit(districts[selected_variables_new_std])

# Assign labels to main data table
districts["hc_ag6"] = model.labels_
In [66]:
# see distribution of districts
districts["hc_ag6"].value_counts()
Out[66]:
hc_ag6
0    255
2    129
3    111
4     57
1     56
5     25
Name: count, dtype: int64
In [67]:
districts.groupby("hc_ag6")[selected_variables_new_std].mean()
Out[67]:
Wealth_Index_Above_2nd_Quintile_std Secondary_School_Attendance_std Prob_Survival_First5Yrs_std
hc_ag6
0 0.810192 0.760727 0.678130
1 0.992289 -0.642456 -0.326209
2 -0.657353 0.066438 0.320661
3 -0.875546 -0.376034 -0.707919
4 -1.161041 -1.928298 -0.968072
5 -0.560150 -0.597021 -2.490463
In [68]:
# Set up figure and ax
f, ax = plt.subplots(1, figsize=(9, 9))
# Plot unique values choropleth including
# a legend and with no boundary lines
districts.plot(
    column="hc_ag6", categorical=True, legend=True, linewidth=0, ax=ax
)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
No description has been provided for this image
In [69]:
# modify previous funtion call to specify cluster model with spatial constraint

model_queen = agnes(linkage="ward",
                    n_clusters=6,
                    connectivity=w_queen.sparse).fit(districts[selected_variables_new_std])
# Fit algorithm to the data
districts["hc_ag6_wQueen"] = model_queen.labels_
C:\Users\mendo\anaconda3\envs\dataespacial__31111\Lib\site-packages\sklearn\cluster\_agglomerative.py:325: UserWarning: the number of connected components of the connectivity matrix is 5 > 1. Completing it to avoid stopping the tree early.
  connectivity, n_connected_components = _fix_connectivity(
In [70]:
# Set up figure and ax
f, ax = plt.subplots(1, figsize=(9, 9))
# Plot unique values choropleth including a legend and with no boundary lines
districts.plot(
    column="hc_ag6_wQueen",
    categorical=True,
    legend=True,
    linewidth=0,
    ax=ax,
)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
No description has been provided for this image
In [71]:
model_wknn8 = agnes(linkage="ward",
                    n_clusters=6,
                    connectivity=w_knn8.sparse).fit(districts[selected_variables_new_std])
districts["hc_ag6_wknn8"] = model_wknn8.labels_
In [72]:
# Set up figure and ax
f, ax = plt.subplots(1, figsize=(10, 12))
# Plot unique values choropleth including a legend and with no boundary lines
districts.plot(
    column="hc_ag6_wknn8",
    categorical=True,
    legend=True,
    linewidth=0,
    ax=ax,
)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
No description has been provided for this image
In [73]:
from esda import shape as shapestats
results={}
for cluster_type in ("hc_ag6_wknn8", "hc_ag6"):
    # compute the region polygons using a dissolve
    regions = districts[[cluster_type, "geometry"]].to_crs(7755).dissolve(by=cluster_type)
    # compute the actual isoperimetric quotient for these regions
    ipqs = shapestats.isoperimetric_quotient(regions)
    # cast to a dataframe
    result = {cluster_type:ipqs}
    results.update(result)
# stack the series together along columns
pd.DataFrame(results)
Out[73]:
hc_ag6_wknn8 hc_ag6
0 0.006588 0.005475
1 0.015873 0.008415
2 0.028184 0.003860
3 0.011220 0.004861
4 0.010901 0.009011
5 0.044095 0.013651
In [74]:
from esda import shape as shapestats
results={}
for cluster_type in ("hc_ag6_wknn8", "hc_ag6"):
    # compute the region polygons using a dissolve
    regions = districts[[cluster_type, "geometry"]].to_crs(7755).dissolve(by=cluster_type)
    # compute the actual convex hull quotient for these regions
    chullr = shapestats.convex_hull_ratio(regions)
    # cast to a dataframe
    result = {cluster_type:chullr}
    results.update(result)
# stack the series together along columns
pd.DataFrame(results)
Out[74]:
hc_ag6_wknn8 hc_ag6
0 0.207758 0.202565
1 0.423602 0.157215
2 0.586189 0.147140
3 0.427325 0.172362
4 0.360999 0.115156
5 0.628876 0.056561
In [75]:
from sklearn import metrics

fit_scores = []
for cluster_type in ("hc_ag6_wknn8", "hc_ag6"):
    # compute the CH score
    ch_score = metrics.calinski_harabasz_score(
        # using scaled variables
        districts[selected_variables_new_std],
        # using these labels
        districts[cluster_type],
    )
    sil_score = metrics.silhouette_score(
        # using scaled variables
        districts[selected_variables_new_std],
        # using these labels
        districts[cluster_type],
    )
    # and append the cluster type with the CH score
    fit_scores.append((cluster_type, ch_score,sil_score))


# re-arrange the scores into a dataframe for display
pd.DataFrame(
    fit_scores, columns=["cluster type", "CH score", "SIL score"]
).set_index("cluster type")
Out[75]:
CH score SIL score
cluster type
hc_ag6_wknn8 126.443557 0.042646
hc_ag6 245.201020 0.249418

Exercise 10

In [87]:
!pip install pysal
Collecting pysal
  Using cached pysal-25.1-py3-none-any.whl.metadata (15 kB)
Requirement already satisfied: beautifulsoup4>=4.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (4.12.3)
Requirement already satisfied: geopandas>=0.10.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (1.0.1)
Requirement already satisfied: numpy>=1.22 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.1.3)
Requirement already satisfied: packaging>=22 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (24.2)
Requirement already satisfied: pandas>=1.4 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.2.3)
Requirement already satisfied: platformdirs>=2.0.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (3.10.0)
Requirement already satisfied: requests>=2.27 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.32.3)
Requirement already satisfied: scipy>=1.8 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (1.15.2)
Requirement already satisfied: shapely>=2.0.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.0.6)
Requirement already satisfied: scikit-learn>=1.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (1.6.1)
Requirement already satisfied: libpysal>=4.12.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (4.13.0)
Collecting access>=1.1.9 (from pysal)
  Using cached access-1.1.9-py3-none-any.whl.metadata (2.4 kB)
Requirement already satisfied: esda>=2.6.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.7.0)
Requirement already satisfied: giddy>=2.3.6 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.3.6)
Collecting inequality>=1.1.1 (from pysal)
  Using cached inequality-1.1.1-py3-none-any.whl.metadata (3.9 kB)
Collecting pointpats>=2.5.1 (from pysal)
  Using cached pointpats-2.5.1-py3-none-any.whl.metadata (4.7 kB)
Collecting segregation>=2.5.1 (from pysal)
  Using cached segregation-2.5.2-py3-none-any.whl.metadata (2.2 kB)
Collecting spaghetti>=1.7.6 (from pysal)
  Using cached spaghetti-1.7.6-py3-none-any.whl.metadata (12 kB)
Collecting mgwr>=2.2.1 (from pysal)
  Using cached mgwr-2.2.1-py3-none-any.whl.metadata (1.5 kB)
Collecting momepy>=0.9.1 (from pysal)
  Using cached momepy-0.10.0-py3-none-any.whl.metadata (1.5 kB)
Collecting spglm>=1.1.0 (from pysal)
  Using cached spglm-1.1.0-py3-none-any.whl.metadata (3.9 kB)
Collecting spint>=1.0.7 (from pysal)
  Using cached spint-1.0.7.tar.gz (28 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Requirement already satisfied: spreg>=1.8.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (1.8.3)
Collecting tobler>=0.12.1 (from pysal)
  Using cached tobler-0.12.1-py3-none-any.whl.metadata (1.9 kB)
Requirement already satisfied: mapclassify>=2.8.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (2.8.1)
Requirement already satisfied: splot>=1.1.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pysal) (1.1.7)
Collecting spopt>=0.6.1 (from pysal)
  Using cached spopt-0.6.1-py3-none-any.whl.metadata (10 kB)
Requirement already satisfied: soupsieve>1.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from beautifulsoup4>=4.10->pysal) (2.5)
Requirement already satisfied: pyogrio>=0.7.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.10.0->pysal) (0.10.0)
Requirement already satisfied: pyproj>=3.3.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from geopandas>=0.10.0->pysal) (3.6.1)
Requirement already satisfied: quantecon>=0.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from giddy>=2.3.6->pysal) (0.8.1)
Requirement already satisfied: matplotlib>=3.6 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from inequality>=1.1.1->pysal) (3.10.1)
Requirement already satisfied: networkx>=2.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from mapclassify>=2.8.1->pysal) (3.4.2)
Collecting tqdm>=4.65 (from momepy>=0.9.1->pysal)
  Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->pysal) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->pysal) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from pandas>=1.4->pysal) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->pysal) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->pysal) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->pysal) (2.3.0)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from requests>=2.27->pysal) (2025.4.26)
Requirement already satisfied: joblib>=1.2.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.1->pysal) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from scikit-learn>=1.1->pysal) (3.6.0)
Collecting deprecation (from segregation>=2.5.1->pysal)
  Using cached deprecation-2.1.0-py2.py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: seaborn in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from segregation>=2.5.1->pysal) (0.13.2)
Requirement already satisfied: numba in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from segregation>=2.5.1->pysal) (0.61.0)
Collecting rtree>=1.0 (from spaghetti>=1.7.6->pysal)
  Using cached rtree-1.4.0-py3-none-win_amd64.whl.metadata (2.1 kB)
Collecting pulp>=2.7 (from spopt>=0.6.1->pysal)
  Using cached pulp-3.2.1-py3-none-any.whl.metadata (6.9 kB)
Collecting rasterio (from tobler>=0.12.1->pysal)
  Using cached rasterio-1.4.3-cp311-cp311-win_amd64.whl.metadata (9.4 kB)
Collecting statsmodels (from tobler>=0.12.1->pysal)
  Using cached statsmodels-0.14.4-cp311-cp311-win_amd64.whl.metadata (9.5 kB)
Collecting rasterstats (from tobler>=0.12.1->pysal)
  Using cached rasterstats-0.20.0-py3-none-any.whl.metadata (4.2 kB)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (1.3.1)
Requirement already satisfied: cycler>=0.10 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (4.56.0)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (1.4.7)
Requirement already satisfied: pillow>=8 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (11.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from matplotlib>=3.6->inequality>=1.1.1->pysal) (3.2.3)
Requirement already satisfied: six>=1.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.4->pysal) (1.16.0)
Requirement already satisfied: sympy in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from quantecon>=0.7->giddy>=2.3.6->pysal) (1.14.0)
Requirement already satisfied: llvmlite<0.45,>=0.44.0dev0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from numba->segregation>=2.5.1->pysal) (0.44.0)
Requirement already satisfied: colorama in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from tqdm>=4.65->momepy>=0.9.1->pysal) (0.4.6)
Collecting affine (from rasterio->tobler>=0.12.1->pysal)
  Using cached affine-2.4.0-py3-none-any.whl.metadata (4.0 kB)
Requirement already satisfied: attrs in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from rasterio->tobler>=0.12.1->pysal) (24.3.0)
Requirement already satisfied: click>=4.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from rasterio->tobler>=0.12.1->pysal) (8.2.1)
Requirement already satisfied: cligj>=0.5 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from rasterio->tobler>=0.12.1->pysal) (0.7.2)
Requirement already satisfied: click-plugins in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from rasterio->tobler>=0.12.1->pysal) (1.1.1)
Requirement already satisfied: fiona in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from rasterstats->tobler>=0.12.1->pysal) (1.10.1)
Collecting simplejson (from rasterstats->tobler>=0.12.1->pysal)
  Using cached simplejson-3.20.1-cp311-cp311-win_amd64.whl.metadata (3.4 kB)
Collecting patsy>=0.5.6 (from statsmodels->tobler>=0.12.1->pysal)
  Using cached patsy-1.0.1-py2.py3-none-any.whl.metadata (3.3 kB)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\users\mendo\anaconda3\envs\dataespacial__31111\lib\site-packages (from sympy->quantecon>=0.7->giddy>=2.3.6->pysal) (1.3.0)
Using cached pysal-25.1-py3-none-any.whl (17 kB)
Using cached access-1.1.9-py3-none-any.whl (21 kB)
Using cached inequality-1.1.1-py3-none-any.whl (29 kB)
Using cached mgwr-2.2.1-py3-none-any.whl (47 kB)
Using cached momepy-0.10.0-py3-none-any.whl (1.7 MB)
Using cached pointpats-2.5.1-py3-none-any.whl (59 kB)
Using cached segregation-2.5.2-py3-none-any.whl (141 kB)
Using cached spaghetti-1.7.6-py3-none-any.whl (53 kB)
Using cached spglm-1.1.0-py3-none-any.whl (41 kB)
Using cached spopt-0.6.1-py3-none-any.whl (243 kB)
Using cached tobler-0.12.1-py3-none-any.whl (28 kB)
Downloading pulp-3.2.1-py3-none-any.whl (16.4 MB)
   ---------------------------------------- 0.0/16.4 MB ? eta -:--:--
    --------------------------------------- 0.3/16.4 MB ? eta -:--:--
   - -------------------------------------- 0.5/16.4 MB 1.9 MB/s eta 0:00:09
   - -------------------------------------- 0.8/16.4 MB 1.8 MB/s eta 0:00:09
   --- ------------------------------------ 1.3/16.4 MB 1.8 MB/s eta 0:00:09
   --- ------------------------------------ 1.6/16.4 MB 1.7 MB/s eta 0:00:09
   ---- ----------------------------------- 1.8/16.4 MB 1.5 MB/s eta 0:00:10
   ----- ---------------------------------- 2.4/16.4 MB 1.7 MB/s eta 0:00:09
   ------ --------------------------------- 2.6/16.4 MB 1.7 MB/s eta 0:00:09
   ------- -------------------------------- 3.1/16.4 MB 1.7 MB/s eta 0:00:08
   ------- -------------------------------- 3.1/16.4 MB 1.7 MB/s eta 0:00:08
   ------- -------------------------------- 3.1/16.4 MB 1.7 MB/s eta 0:00:08
   ------- -------------------------------- 3.1/16.4 MB 1.7 MB/s eta 0:00:08
   -------- ------------------------------- 3.4/16.4 MB 1.3 MB/s eta 0:00:10
   -------- ------------------------------- 3.4/16.4 MB 1.3 MB/s eta 0:00:10
   -------- ------------------------------- 3.7/16.4 MB 1.2 MB/s eta 0:00:11
   --------- ------------------------------ 3.9/16.4 MB 1.2 MB/s eta 0:00:11
   ---------- ----------------------------- 4.5/16.4 MB 1.3 MB/s eta 0:00:10
   ----------- ---------------------------- 4.7/16.4 MB 1.3 MB/s eta 0:00:09
   ------------ --------------------------- 5.2/16.4 MB 1.3 MB/s eta 0:00:09
   ------------- -------------------------- 5.5/16.4 MB 1.4 MB/s eta 0:00:09
   -------------- ------------------------- 5.8/16.4 MB 1.4 MB/s eta 0:00:08
   -------------- ------------------------- 6.0/16.4 MB 1.4 MB/s eta 0:00:08
   -------------- ------------------------- 6.0/16.4 MB 1.4 MB/s eta 0:00:08
   -------------- ------------------------- 6.0/16.4 MB 1.4 MB/s eta 0:00:08
   ---------------- ----------------------- 6.6/16.4 MB 1.3 MB/s eta 0:00:08
   ---------------- ----------------------- 6.8/16.4 MB 1.3 MB/s eta 0:00:08
   ------------------ --------------------- 7.6/16.4 MB 1.4 MB/s eta 0:00:07
   ------------------- -------------------- 7.9/16.4 MB 1.4 MB/s eta 0:00:07
   ------------------- -------------------- 7.9/16.4 MB 1.4 MB/s eta 0:00:07
   -------------------- ------------------- 8.4/16.4 MB 1.3 MB/s eta 0:00:06
   -------------------- ------------------- 8.4/16.4 MB 1.3 MB/s eta 0:00:06
   -------------------- ------------------- 8.4/16.4 MB 1.3 MB/s eta 0:00:06
   --------------------- ------------------ 8.7/16.4 MB 1.3 MB/s eta 0:00:06
   --------------------- ------------------ 8.9/16.4 MB 1.3 MB/s eta 0:00:06
   ----------------------- ---------------- 9.4/16.4 MB 1.3 MB/s eta 0:00:06
   ----------------------- ---------------- 9.4/16.4 MB 1.3 MB/s eta 0:00:06
   ----------------------- ---------------- 9.7/16.4 MB 1.3 MB/s eta 0:00:06
   ------------------------ --------------- 10.0/16.4 MB 1.3 MB/s eta 0:00:06
   ------------------------ --------------- 10.2/16.4 MB 1.3 MB/s eta 0:00:05
   ------------------------ --------------- 10.2/16.4 MB 1.3 MB/s eta 0:00:05
   ------------------------- -------------- 10.5/16.4 MB 1.2 MB/s eta 0:00:05
   -------------------------- ------------- 10.7/16.4 MB 1.2 MB/s eta 0:00:05
   -------------------------- ------------- 11.0/16.4 MB 1.2 MB/s eta 0:00:05
   ---------------------------- ----------- 11.5/16.4 MB 1.3 MB/s eta 0:00:04
   ---------------------------- ----------- 11.8/16.4 MB 1.3 MB/s eta 0:00:04
   ------------------------------ --------- 12.3/16.4 MB 1.3 MB/s eta 0:00:04
   ------------------------------ --------- 12.6/16.4 MB 1.3 MB/s eta 0:00:03
   ------------------------------ --------- 12.6/16.4 MB 1.3 MB/s eta 0:00:03
   ------------------------------ --------- 12.6/16.4 MB 1.3 MB/s eta 0:00:03
   ------------------------------ --------- 12.6/16.4 MB 1.3 MB/s eta 0:00:03
   ------------------------------- -------- 12.8/16.4 MB 1.2 MB/s eta 0:00:03
   -------------------------------- ------- 13.4/16.4 MB 1.2 MB/s eta 0:00:03
   -------------------------------- ------- 13.4/16.4 MB 1.2 MB/s eta 0:00:03
   -------------------------------- ------- 13.4/16.4 MB 1.2 MB/s eta 0:00:03
   -------------------------------- ------- 13.4/16.4 MB 1.2 MB/s eta 0:00:03
   --------------------------------- ------ 13.6/16.4 MB 1.2 MB/s eta 0:00:03
   --------------------------------- ------ 13.6/16.4 MB 1.2 MB/s eta 0:00:03
   --------------------------------- ------ 13.6/16.4 MB 1.2 MB/s eta 0:00:03
   --------------------------------- ------ 13.6/16.4 MB 1.2 MB/s eta 0:00:03
   --------------------------------- ------ 13.9/16.4 MB 1.1 MB/s eta 0:00:03
   --------------------------------- ------ 13.9/16.4 MB 1.1 MB/s eta 0:00:03
   ---------------------------------- ----- 14.2/16.4 MB 1.1 MB/s eta 0:00:03
   ----------------------------------- ---- 14.4/16.4 MB 1.1 MB/s eta 0:00:02
   ----------------------------------- ---- 14.7/16.4 MB 1.1 MB/s eta 0:00:02
   ----------------------------------- ---- 14.7/16.4 MB 1.1 MB/s eta 0:00:02
   ----------------------------------- ---- 14.7/16.4 MB 1.1 MB/s eta 0:00:02
   ------------------------------------ --- 14.9/16.4 MB 1.1 MB/s eta 0:00:02
   ------------------------------------- -- 15.5/16.4 MB 1.1 MB/s eta 0:00:01
   -------------------------------------- - 15.7/16.4 MB 1.1 MB/s eta 0:00:01
   ---------------------------------------  16.0/16.4 MB 1.1 MB/s eta 0:00:01
   ---------------------------------------  16.0/16.4 MB 1.1 MB/s eta 0:00:01
   ---------------------------------------  16.0/16.4 MB 1.1 MB/s eta 0:00:01
   ---------------------------------------  16.0/16.4 MB 1.1 MB/s eta 0:00:01
   ---------------------------------------- 16.4/16.4 MB 1.1 MB/s eta 0:00:00
Downloading rtree-1.4.0-py3-none-win_amd64.whl (385 kB)
Downloading tqdm-4.67.1-py3-none-any.whl (78 kB)
Downloading deprecation-2.1.0-py2.py3-none-any.whl (11 kB)
Downloading rasterio-1.4.3-cp311-cp311-win_amd64.whl (25.5 MB)
   ---------------------------------------- 0.0/25.5 MB ? eta -:--:--
   ---------------------------------------- 0.3/25.5 MB ? eta -:--:--
   - -------------------------------------- 0.8/25.5 MB 2.2 MB/s eta 0:00:12
   -- ------------------------------------- 1.3/25.5 MB 2.2 MB/s eta 0:00:11
   -- ------------------------------------- 1.6/25.5 MB 2.3 MB/s eta 0:00:11
   -- ------------------------------------- 1.8/25.5 MB 2.2 MB/s eta 0:00:11
   --- ------------------------------------ 2.1/25.5 MB 1.8 MB/s eta 0:00:14
   --- ------------------------------------ 2.4/25.5 MB 1.7 MB/s eta 0:00:14
   ---- ----------------------------------- 2.6/25.5 MB 1.6 MB/s eta 0:00:15
   ---- ----------------------------------- 2.6/25.5 MB 1.6 MB/s eta 0:00:15
   ---- ----------------------------------- 2.6/25.5 MB 1.6 MB/s eta 0:00:15
   ---- ----------------------------------- 2.9/25.5 MB 1.3 MB/s eta 0:00:19
   ---- ----------------------------------- 3.1/25.5 MB 1.2 MB/s eta 0:00:18
   ----- ---------------------------------- 3.4/25.5 MB 1.3 MB/s eta 0:00:18
   ----- ---------------------------------- 3.7/25.5 MB 1.3 MB/s eta 0:00:18
   ----- ---------------------------------- 3.7/25.5 MB 1.3 MB/s eta 0:00:18
   ------ --------------------------------- 4.2/25.5 MB 1.2 MB/s eta 0:00:18
   ------ --------------------------------- 4.2/25.5 MB 1.2 MB/s eta 0:00:18
   ------- -------------------------------- 4.7/25.5 MB 1.3 MB/s eta 0:00:17
   ------- -------------------------------- 4.7/25.5 MB 1.3 MB/s eta 0:00:17
   -------- ------------------------------- 5.2/25.5 MB 1.2 MB/s eta 0:00:17
   -------- ------------------------------- 5.2/25.5 MB 1.2 MB/s eta 0:00:17
   -------- ------------------------------- 5.2/25.5 MB 1.2 MB/s eta 0:00:17
   -------- ------------------------------- 5.5/25.5 MB 1.1 MB/s eta 0:00:18
   -------- ------------------------------- 5.5/25.5 MB 1.1 MB/s eta 0:00:18
   -------- ------------------------------- 5.5/25.5 MB 1.1 MB/s eta 0:00:18
   --------- ------------------------------ 5.8/25.5 MB 1.1 MB/s eta 0:00:19
   --------- ------------------------------ 5.8/25.5 MB 1.1 MB/s eta 0:00:19
   --------- ------------------------------ 6.0/25.5 MB 1.0 MB/s eta 0:00:19
   --------- ------------------------------ 6.0/25.5 MB 1.0 MB/s eta 0:00:19
   --------- ------------------------------ 6.3/25.5 MB 986.8 kB/s eta 0:00:20
   ---------- ----------------------------- 6.6/25.5 MB 994.1 kB/s eta 0:00:20
   ---------- ----------------------------- 6.6/25.5 MB 994.1 kB/s eta 0:00:20
   ---------- ----------------------------- 6.8/25.5 MB 973.1 kB/s eta 0:00:20
   ----------- ---------------------------- 7.1/25.5 MB 978.0 kB/s eta 0:00:19
   ----------- ---------------------------- 7.1/25.5 MB 978.0 kB/s eta 0:00:19
   ----------- ---------------------------- 7.1/25.5 MB 978.0 kB/s eta 0:00:19
   ----------- ---------------------------- 7.3/25.5 MB 953.6 kB/s eta 0:00:19
   ------------ --------------------------- 7.9/25.5 MB 980.9 kB/s eta 0:00:18
   ------------ --------------------------- 8.1/25.5 MB 994.6 kB/s eta 0:00:18
   ------------- -------------------------- 8.7/25.5 MB 1.0 MB/s eta 0:00:17
   -------------- ------------------------- 8.9/25.5 MB 1.0 MB/s eta 0:00:16
   -------------- ------------------------- 9.2/25.5 MB 1.0 MB/s eta 0:00:16
   -------------- ------------------------- 9.4/25.5 MB 1.0 MB/s eta 0:00:16
   --------------- ------------------------ 9.7/25.5 MB 1.1 MB/s eta 0:00:15
   --------------- ------------------------ 10.0/25.5 MB 1.1 MB/s eta 0:00:15
   ---------------- ----------------------- 10.2/25.5 MB 1.1 MB/s eta 0:00:15
   ---------------- ----------------------- 10.7/25.5 MB 1.1 MB/s eta 0:00:14
   ----------------- ---------------------- 11.3/25.5 MB 1.1 MB/s eta 0:00:13
   ------------------ --------------------- 11.5/25.5 MB 1.1 MB/s eta 0:00:13
   ------------------ --------------------- 12.1/25.5 MB 1.1 MB/s eta 0:00:12
   ------------------- -------------------- 12.3/25.5 MB 1.2 MB/s eta 0:00:12
   -------------------- ------------------- 12.8/25.5 MB 1.2 MB/s eta 0:00:11
   --------------------- ------------------ 13.4/25.5 MB 1.2 MB/s eta 0:00:11
   --------------------- ------------------ 13.6/25.5 MB 1.2 MB/s eta 0:00:10
   --------------------- ------------------ 13.9/25.5 MB 1.2 MB/s eta 0:00:10
   ---------------------- ----------------- 14.2/25.5 MB 1.2 MB/s eta 0:00:10
   ---------------------- ----------------- 14.4/25.5 MB 1.2 MB/s eta 0:00:10
   ----------------------- ---------------- 14.7/25.5 MB 1.2 MB/s eta 0:00:09
   ----------------------- ---------------- 15.2/25.5 MB 1.2 MB/s eta 0:00:09
   ------------------------ --------------- 15.7/25.5 MB 1.2 MB/s eta 0:00:08
   ------------------------- -------------- 16.0/25.5 MB 1.3 MB/s eta 0:00:08
   ------------------------- -------------- 16.5/25.5 MB 1.3 MB/s eta 0:00:08
   -------------------------- ------------- 16.8/25.5 MB 1.3 MB/s eta 0:00:07
   --------------------------- ------------ 17.3/25.5 MB 1.3 MB/s eta 0:00:07
   ---------------------------- ----------- 17.8/25.5 MB 1.3 MB/s eta 0:00:06
   ---------------------------- ----------- 18.4/25.5 MB 1.3 MB/s eta 0:00:06
   ----------------------------- ---------- 18.6/25.5 MB 1.3 MB/s eta 0:00:06
   ----------------------------- ---------- 18.9/25.5 MB 1.3 MB/s eta 0:00:05
   ------------------------------ --------- 19.4/25.5 MB 1.3 MB/s eta 0:00:05
   ------------------------------ --------- 19.7/25.5 MB 1.3 MB/s eta 0:00:05
   ------------------------------- -------- 19.9/25.5 MB 1.3 MB/s eta 0:00:05
   ------------------------------- -------- 20.2/25.5 MB 1.3 MB/s eta 0:00:04
   -------------------------------- ------- 20.7/25.5 MB 1.4 MB/s eta 0:00:04
   -------------------------------- ------- 20.7/25.5 MB 1.4 MB/s eta 0:00:04
   --------------------------------- ------ 21.2/25.5 MB 1.4 MB/s eta 0:00:04
   --------------------------------- ------ 21.5/25.5 MB 1.4 MB/s eta 0:00:03
   ---------------------------------- ----- 22.0/25.5 MB 1.4 MB/s eta 0:00:03
   ----------------------------------- ---- 22.5/25.5 MB 1.4 MB/s eta 0:00:03
   ----------------------------------- ---- 22.8/25.5 MB 1.4 MB/s eta 0:00:02
   ------------------------------------ --- 23.1/25.5 MB 1.4 MB/s eta 0:00:02
   ------------------------------------- -- 23.6/25.5 MB 1.4 MB/s eta 0:00:02
   ------------------------------------- -- 24.1/25.5 MB 1.4 MB/s eta 0:00:01
   -------------------------------------- - 24.6/25.5 MB 1.4 MB/s eta 0:00:01
   ---------------------------------------  25.2/25.5 MB 1.4 MB/s eta 0:00:01
   ---------------------------------------- 25.5/25.5 MB 1.4 MB/s eta 0:00:00
Downloading rasterstats-0.20.0-py3-none-any.whl (17 kB)
Downloading statsmodels-0.14.4-cp311-cp311-win_amd64.whl (9.9 MB)
   ---------------------------------------- 0.0/9.9 MB ? eta -:--:--
   - -------------------------------------- 0.3/9.9 MB ? eta -:--:--
   --- ------------------------------------ 0.8/9.9 MB 1.9 MB/s eta 0:00:05
   ---- ----------------------------------- 1.0/9.9 MB 2.0 MB/s eta 0:00:05
   ----- ---------------------------------- 1.3/9.9 MB 1.9 MB/s eta 0:00:05
   ------ --------------------------------- 1.6/9.9 MB 1.7 MB/s eta 0:00:05
   ------- -------------------------------- 1.8/9.9 MB 1.5 MB/s eta 0:00:06
   -------- ------------------------------- 2.1/9.9 MB 1.5 MB/s eta 0:00:06
   ---------- ----------------------------- 2.6/9.9 MB 1.6 MB/s eta 0:00:05
   ------------ --------------------------- 3.1/9.9 MB 1.7 MB/s eta 0:00:05
   ------------- -------------------------- 3.4/9.9 MB 1.6 MB/s eta 0:00:04
   --------------- ------------------------ 3.9/9.9 MB 1.7 MB/s eta 0:00:04
   ----------------- ---------------------- 4.2/9.9 MB 1.7 MB/s eta 0:00:04
   ------------------- -------------------- 4.7/9.9 MB 1.8 MB/s eta 0:00:03
   -------------------- ------------------- 5.0/9.9 MB 1.8 MB/s eta 0:00:03
   ---------------------- ----------------- 5.5/9.9 MB 1.8 MB/s eta 0:00:03
   ------------------------ --------------- 6.0/9.9 MB 1.8 MB/s eta 0:00:03
   ------------------------- -------------- 6.3/9.9 MB 1.8 MB/s eta 0:00:03
   --------------------------- ------------ 6.8/9.9 MB 1.8 MB/s eta 0:00:02
   ---------------------------- ----------- 7.1/9.9 MB 1.8 MB/s eta 0:00:02
   ----------------------------- ---------- 7.3/9.9 MB 1.8 MB/s eta 0:00:02
   ------------------------------ --------- 7.6/9.9 MB 1.7 MB/s eta 0:00:02
   ------------------------------- -------- 7.9/9.9 MB 1.8 MB/s eta 0:00:02
   -------------------------------- ------- 8.1/9.9 MB 1.7 MB/s eta 0:00:02
   ---------------------------------- ----- 8.4/9.9 MB 1.7 MB/s eta 0:00:01
   ------------------------------------ --- 8.9/9.9 MB 1.7 MB/s eta 0:00:01
   ------------------------------------- -- 9.2/9.9 MB 1.7 MB/s eta 0:00:01
   ---------------------------------------  9.7/9.9 MB 1.7 MB/s eta 0:00:01
   ---------------------------------------- 9.9/9.9 MB 1.7 MB/s eta 0:00:00
Downloading patsy-1.0.1-py2.py3-none-any.whl (232 kB)
Downloading affine-2.4.0-py3-none-any.whl (15 kB)
Downloading simplejson-3.20.1-cp311-cp311-win_amd64.whl (75 kB)
Building wheels for collected packages: spint
  Building wheel for spint (setup.py): started
  Building wheel for spint (setup.py): finished with status 'done'
  Created wheel for spint: filename=spint-1.0.7-py3-none-any.whl size=31364 sha256=13723e29ca805790ef5bfd3539d084b0cc646610024564562278acff53e3e9d3
  Stored in directory: c:\users\mendo\appdata\local\pip\cache\wheels\32\dc\2e\400caaa67e697355772a82b77b8c2ac7cd61633f595c477fd8
Successfully built spint
Installing collected packages: tqdm, simplejson, rtree, pulp, patsy, deprecation, affine, statsmodels, rasterio, rasterstats, access, tobler, segregation, pointpats, momepy, inequality, spglm, spaghetti, spopt, spint, mgwr, pysal
Successfully installed access-1.1.9 affine-2.4.0 deprecation-2.1.0 inequality-1.1.1 mgwr-2.2.1 momepy-0.10.0 patsy-1.0.1 pointpats-2.5.1 pulp-3.2.1 pysal-25.1 rasterio-1.4.3 rasterstats-0.20.0 rtree-1.4.0 segregation-2.5.2 simplejson-3.20.1 spaghetti-1.7.6 spglm-1.1.0 spint-1.0.7 spopt-0.6.1 statsmodels-0.14.4 tobler-0.12.1 tqdm-4.67.1
In [89]:
from pysal.model import spreg

dep_var_name=['Wealth_Index_Above_2nd_Quintile']
ind_vars_names=['Secondary_School_Attendance','Prob_Survival_First5Yrs']
labels=['Wealth Index Above 2nd Quintile %','Secondary-School Attendance %', 'Probability of Survival in the First 5Yrs %']

ols_model = spreg.OLS(
    # Dependent variable
    districts[dep_var_name].values,
    # Independent variables
    districts[ind_vars_names].values,
    # Dependent variable name
    name_y=labels[0],
    # Independent variable name
    name_x=labels[1:],
    name_ds='districts')

print(ols_model.summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: ORDINARY LEAST SQUARES
-----------------------------------------
Data set            :   districts
Weights matrix      :        None
Dependent Variable  :Wealth Index Above 2nd Quintile %                Number of Observations:         633
Mean dependent var  :      0.5690                Number of Variables   :           3
S.D. dependent var  :      0.2539                Degrees of Freedom    :         630
R-squared           :      0.2752
Adjusted R-squared  :      0.2729
Sum squared residual:     29.5313                F-statistic           :    119.6281
Sigma-square        :       0.047                Prob(F-statistic)     :   9.135e-45
S.E. of regression  :       0.217                Log likelihood        :      71.891
Sigma-square ML     :       0.047                Akaike info criterion :    -137.781
S.E of regression ML:      0.2160                Schwarz criterion     :    -124.430

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     t-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT        -2.62387         0.38785        -6.76519         0.00000
Secondary-School Attendance %         0.92627         0.09159        10.11329         0.00000
Probability of Survival in the First 5Yrs %         2.51955         0.43245         5.82620         0.00000
------------------------------------------------------------------------------------

REGRESSION DIAGNOSTICS
MULTICOLLINEARITY CONDITION NUMBER         114.055

TEST ON NORMALITY OF ERRORS
TEST                             DF        VALUE           PROB
Jarque-Bera                       2         11.758           0.0028

DIAGNOSTICS FOR HETEROSKEDASTICITY
RANDOM COEFFICIENTS
TEST                             DF        VALUE           PROB
Breusch-Pagan test                2          4.616           0.0994
Koenker-Bassett test              2          6.913           0.0315
================================ END OF REPORT =====================================
In [91]:
# the dependent variable
moranWI = Moran(districts[dep_var_name], w_knn8)
moranWI.I,moranWI.p_sim
Out[91]:
(np.float64(0.6755988730591118), np.float64(0.001))
In [93]:
# the error term
moranError = Moran(ols_model.u, w_knn8)
moranError.I,moranError.p_sim
Out[93]:
(np.float64(0.6669397918119403), np.float64(0.001))
In [95]:
SAC_model = spreg.ML_Lag(
    # Dependent variable
    districts[dep_var_name].values,
    # Independent variables
    districts[ind_vars_names].values,
    w=w_knn8,
    # Dependent variable name
    name_y=labels[0],
    # Independent variable name
    name_x=labels[1:],
    name_w='KNN8',
    name_ds='districts'
    )

print(SAC_model.summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: MAXIMUM LIKELIHOOD SPATIAL LAG (METHOD = FULL)
-----------------------------------------------------------------
Data set            :   districts
Weights matrix      :        KNN8
Dependent Variable  :Wealth Index Above 2nd Quintile %                Number of Observations:         633
Mean dependent var  :      0.5690                Number of Variables   :           4
S.D. dependent var  :      0.2539                Degrees of Freedom    :         629
Pseudo R-squared    :      0.7186
Spatial Pseudo R-squared:  0.2557
Log likelihood      :    334.9282
Sigma-square ML     :      0.0183                Akaike info criterion :    -661.856
S.E of regression   :      0.1353                Schwarz criterion     :    -644.055

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT        -1.80993         0.24958        -7.25179         0.00000
Secondary-School Attendance %         0.41817         0.06383         6.55095         0.00000
Probability of Survival in the First 5Yrs %         1.64886         0.27522         5.99115         0.00000
W_Wealth Index Above 2nd Quintile %         0.78410         0.02467        31.77713         0.00000
------------------------------------------------------------------------------------

SPATIAL LAG MODEL IMPACTS
Impacts computed using the 'simple' method.
            Variable         Direct        Indirect          Total
Secondary-School Attendance %         0.4182          1.5187          1.9369
Probability of Survival in the First 5Yrs %         1.6489          5.9883          7.6372
================================ END OF REPORT =====================================
In [97]:
SER_model = spreg.ML_Error(
    # Dependent variable
    districts[dep_var_name].values,
    # Independent variables
    districts[ind_vars_names].values,
    w=w_knn8,
    # Dependent variable name
    name_y=labels[0],
    # Independent variable name
    name_x=labels[1:],
    name_w='KNN8',
    name_ds='districts'
    )

print(SER_model.summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: ML SPATIAL ERROR (METHOD = full)
---------------------------------------------------
Data set            :   districts
Weights matrix      :        KNN8
Dependent Variable  :Wealth Index Above 2nd Quintile %                Number of Observations:         633
Mean dependent var  :      0.5690                Number of Variables   :           3
S.D. dependent var  :      0.2539                Degrees of Freedom    :         630
Pseudo R-squared    :      0.2750
Log likelihood      :    361.6024
Sigma-square ML     :      0.0163                Akaike info criterion :    -717.205
S.E of regression   :      0.1277                Schwarz criterion     :    -703.853

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT        -1.99741         0.25990        -7.68545         0.00000
Secondary-School Attendance %         0.81484         0.07657        10.64143         0.00000
Probability of Survival in the First 5Yrs %         1.96555         0.27779         7.07559         0.00000
              lambda         0.85301         0.02416        35.31175         0.00000
------------------------------------------------------------------------------------
================================ END OF REPORT =====================================
C:\Users\mendo\anaconda3\envs\dataespacial__31111\Lib\site-packages\spreg\ml_error.py:184: RuntimeWarning: Method 'bounded' does not support relative tolerance in x; defaulting to absolute tolerance.
  res = minimize_scalar(
In [99]:
SAC_model = spreg.GM_Combo_Het(
    # Dependent variable
    districts[dep_var_name].values,
    # Independent variables
    districts[ind_vars_names].values,
    w=w_knn8,
    # Dependent variable name
    name_y=labels[0],
    # Independent variable name
    name_x=labels[1:],
    name_w='KNN8',
    name_ds='districts'
)

# Print results
print(SAC_model.summary)
REGRESSION RESULTS
------------------

SUMMARY OF OUTPUT: SPATIALLY WEIGHTED 2SLS- GM-COMBO MODEL (HET)
----------------------------------------------------------------
Data set            :   districts
Weights matrix      :        KNN8
Dependent Variable  :Wealth Index Above 2nd Quintile %                Number of Observations:         633
Mean dependent var  :      0.5690                Number of Variables   :           4
S.D. dependent var  :      0.2539                Degrees of Freedom    :         629
Pseudo R-squared    :      0.5238
Spatial Pseudo R-squared:  0.2829
N. of iterations    :           1                Step1c computed       :          No

------------------------------------------------------------------------------------
            Variable     Coefficient       Std.Error     z-Statistic     Probability
------------------------------------------------------------------------------------
            CONSTANT        -2.19490         0.28020        -7.83320         0.00000
Secondary-School Attendance %         0.79369         0.08852         8.96590         0.00000
Probability of Survival in the First 5Yrs %         2.03657         0.29858         6.82088         0.00000
W_Wealth Index Above 2nd Quintile %         0.25672         0.17012         1.50899         0.13130
              lambda         0.82484         0.06037        13.66235         0.00000
------------------------------------------------------------------------------------
Instrumented: W_Wealth Index Above 2nd Quintile %
Instruments: W_Probability of Survival in the First 5Yrs %, W_Secondary-
             School Attendance %
================================ END OF REPORT =====================================