{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "* [Example of use of computing several measures of the PySAL *segregation* module: using `ComputeAll` classes](#Example-of-use-of-computing-several-measures-of-the-PySAL-*segregation*-module:-using-ComputeAll-classes)\n", "\t* [Compute All Aspatial Measures](#Compute-All-Aspatial-Measures)\n", "\t* [Compute All Spatial Measures](#Compute-All-Spatial-Measures)\n", "\t* [Compute All Segregation Measures](#Compute-All-Segregation-Measures)\n" ] }, { "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", "import segregation\n", "import libpysal\n", "import numpy as np\n", "from 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/libpysal/tree/master/libpysal/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(libpysal.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", " \n", " \n", " \n", " \n", " \n", "
MeasureValue
0Dissimilarity0.321847
1Gini0.435065
2Entropy0.094598
3Atkinson0.150793
4Exposure0.768038
5Isolation0.231962
6Concentration Profile0.137687
7Bias Corrected Dissimilarity0.321420
8Density Corrected Dissimilarity0.295205
9Correlation Ratio0.091640
10Modified Dissimilarity0.310746
11Modified Gini0.421793
12Minimun-Maximum0.486965
\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.321420\n", "8 Density Corrected Dissimilarity 0.295205\n", "9 Correlation Ratio 0.091640\n", "10 Modified Dissimilarity 0.310746\n", "11 Modified Gini 0.421793\n", "12 Minimun-Maximum 0.486965" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "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", " \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
14Spatial Minimun-Maximum0.171200
\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\n", "14 Spatial Minimun-Maximum 0.171200" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "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", " \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.321420
8Density Corrected Dissimilarity0.295205
9Correlation Ratio0.091640
10Modified Dissimilarity0.310746
11Modified Gini0.421793
12Minimun-Maximum0.486965
13Spatial Dissimilarity0.261197
14Absolute Centralization0.689142
15Absolute Clustering0.005189
16Absolute Concentration0.851282
17Delta0.804497
18Relative Centralization-0.111942
19Relative Clustering0.009096
20Relative Concentration0.127338
21Distance Decay Exposure0.839658
22Distance Decay Isolation0.156216
23Spatial Proximity Profile0.228473
24Spatial Proximity1.002662
25Boundary Spatial Dissimilarity0.266763
26Perimeter Area Ratio Spatial Dissimilarity0.311172
27Spatial Minimun-Maximum0.171200
\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.321420\n", "8 Density Corrected Dissimilarity 0.295205\n", "9 Correlation Ratio 0.091640\n", "10 Modified Dissimilarity 0.310746\n", "11 Modified Gini 0.421793\n", "12 Minimun-Maximum 0.486965\n", "13 Spatial Dissimilarity 0.261197\n", "14 Absolute Centralization 0.689142\n", "15 Absolute Clustering 0.005189\n", "16 Absolute Concentration 0.851282\n", "17 Delta 0.804497\n", "18 Relative Centralization -0.111942\n", "19 Relative Clustering 0.009096\n", "20 Relative Concentration 0.127338\n", "21 Distance Decay Exposure 0.839658\n", "22 Distance Decay Isolation 0.156216\n", "23 Spatial Proximity Profile 0.228473\n", "24 Spatial Proximity 1.002662\n", "25 Boundary Spatial Dissimilarity 0.266763\n", "26 Perimeter Area Ratio Spatial Dissimilarity 0.311172\n", "27 Spatial Minimun-Maximum 0.171200" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "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 }