{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MeasureValue
0Dissimilarity0.321847
1Gini0.435065
2Entropy0.094598
3Atkinson0.150793
4Exposure0.768038
5Isolation0.231962
6Concentration Profile0.137687
7Bias Corrected Dissimilarity0.321431
8Density Corrected Dissimilarity0.295205
9Correlation Ratio0.091640
10Modified Dissimilarity0.310733
11Modified Gini0.421795
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MeasureValue
0Spatial Dissimilarity0.261197
1Absolute Centralization0.689142
2Absolute Clustering0.005189
3Absolute Concentration0.851282
4Delta0.804497
5Relative Centralization-0.111942
6Relative Clustering0.009096
7Relative Concentration0.127338
8Distance Decay Exposure0.839658
9Distance Decay Isolation0.156216
10Spatial Proximity Profile0.228473
11Spatial Proximity1.002662
12Boundary Spatial Dissimilarity0.266763
13Perimeter Area Ratio Spatial Dissimilarity0.311172
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
MeasureValue
0Dissimilarity0.321847
1Gini0.435065
2Entropy0.094598
3Atkinson0.150793
4Exposure0.768038
5Isolation0.231962
6Concentration Profile0.137687
7Bias Corrected Dissimilarity0.321369
8Density Corrected Dissimilarity0.295205
9Correlation Ratio0.091640
10Modified Dissimilarity0.310738
11Modified Gini0.421745
12Spatial Dissimilarity0.261197
13Absolute Centralization0.689142
14Absolute Clustering0.005189
15Absolute Concentration0.851282
16Delta0.804497
17Relative Centralization-0.111942
18Relative Clustering0.009096
19Relative Concentration0.127338
20Distance Decay Exposure0.839658
21Distance Decay Isolation0.156216
22Spatial Proximity Profile0.228473
23Spatial Proximity1.002662
24Boundary Spatial Dissimilarity0.266763
25Perimeter Area Ratio Spatial Dissimilarity0.311172
\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 }