{ "cells": [ { "cell_type": "markdown", "id": "nominated-breakdown", "metadata": {}, "source": [ "# Multi-group Segregation Indices" ] }, { "cell_type": "code", "execution_count": 1, "id": "ef04f168-d859-4c12-a3e0-aff7df0b0b2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Author: eli knaap\n", "\n", "Last updated: 2021-05-09\n", "\n", "Python implementation: CPython\n", "Python version : 3.9.2\n", "IPython version : 7.23.1\n", "\n", "segregation: 2.0.0\n", "geopandas : 0.9.0\n", "libpysal : 4.3.0\n", "pandana : 0.6.1\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'eli knaap' -v -d -u -p segregation,geopandas,libpysal,pandana" ] }, { "cell_type": "markdown", "id": "b580cc30-29dc-4561-9513-a141ba39514b", "metadata": {}, "source": [ "Classes for computing multigroup segregation indices are in the `multigroup` module" ] }, { "cell_type": "code", "execution_count": 2, "id": "bf5fb7d9-8843-4eaa-818d-b25acf9e2345", "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import matplotlib.pyplot as plt\n", "\n", "from libpysal.examples import load_example\n", "from segregation.multigroup import MultiDissim, MultiInfoTheory" ] }, { "cell_type": "code", "execution_count": 3, "id": "9f20f5a1-8d25-4976-89ba-ed66905f5650", "metadata": {}, "outputs": [], "source": [ "sacramento = gpd.read_file(load_example(\"Sacramento1\").get_path(\"sacramentot2.shp\"))\n", "sacramento = sacramento.to_crs(sacramento.estimate_utm_crs())" ] }, { "cell_type": "markdown", "id": "fe313580-070e-48d1-9ad7-4925fdadc22c", "metadata": {}, "source": [ "## Aspatial Segregation Indices" ] }, { "cell_type": "code", "execution_count": 4, "id": "441a1c53-a267-4f24-919a-19aa8a6770c5", "metadata": {}, "outputs": [], "source": [ "multi_dissim = MultiDissim(sacramento, groups=['WHITE', 'BLACK', 'HISP'])" ] }, { "cell_type": "code", "execution_count": 5, "id": "ee2a6015-9703-490f-8a98-e0e340e76b72", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.42469982288295693" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multi_dissim.statistic" ] }, { "cell_type": "code", "execution_count": 6, "id": "19252c53-8411-4850-88c5-347b4d0347df", "metadata": {}, "outputs": [], "source": [ "multi_info = MultiInfoTheory(sacramento, groups=['WHITE', 'BLACK', 'HISP'])" ] }, { "cell_type": "code", "execution_count": 7, "id": "9e960e74-2b96-49ee-b0d0-55f27757169e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1800803002655424" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multi_info.statistic" ] }, { "cell_type": "markdown", "id": "f2f95a02-ae16-4210-9919-33a4646824ce", "metadata": {}, "source": [ "## Spatial Segregation Indices" ] }, { "cell_type": "markdown", "id": "ba6ecd48-d155-4169-962c-1778bcdf8f89", "metadata": {}, "source": [ "As with single group measures, generalized spatial versions of multigroup indices can be created by passing a distance parameter or a `W`/`Network` object." ] }, { "cell_type": "code", "execution_count": 8, "id": "980b6183-b060-4a57-bcbf-ff7a2224911f", "metadata": {}, "outputs": [], "source": [ "spatial_multi_dissim = MultiDissim(sacramento, groups=['WHITE', 'BLACK', 'HISP'], distance=2000)" ] }, { "cell_type": "code", "execution_count": 9, "id": "1998948e-0a9e-447a-8be6-d2f63724ff2b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3776841098505291" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spatial_multi_dissim.statistic" ] }, { "cell_type": "code", "execution_count": 10, "id": "088ca7ac-361b-441e-ae3a-2c37827017a3", "metadata": {}, "outputs": [], "source": [ "from pandana import Network" ] }, { "cell_type": "code", "execution_count": 11, "id": "781fb45f-be69-4c19-8463-3305ae74894a", "metadata": {}, "outputs": [], "source": [ "net = Network.from_hdf5(\"../40900.h5\")" ] }, { "cell_type": "code", "execution_count": 12, "id": "97b73a0c-c329-417a-8011-33ce2f44332a", "metadata": {}, "outputs": [], "source": [ "net_multi_dissim = MultiDissim(sacramento, groups=['WHITE', 'BLACK', 'HISP'], distance=2000, network=net, decay='linear')" ] }, { "cell_type": "code", "execution_count": 13, "id": "60add795-76ed-4a4a-8ee6-4be5060c3a30", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3997196467720179" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "net_multi_dissim.statistic" ] }, { "cell_type": "markdown", "id": "9835fd10-7fe3-429f-962c-a8329a621454", "metadata": {}, "source": [ "## Batch-Computing Multi-Group Measures" ] }, { "cell_type": "markdown", "id": "5359e559-509e-46b2-8d1d-c727688a8393", "metadata": {}, "source": [ "To compute all single group indices in one go, the package provides a wrapper function in the `batch` module similar to single-group indices" ] }, { "cell_type": "code", "execution_count": 14, "id": "58e47a6b-6f34-473a-9312-bc537713a556", "metadata": {}, "outputs": [], "source": [ "from segregation.batch import batch_compute_multigroup" ] }, { "cell_type": "code", "execution_count": 16, "id": "f41712ce-8923-4df8-8f46-13b21adf99c9", "metadata": {}, "outputs": [], "source": [ "all_multigroup = batch_compute_multigroup(sacramento, groups=['WHITE', 'BLACK', 'HISP'],)" ] }, { "cell_type": "code", "execution_count": 17, "id": "39ad47fa-51b1-4b52-bb9e-ff0b8227f3b7", "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", "
Statistic
MultiDissim0.424700
MultiDivergence0.131709
MultiDiversity0.731390
MultiGini0.556467
MultiInfoTheory0.180080
MultiNormExposure0.191362
MultiRelativeDiversity0.168574
MultiSquaredCoefVar0.145315
SimpsonsConcentration0.587698
SimpsonsInteraction0.412302
\n", "
" ], "text/plain": [ " Statistic\n", "MultiDissim 0.424700\n", "MultiDivergence 0.131709\n", "MultiDiversity 0.731390\n", "MultiGini 0.556467\n", "MultiInfoTheory 0.180080\n", "MultiNormExposure 0.191362\n", "MultiRelativeDiversity 0.168574\n", "MultiSquaredCoefVar 0.145315\n", "SimpsonsConcentration 0.587698\n", "SimpsonsInteraction 0.412302" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_multigroup" ] }, { "cell_type": "code", "execution_count": null, "id": "0b023ec4-34ea-4646-9572-fa78062ade9c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:segregation]", "language": "python", "name": "conda-env-segregation-py" }, "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.9.2" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }