The Compute_All classes comprises simple and quick functions to assess multiple segregation measures at once in a dataset. It uses all the default parameters and returns an object that has an attribute (.computed) of a dictionary with summary of all values fitted.
The wrappers have currently three classes: ComputeAllAspatialSegregation, ComputeAllSpatialSegregation and ComputeAllSegregation which comprises all the measures available.
Firstly, we need to import the libraries and functions to be used.
%matplotlib inline
import geopandas as gpd
import segregation
import libpysal
import numpy as np
from segregation.compute_all import ComputeAllAspatialSegregation, ComputeAllSpatialSegregation, ComputeAllSegregation
Then it's time to load some data to estimate segregation. We use the data of 2000 Census Tract Data for the metropolitan area of Sacramento, CA, USA.
We use a geopandas dataframe available in PySAL examples repository.
For more information about the data: https://github.com/pysal/libpysal/tree/master/libpysal/examples/sacramento2
s_map = gpd.read_file(libpysal.examples.get_path("sacramentot2.shp"))
s_map.columns
The data have several demographic variables. We are going to assess the segregation of the Hispanic Population (variable 'HISP_'). For this, we only extract some columns of the geopandas dataframe.
gdf = s_map[['geometry', 'HISP_', 'TOT_POP']]
np.random.seed(123)
aspatial_fit = ComputeAllAspatialSegregation(gdf, 'HISP_', 'TOT_POP')
aspatial_fit.computed
np.random.seed(123)
spatial_fit = ComputeAllSpatialSegregation(gdf, 'HISP_', 'TOT_POP')
spatial_fit.computed
np.random.seed(123)
segregation_fit = ComputeAllSegregation(gdf, 'HISP_', 'TOT_POP')
segregation_fit.computed