{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example of use of computing several measures of the PySAL *segregation* module: using `ComputeAll` classes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"The wrappers have currently three classes: ComputeAllAspatialSegregation, ComputeAllSpatialSegregation and ComputeAllSegregation which comprises all the measures available.\n",
"\n",
"Firstly, we need to import the libraries and functions to be used."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import geopandas as gpd\n",
"from pysal.explore import segregation\n",
"import pysal.lib\n",
"from pysal.explore.segregation.compute_all import ComputeAllAspatialSegregation, ComputeAllSpatialSegregation, ComputeAllSegregation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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. \n",
"\n",
"We use a geopandas dataframe available in PySAL examples repository.\n",
"\n",
"For more information about the data: https://github.com/pysal/pysal.lib/tree/master/pysal.lib/examples/sacramento2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['FIPS', 'MSA', 'TOT_POP', 'POP_16', 'POP_65', 'WHITE_', 'BLACK_',\n",
" 'ASIAN_', 'HISP_', 'MULTI_RA', 'MALES', 'FEMALES', 'MALE1664',\n",
" 'FEM1664', 'EMPL16', 'EMP_AWAY', 'EMP_HOME', 'EMP_29', 'EMP_30',\n",
" 'EMP16_2', 'EMP_MALE', 'EMP_FEM', 'OCC_MAN', 'OCC_OFF1', 'OCC_INFO',\n",
" 'HH_INC', 'POV_POP', 'POV_TOT', 'HSG_VAL', 'FIPSNO', 'POLYID',\n",
" 'geometry'],\n",
" dtype='object')"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s_map = gpd.read_file(pysal.lib.examples.get_path(\"sacramentot2.shp\"))\n",
"s_map.columns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"gdf = s_map[['geometry', 'HISP_', 'TOT_POP']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute All Aspatial Measures"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Measure | \n",
" Value | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Dissimilarity | \n",
" 0.321847 | \n",
"
\n",
" \n",
" 1 | \n",
" Gini | \n",
" 0.435065 | \n",
"
\n",
" \n",
" 2 | \n",
" Entropy | \n",
" 0.094598 | \n",
"
\n",
" \n",
" 3 | \n",
" Atkinson | \n",
" 0.150793 | \n",
"
\n",
" \n",
" 4 | \n",
" Exposure | \n",
" 0.768038 | \n",
"
\n",
" \n",
" 5 | \n",
" Isolation | \n",
" 0.231962 | \n",
"
\n",
" \n",
" 6 | \n",
" Concentration Profile | \n",
" 0.137687 | \n",
"
\n",
" \n",
" 7 | \n",
" Bias Corrected Dissimilarity | \n",
" 0.321431 | \n",
"
\n",
" \n",
" 8 | \n",
" Density Corrected Dissimilarity | \n",
" 0.295205 | \n",
"
\n",
" \n",
" 9 | \n",
" Correlation Ratio | \n",
" 0.091640 | \n",
"
\n",
" \n",
" 10 | \n",
" Modified Dissimilarity | \n",
" 0.310733 | \n",
"
\n",
" \n",
" 11 | \n",
" Modified Gini | \n",
" 0.421795 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Measure Value\n",
"0 Dissimilarity 0.321847\n",
"1 Gini 0.435065\n",
"2 Entropy 0.094598\n",
"3 Atkinson 0.150793\n",
"4 Exposure 0.768038\n",
"5 Isolation 0.231962\n",
"6 Concentration Profile 0.137687\n",
"7 Bias Corrected Dissimilarity 0.321431\n",
"8 Density Corrected Dissimilarity 0.295205\n",
"9 Correlation Ratio 0.091640\n",
"10 Modified Dissimilarity 0.310733\n",
"11 Modified Gini 0.421795"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aspatial_fit = ComputeAllAspatialSegregation(gdf, 'HISP_', 'TOT_POP')\n",
"aspatial_fit.computed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute All Spatial Measures"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Measure | \n",
" Value | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Spatial Dissimilarity | \n",
" 0.261197 | \n",
"
\n",
" \n",
" 1 | \n",
" Absolute Centralization | \n",
" 0.689142 | \n",
"
\n",
" \n",
" 2 | \n",
" Absolute Clustering | \n",
" 0.005189 | \n",
"
\n",
" \n",
" 3 | \n",
" Absolute Concentration | \n",
" 0.851282 | \n",
"
\n",
" \n",
" 4 | \n",
" Delta | \n",
" 0.804497 | \n",
"
\n",
" \n",
" 5 | \n",
" Relative Centralization | \n",
" -0.111942 | \n",
"
\n",
" \n",
" 6 | \n",
" Relative Clustering | \n",
" 0.009096 | \n",
"
\n",
" \n",
" 7 | \n",
" Relative Concentration | \n",
" 0.127338 | \n",
"
\n",
" \n",
" 8 | \n",
" Distance Decay Exposure | \n",
" 0.839658 | \n",
"
\n",
" \n",
" 9 | \n",
" Distance Decay Isolation | \n",
" 0.156216 | \n",
"
\n",
" \n",
" 10 | \n",
" Spatial Proximity Profile | \n",
" 0.228473 | \n",
"
\n",
" \n",
" 11 | \n",
" Spatial Proximity | \n",
" 1.002662 | \n",
"
\n",
" \n",
" 12 | \n",
" Boundary Spatial Dissimilarity | \n",
" 0.266763 | \n",
"
\n",
" \n",
" 13 | \n",
" Perimeter Area Ratio Spatial Dissimilarity | \n",
" 0.311172 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Measure Value\n",
"0 Spatial Dissimilarity 0.261197\n",
"1 Absolute Centralization 0.689142\n",
"2 Absolute Clustering 0.005189\n",
"3 Absolute Concentration 0.851282\n",
"4 Delta 0.804497\n",
"5 Relative Centralization -0.111942\n",
"6 Relative Clustering 0.009096\n",
"7 Relative Concentration 0.127338\n",
"8 Distance Decay Exposure 0.839658\n",
"9 Distance Decay Isolation 0.156216\n",
"10 Spatial Proximity Profile 0.228473\n",
"11 Spatial Proximity 1.002662\n",
"12 Boundary Spatial Dissimilarity 0.266763\n",
"13 Perimeter Area Ratio Spatial Dissimilarity 0.311172"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spatial_fit = ComputeAllSpatialSegregation(gdf, 'HISP_', 'TOT_POP')\n",
"spatial_fit.computed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Compute All Segregation Measures"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Measure | \n",
" Value | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Dissimilarity | \n",
" 0.321847 | \n",
"
\n",
" \n",
" 1 | \n",
" Gini | \n",
" 0.435065 | \n",
"
\n",
" \n",
" 2 | \n",
" Entropy | \n",
" 0.094598 | \n",
"
\n",
" \n",
" 3 | \n",
" Atkinson | \n",
" 0.150793 | \n",
"
\n",
" \n",
" 4 | \n",
" Exposure | \n",
" 0.768038 | \n",
"
\n",
" \n",
" 5 | \n",
" Isolation | \n",
" 0.231962 | \n",
"
\n",
" \n",
" 6 | \n",
" Concentration Profile | \n",
" 0.137687 | \n",
"
\n",
" \n",
" 7 | \n",
" Bias Corrected Dissimilarity | \n",
" 0.321369 | \n",
"
\n",
" \n",
" 8 | \n",
" Density Corrected Dissimilarity | \n",
" 0.295205 | \n",
"
\n",
" \n",
" 9 | \n",
" Correlation Ratio | \n",
" 0.091640 | \n",
"
\n",
" \n",
" 10 | \n",
" Modified Dissimilarity | \n",
" 0.310738 | \n",
"
\n",
" \n",
" 11 | \n",
" Modified Gini | \n",
" 0.421745 | \n",
"
\n",
" \n",
" 12 | \n",
" Spatial Dissimilarity | \n",
" 0.261197 | \n",
"
\n",
" \n",
" 13 | \n",
" Absolute Centralization | \n",
" 0.689142 | \n",
"
\n",
" \n",
" 14 | \n",
" Absolute Clustering | \n",
" 0.005189 | \n",
"
\n",
" \n",
" 15 | \n",
" Absolute Concentration | \n",
" 0.851282 | \n",
"
\n",
" \n",
" 16 | \n",
" Delta | \n",
" 0.804497 | \n",
"
\n",
" \n",
" 17 | \n",
" Relative Centralization | \n",
" -0.111942 | \n",
"
\n",
" \n",
" 18 | \n",
" Relative Clustering | \n",
" 0.009096 | \n",
"
\n",
" \n",
" 19 | \n",
" Relative Concentration | \n",
" 0.127338 | \n",
"
\n",
" \n",
" 20 | \n",
" Distance Decay Exposure | \n",
" 0.839658 | \n",
"
\n",
" \n",
" 21 | \n",
" Distance Decay Isolation | \n",
" 0.156216 | \n",
"
\n",
" \n",
" 22 | \n",
" Spatial Proximity Profile | \n",
" 0.228473 | \n",
"
\n",
" \n",
" 23 | \n",
" Spatial Proximity | \n",
" 1.002662 | \n",
"
\n",
" \n",
" 24 | \n",
" Boundary Spatial Dissimilarity | \n",
" 0.266763 | \n",
"
\n",
" \n",
" 25 | \n",
" Perimeter Area Ratio Spatial Dissimilarity | \n",
" 0.311172 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Measure Value\n",
"0 Dissimilarity 0.321847\n",
"1 Gini 0.435065\n",
"2 Entropy 0.094598\n",
"3 Atkinson 0.150793\n",
"4 Exposure 0.768038\n",
"5 Isolation 0.231962\n",
"6 Concentration Profile 0.137687\n",
"7 Bias Corrected Dissimilarity 0.321369\n",
"8 Density Corrected Dissimilarity 0.295205\n",
"9 Correlation Ratio 0.091640\n",
"10 Modified Dissimilarity 0.310738\n",
"11 Modified Gini 0.421745\n",
"12 Spatial Dissimilarity 0.261197\n",
"13 Absolute Centralization 0.689142\n",
"14 Absolute Clustering 0.005189\n",
"15 Absolute Concentration 0.851282\n",
"16 Delta 0.804497\n",
"17 Relative Centralization -0.111942\n",
"18 Relative Clustering 0.009096\n",
"19 Relative Concentration 0.127338\n",
"20 Distance Decay Exposure 0.839658\n",
"21 Distance Decay Isolation 0.156216\n",
"22 Spatial Proximity Profile 0.228473\n",
"23 Spatial Proximity 1.002662\n",
"24 Boundary Spatial Dissimilarity 0.266763\n",
"25 Perimeter Area Ratio Spatial Dissimilarity 0.311172"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"segregation_fit = ComputeAllSegregation(gdf, 'HISP_', 'TOT_POP')\n",
"segregation_fit.computed"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}