{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Single-layer Territory Management. Optimizing territories considering only clients\n",
"\n",
"In territory management, a territory is a customer group or geographic area over which either an individual salesperson or a sales team has responsibility. These territories are usually defined based on geography, sales potential, number of clients or a combination of these factors.\n",
"\n",
"The main complexity in territory management is to create areas that are balanced with regards to more than one factor that usually behave very differently. There is no one-size-fits-all solution, and if the balance is off, sales management is likely to leave someone within their organization unhappy or leave money on the table. This is why it is very important to identify and understand all the components and requirements of your use case to apply the most appropriate technique.\n",
"\n",
"We can differentiate between two main use cases: when the location of sales reps is important (usually because they have to travel to visit their clients) and when it is not (travel rarely occurs). The first case is clearly more complex than the latter.\n",
"\n",
"In this notebook we will use two different techniques to solve territory management problems when only the location of clients needs to be considered, i.e., we will have a single layer of data consisting of client locations. We will prove the value Spatial Data Science techniques by showing their additional value compared to traditional techniques."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use case description\n",
"\n",
"A pharma lab is interested in balancing their sales territories in the state of Texas based on the number of current and potential clients per territory. \n",
"\n",
"Their clients are mainly offices and clinics of medical doctors.\n",
"\n",
"They are interested in creating 5 balanced territories.\n",
"\n",
"We will use the following two datasets from [CARTO's Data Observatory](https://carto.com/spatial-data-catalog/):\n",
"- Points of Interest (POIs). In particular, office and clinic of medical doctors POIs. We will use [Pitney Bowes POI-Consumer dataset](https://carto.com/spatial-data-catalog/browser/dataset/pb_consumer_po_62cddc04/).\n",
"- Texas boundary geometry. We'll use [Who's on First GeoJSON - Global dataset](https://carto.com/spatial-data-catalog/browser/geography/wof_geojson_4e78587c/).\n",
"\n",
"*Note* the POI dataset is premium and a subscription is needed to access this data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 0. Setup\n",
"\n",
"We'll start by importing all packages we'll use."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import geopandas as gpd\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"from cartoframes.auth import set_default_credentials\n",
"from cartoframes.data.observatory import *\n",
"from cartoframes.viz import *\n",
"from h3 import h3\n",
"from libpysal.weights import Rook\n",
"from shapely import wkt\n",
"from shapely.geometry import mapping, Polygon\n",
"from sklearn.cluster import KMeans\n",
"from spopt.region.maxp import MaxPHeuristic\n",
"\n",
"pd.set_option('display.max_columns', None)\n",
"plt.rc('axes', titlesize='large')\n",
"plt.rc('xtick', labelsize='large')\n",
"plt.rc('ytick', labelsize='large')\n",
"sns.set_style('whitegrid')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to be able to use the Data Observatory via CARTOframes, you need to set your CARTO account credentials first.\n",
"\n",
"Please, visit the [Authentication guide](https://carto.com/developers/cartoframes/guides/Authentication/) for further detail."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"set_default_credentials('creds.json')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 0.1. Functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function creates an [H3](https://eng.uber.com/h3/) polyfill of the polygon and at the resolution indicated."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"def create_h3_grid(polygon, resolution=8):\n",
" hex_id_list = list(h3.polyfill(geojson = mapping(polygon), res = resolution, geo_json_conformant=True))\n",
" hexagon_list = list(map(lambda x : Polygon(h3.h3_to_geo_boundary(h=x, geo_json=True)), hex_id_list))\n",
" grid = pd.DataFrame(data={'hex_id':hex_id_list, 'geometry':hexagon_list})\n",
" grid = gpd.GeoDataFrame(grid, crs='epsg:4326')\n",
" return grid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The function below is used throughout the analysis to check is clusters are balanced based on different metrics.\n",
"\n",
"The function arguments are:\n",
"- `cluster_names` so that we can provide descriptive names to clusters\n",
"- `areas_df` is the GeoDataFrame\n",
"- `groupby` is the column with the cluster to which each cell belongs to\n",
"- `**kaggregations` for the different metrics we'd like to calculate"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [],
"source": [
"def plot_clinic_balance(clusters, areas_df, groupby, **kaggregations):\n",
" areas_df_g = areas_df.groupby(groupby).agg(kaggregations).reset_index()\n",
"\n",
" n_plots = len(kaggregations)\n",
" fig, axs = plt.subplots(1, n_plots, figsize=(9 + 3*n_plots,4))\n",
" if n_plots == 1:\n",
" axs = [axs]\n",
" \n",
" for i in range(n_plots):\n",
" sns.barplot(y=groupby, x=list(kaggregations.keys())[i], data=areas_df_g, order=clusters, \n",
" palette=['#7F3C8D','#11A579','#3969AC','#F2B701','#E73F74'], ax=axs[i])\n",
" axs[i].set_xlabel(list(kaggregations.keys())[i], fontsize=13)\n",
" axs[i].set_ylabel('Sales rep locations', fontsize=13)\n",
" \n",
" fig.tight_layout()\n",
" \n",
" return axs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Download and visualize data\n",
"\n",
"Next, we will download the data described in the usecase using [CARTOframes](https://carto.com/developers/cartoframes/).\n",
"\n",
"*Note* in this notebook some prior knowledge on how to explore and download data from the [Data Observatory](https://carto.com/spatial-data-catalog/) is assumed. If this is your first time exploring and downloading data from the [Data Observatory](https://carto.com/spatial-data-catalog/), take a look at [CARTOframes Guides](https://carto.com/developers/cartoframes/guides/Introduction/) and the [Data Observatory examples](https://carto.com/developers/cartoframes/guides/Data-Observatory/) and **discover how easy it is to get started!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.1 Texas boundary geometry\n",
"\n",
"We are interested in the geometry of the state of Texas. We'll download it from [Who's on First GeoJSON - Global dataset](https://carto.com/spatial-data-catalog/browser/geography/wof_geojson_4e78587c/)."
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'slug': 'wof_geojson_4e78587c',\n",
" 'name': 'GeoJSON - Global',\n",
" 'description': \"The main table in Who's On First. Holds all the relevant information for a place in the 'body' JSON field.\",\n",
" 'country_id': 'glo',\n",
" 'provider_id': 'whos_on_first',\n",
" 'geom_type': 'MULTIPLE',\n",
" 'update_frequency': None,\n",
" 'is_public_data': True,\n",
" 'lang': 'eng',\n",
" 'version': '20190520',\n",
" 'provider_name': \"Who's On First\",\n",
" 'id': 'carto-do-public-data.whos_on_first.geography_glo_geojson_20190520'}"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wof_grographies = Geography.get('wof_geojson_4e78587c')\n",
"wof_grographies.to_dict()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
" geoid id body \\\n",
"0 85688753 85688753 {\"id\": 85688753, \"type\": \"Feature\", \"propertie... \n",
"\n",
" name country parent_id is_current placetype geometry_type \\\n",
"0 Texas US 85633793 1 region Polygon \n",
"\n",
" bbox \\\n",
"0 POLYGON((-93.508039 25.837164, -93.508039 36.5... \n",
"\n",
" geom lastmodified \\\n",
"0 POLYGON ((-103.06466 32.95910, -103.06460 32.9... 1555446728 \n",
"\n",
" lastmodified_timestamp \n",
"0 2019-04-16 20:32:08+00:00 "
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"state_name = 'Texas'\n",
"country_code = 'US'\n",
"placetype = 'region'\n",
"\n",
"sql_query = f\"\"\"SELECT * \n",
" FROM $geography$ \n",
" WHERE name = '{state_name}' AND \n",
" country = '{country_code}' AND \n",
" placetype='{placetype}'\"\"\"\n",
"\n",
"tx_boundary = wof_grographies.to_dataframe(sql_query=sql_query)\n",
"\n",
"tx_boundary['geom'] = list(map(wkt.loads, tx_boundary['geom']))\n",
"tx_boundary = gpd.GeoDataFrame(tx_boundary, geometry='geom', crs='epsg:4326')\n",
"tx_boundary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.2. Client locations\n",
"\n",
"We'll download all POIs in Texas classified as \"OFFICES AND CLINICS OF MEDICAL DOCTORS\" from [Pitney Bowes POI-Consumer dataset](https://carto.com/spatial-data-catalog/browser/dataset/pb_consumer_po_62cddc04/).\n",
"\n",
"*Note* this is a premium dataset and a subscription is required."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"poi_dataset = Dataset.get('pb_consumer_po_62cddc04')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
geoid
\n",
"
do_date
\n",
"
NAME
\n",
"
BRANDNAME
\n",
"
PB_ID
\n",
"
TRADE_NAME
\n",
"
FRANCHISE_NAME
\n",
"
ISO3
\n",
"
AREANAME4
\n",
"
AREANAME3
\n",
"
AREANAME2
\n",
"
AREANAME1
\n",
"
STABB
\n",
"
POSTCODE
\n",
"
FORMATTEDADDRESS
\n",
"
MAINADDRESSLINE
\n",
"
ADDRESSLASTLINE
\n",
"
LONGITUDE
\n",
"
LATITUDE
\n",
"
GEORESULT
\n",
"
CONFIDENCE_CODE
\n",
"
COUNTRY_ACCESS_CODE
\n",
"
TEL_NUM
\n",
"
FAXNUM
\n",
"
EMAIL
\n",
"
HTTP
\n",
"
OPEN_24H
\n",
"
BUSINESS_LINE
\n",
"
SIC1
\n",
"
SIC2
\n",
"
SIC8
\n",
"
SIC8_DESCRIPTION
\n",
"
ALT_INDUSTRY_CODE
\n",
"
MICODE
\n",
"
TRADE_DIVISION
\n",
"
GROUP
\n",
"
CLASS
\n",
"
SUB_CLASS
\n",
"
EMPLOYEE_HERE
\n",
"
EMPLOYEE_COUNT
\n",
"
YEAR_START
\n",
"
SALES_VOLUME_LOCAL
\n",
"
SALES_VOLUME_US_DOLLARS
\n",
"
CURRENCY_CODE
\n",
"
AGENT_CODE
\n",
"
LEGAL_STATUS_CODE
\n",
"
STATUS_CODE
\n",
"
SUBSIDIARY_INDICATOR
\n",
"
PARENT_BUSINESS_NAME
\n",
"
PARENT_ADDRESS
\n",
"
PARENT_STREET_ADDRESS
\n",
"
PARENT_AREANAME3
\n",
"
PARENT_AREANAME1
\n",
"
PARENT_COUNTRY
\n",
"
PARENT_POSTCODE
\n",
"
DOMESTIC_ULTIMATE_BUSINESS_NAME
\n",
"
DOMESTIC_ULTIMATE_ADDRESS
\n",
"
DOMESTIC_ULTIMATE_STREET_ADDRESS
\n",
"
DOMESTIC_ULTIMATE_AREANAME3
\n",
"
DOMESTIC_ULTIMATE_AREANAME1
\n",
"
DOMESTIC_ULTIMATE_POSTCODE
\n",
"
GLOBAL_ULTIMATE_INDICATOR
\n",
"
GLOBAL_ULTIMATE_BUSINESS_NAME
\n",
"
GLOBAL_ULTIMATE_ADDRESS
\n",
"
GLOBAL_ULTIMATE_STREET_ADDRESS
\n",
"
GLOBAL_ULTIMATE_AREANAME3
\n",
"
GLOBAL_ULTIMATE_AREANAME1
\n",
"
GLOBAL_ULTIMATE_COUNTRY
\n",
"
GLOBAL_ULTIMATE_POSTCODE
\n",
"
FAMILY_MEMBERS
\n",
"
HIERARCHY_CODE
\n",
"
TICKER_SYMBOL
\n",
"
EXCHANGE_NAME
\n",
"
geom
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
1484649291#-95.391433#29.730764
\n",
"
2020-08-01
\n",
"
CHARLES GARCIA
\n",
"
NaN
\n",
"
1484649291
\n",
"
CHARLES GARCIA, MD PA
\n",
"
NaN
\n",
"
USA
\n",
"
NaN
\n",
"
HOUSTON
\n",
"
NaN
\n",
"
TEXAS
\n",
"
TX
\n",
"
77006-6122
\n",
"
4704 MONTROSE BLVD, HOUSTON, TX, 77006-6122
\n",
"
4704 MONTROSE BLVD
\n",
"
HOUSTON, TX, 77006-6122
\n",
"
-95.391433
\n",
"
29.730764
\n",
"
S8HPNTSCZA
\n",
"
HIGH
\n",
"
1.0
\n",
"
(713) 333-0151
\n",
"
NaN
\n",
"
NaN
\n",
"
WWW.CHARLESGARCIAMD.COM
\n",
"
NaN
\n",
"
OFFICES AND CLINICS MEDICAL DOCTORS,NSK
\n",
"
8011.0
\n",
"
NaN
\n",
"
80110513
\n",
"
OPTHALMOLOGIST
\n",
"
621111.0
\n",
"
10942513
\n",
"
DIVISION I. - SERVICES
\n",
"
HEALTH SERVICES
\n",
"
OFFICES AND CLINICS OF DOCTORS OF MEDICINE
\n",
"
OFFICES AND CLINICS OF MEDICAL DOCTORS
\n",
"
75.0
\n",
"
75.0
\n",
"
2019.0
\n",
"
515248.0
\n",
"
515248.0
\n",
"
20.0
\n",
"
G
\n",
"
13.0
\n",
"
0.0
\n",
"
0.0
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
N
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
0.0
\n",
"
0.0
\n",
"
NaN
\n",
"
NaN
\n",
"
POINT (-95.39143 29.73076)
\n",
"
\n",
"
\n",
"
1
\n",
"
1121837904#-93.987719#29.956197
\n",
"
2020-08-01
\n",
"
SOUTHEAST TEXAS IMAGING LLP
\n",
"
NaN
\n",
"
1121837904
\n",
"
NaN
\n",
"
NaN
\n",
"
USA
\n",
"
NaN
\n",
"
NEDERLAND
\n",
"
NaN
\n",
"
TEXAS
\n",
"
TX
\n",
"
77627-6290
\n",
"
1323 S 27TH ST STE 700, NEDERLAND, TX, 77627-6290
\n",
"
1323 S 27TH ST STE 700
\n",
"
NEDERLAND, TX, 77627-6290
\n",
"
-93.987719
\n",
"
29.956197
\n",
"
S8HPNTSCZA
\n",
"
HIGH
\n",
"
1.0
\n",
"
(409) 729-5400
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
OFFICES AND CLINICS MEDICAL DOCTORS,NSK
\n",
"
8011.0
\n",
"
NaN
\n",
"
80110519
\n",
"
RADIOLOGIST
\n",
"
621111.0
\n",
"
10942519
\n",
"
DIVISION I. - SERVICES
\n",
"
HEALTH SERVICES
\n",
"
OFFICES AND CLINICS OF DOCTORS OF MEDICINE
\n",
"
OFFICES AND CLINICS OF MEDICAL DOCTORS
\n",
"
11.0
\n",
"
11.0
\n",
"
1995.0
\n",
"
1386157.0
\n",
"
1386157.0
\n",
"
20.0
\n",
"
G
\n",
"
12.0
\n",
"
1.0
\n",
"
0.0
\n",
"
SOUTHEAST TEXAS IMAGING LLP
\n",
"
1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762...
\n",
"
1323 S 27TH ST STE 700
\n",
"
NEDERLAND
\n",
"
TEXAS
\n",
"
USA
\n",
"
776276290.0
\n",
"
SOUTHEAST TEXAS IMAGING LLP
\n",
"
1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762...
\n",
"
1323 S 27TH ST STE 700
\n",
"
NEDERLAND
\n",
"
TEXAS
\n",
"
776276290.0
\n",
"
Y
\n",
"
SOUTHEAST TEXAS IMAGING LLP
\n",
"
1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762...
\n",
"
1323 S 27TH ST STE 700
\n",
"
NEDERLAND
\n",
"
TEXAS
\n",
"
USA
\n",
"
7.76276e+08
\n",
"
2.0
\n",
"
1.0
\n",
"
NaN
\n",
"
NaN
\n",
"
POINT (-93.98772 29.95620)
\n",
"
\n",
"
\n",
"
2
\n",
"
1129360394#-97.401965#27.776169
\n",
"
2020-08-01
\n",
"
CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI
\n",
"
NaN
\n",
"
1129360394
\n",
"
NaN
\n",
"
NaN
\n",
"
USA
\n",
"
NaN
\n",
"
CORPUS CHRISTI
\n",
"
NaN
\n",
"
TEXAS
\n",
"
TX
\n",
"
78404-3160
\n",
"
1521 S STAPLES ST STE 700, CORPUS CHRISTI, TX,...
\n",
"
1521 S STAPLES ST STE 700
\n",
"
CORPUS CHRISTI, TX, 78404-3160
\n",
"
-97.401965
\n",
"
27.776169
\n",
"
S8HPNTSCZA
\n",
"
HIGH
\n",
"
1.0
\n",
"
(361) 888-8271
\n",
"
NaN
\n",
"
NaN
\n",
"
WWW.HEARTOFCC.COM
\n",
"
NaN
\n",
"
OFFICES AND CLINICS MEDICAL DOCTORS,NSK
\n",
"
8011.0
\n",
"
NaN
\n",
"
80110101
\n",
"
CARDIOLOGIST AND CARDIO-VASCULAR SPECIALIST
\n",
"
621111.0
\n",
"
10942101
\n",
"
DIVISION I. - SERVICES
\n",
"
HEALTH SERVICES
\n",
"
OFFICES AND CLINICS OF DOCTORS OF MEDICINE
\n",
"
OFFICES AND CLINICS OF MEDICAL DOCTORS
\n",
"
100.0
\n",
"
100.0
\n",
"
1976.0
\n",
"
11077130.0
\n",
"
11077130.0
\n",
"
20.0
\n",
"
G
\n",
"
3.0
\n",
"
1.0
\n",
"
0.0
\n",
"
CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI
\n",
"
1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX...
\n",
"
1521 S STAPLES ST STE 700
\n",
"
CORPUS CHRISTI
\n",
"
TEXAS
\n",
"
USA
\n",
"
784043160.0
\n",
"
CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI
\n",
"
1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX...
\n",
"
1521 S STAPLES ST STE 700
\n",
"
CORPUS CHRISTI
\n",
"
TEXAS
\n",
"
784043160.0
\n",
"
Y
\n",
"
CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI
\n",
"
1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX...
\n",
"
1521 S STAPLES ST STE 700
\n",
"
CORPUS CHRISTI
\n",
"
TEXAS
\n",
"
USA
\n",
"
7.84043e+08
\n",
"
3.0
\n",
"
1.0
\n",
"
NaN
\n",
"
NaN
\n",
"
POINT (-97.40197 27.77617)
\n",
"
\n",
"
\n",
"
3
\n",
"
1119324698#-98.203039#26.254586
\n",
"
2020-08-01
\n",
"
VINCENT HONRUBIA, M.D., P.A.
\n",
"
NaN
\n",
"
1119324698
\n",
"
NaN
\n",
"
NaN
\n",
"
USA
\n",
"
NaN
\n",
"
EDINBURG
\n",
"
NaN
\n",
"
TEXAS
\n",
"
TX
\n",
"
78539-1406
\n",
"
2821 MICHAELANGELO DR STE 201, EDINBURG, TX, 7...
\n",
"
2821 MICHAELANGELO DR STE 201
\n",
"
EDINBURG, TX, 78539-1406
\n",
"
-98.203039
\n",
"
26.254586
\n",
"
S8HPNTSCZA
\n",
"
HIGH
\n",
"
1.0
\n",
"
(956) 421-4500
\n",
"
NaN
\n",
"
NaN
\n",
"
WWW.SOUTHTEXASSINUSINSTITUTE.COM
\n",
"
NaN
\n",
"
OFFICES AND CLINICS MEDICAL DOCTORS,NSK
\n",
"
8011.0
\n",
"
NaN
\n",
"
80119901
\n",
"
GENERAL AND FAMILY PRACTICE, PHYSICIAN/SURGEON
\n",
"
621111.0
\n",
"
10230302
\n",
"
DIVISION I. - SERVICES
\n",
"
HEALTH SERVICES
\n",
"
OFFICES AND CLINICS OF DOCTORS OF MEDICINE
\n",
"
OFFICES AND CLINICS OF MEDICAL DOCTORS
\n",
"
13.0
\n",
"
13.0
\n",
"
2008.0
\n",
"
1464692.0
\n",
"
1464692.0
\n",
"
20.0
\n",
"
G
\n",
"
0.0
\n",
"
0.0
\n",
"
0.0
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
N
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
0.0
\n",
"
0.0
\n",
"
NaN
\n",
"
NaN
\n",
"
POINT (-98.20304 26.25459)
\n",
"
\n",
"
\n",
"
4
\n",
"
1130039254#-95.282651#32.278874
\n",
"
2020-08-01
\n",
"
TINSLEY ASSOCIATES, L.L.P.
\n",
"
NaN
\n",
"
1130039254
\n",
"
EXCEPTIONAL HOME CARE
\n",
"
NaN
\n",
"
USA
\n",
"
NaN
\n",
"
TYLER
\n",
"
NaN
\n",
"
TEXAS
\n",
"
TX
\n",
"
75703-3978
\n",
"
1510 E GRANDE BLVD, TYLER, TX, 75703-3978
\n",
"
1510 E GRANDE BLVD
\n",
"
TYLER, TX, 75703-3978
\n",
"
-95.282651
\n",
"
32.278874
\n",
"
S8HPNTSCZA
\n",
"
HIGH
\n",
"
1.0
\n",
"
(903) 533-0290
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
NaN
\n",
"
OFFICES AND CLINICS MEDICAL DOCTORS,NSK
\n",
"
8011.0
\n",
"
8082.0
\n",
"
80110516
\n",
"
PEDIATRICIAN
\n",
"
621111.0
\n",
"
10942516
\n",
"
DIVISION I. - SERVICES
\n",
"
HEALTH SERVICES
\n",
"
OFFICES AND CLINICS OF DOCTORS OF MEDICINE
\n",
"
OFFICES AND CLINICS OF MEDICAL DOCTORS
\n",
"
23.0
\n",
"
200.0
\n",
"
2002.0
\n",
"
11307457.0
\n",
"
11307457.0
\n",
"
20.0
\n",
"
G
\n",
"
12.0
\n",
"
1.0
\n",
"
0.0
\n",
"
TINSLEY ASSOCIATES, L.L.P.
\n",
"
1510 E GRANDE BLVD, TYLER, TEXAS, 757033978, USA
\n",
"
1510 E GRANDE BLVD
\n",
"
TYLER
\n",
"
TEXAS
\n",
"
USA
\n",
"
757033978.0
\n",
"
TINSLEY ASSOCIATES, L.L.P.
\n",
"
1510 E GRANDE BLVD, TYLER, TEXAS, 757033978
\n",
"
1510 E GRANDE BLVD
\n",
"
TYLER
\n",
"
TEXAS
\n",
"
757033978.0
\n",
"
Y
\n",
"
TINSLEY ASSOCIATES, L.L.P.
\n",
"
1510 E GRANDE BLVD, TYLER, TEXAS, 757033978, USA
\n",
"
1510 E GRANDE BLVD
\n",
"
TYLER
\n",
"
TEXAS
\n",
"
USA
\n",
"
7.57034e+08
\n",
"
2.0
\n",
"
1.0
\n",
"
NaN
\n",
"
NaN
\n",
"
POINT (-95.28265 32.27887)
\n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" geoid do_date \\\n",
"0 1484649291#-95.391433#29.730764 2020-08-01 \n",
"1 1121837904#-93.987719#29.956197 2020-08-01 \n",
"2 1129360394#-97.401965#27.776169 2020-08-01 \n",
"3 1119324698#-98.203039#26.254586 2020-08-01 \n",
"4 1130039254#-95.282651#32.278874 2020-08-01 \n",
"\n",
" NAME BRANDNAME PB_ID \\\n",
"0 CHARLES GARCIA NaN 1484649291 \n",
"1 SOUTHEAST TEXAS IMAGING LLP NaN 1121837904 \n",
"2 CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI NaN 1129360394 \n",
"3 VINCENT HONRUBIA, M.D., P.A. NaN 1119324698 \n",
"4 TINSLEY ASSOCIATES, L.L.P. NaN 1130039254 \n",
"\n",
" TRADE_NAME FRANCHISE_NAME ISO3 AREANAME4 AREANAME3 \\\n",
"0 CHARLES GARCIA, MD PA NaN USA NaN HOUSTON \n",
"1 NaN NaN USA NaN NEDERLAND \n",
"2 NaN NaN USA NaN CORPUS CHRISTI \n",
"3 NaN NaN USA NaN EDINBURG \n",
"4 EXCEPTIONAL HOME CARE NaN USA NaN TYLER \n",
"\n",
" AREANAME2 AREANAME1 STABB POSTCODE \\\n",
"0 NaN TEXAS TX 77006-6122 \n",
"1 NaN TEXAS TX 77627-6290 \n",
"2 NaN TEXAS TX 78404-3160 \n",
"3 NaN TEXAS TX 78539-1406 \n",
"4 NaN TEXAS TX 75703-3978 \n",
"\n",
" FORMATTEDADDRESS \\\n",
"0 4704 MONTROSE BLVD, HOUSTON, TX, 77006-6122 \n",
"1 1323 S 27TH ST STE 700, NEDERLAND, TX, 77627-6290 \n",
"2 1521 S STAPLES ST STE 700, CORPUS CHRISTI, TX,... \n",
"3 2821 MICHAELANGELO DR STE 201, EDINBURG, TX, 7... \n",
"4 1510 E GRANDE BLVD, TYLER, TX, 75703-3978 \n",
"\n",
" MAINADDRESSLINE ADDRESSLASTLINE LONGITUDE \\\n",
"0 4704 MONTROSE BLVD HOUSTON, TX, 77006-6122 -95.391433 \n",
"1 1323 S 27TH ST STE 700 NEDERLAND, TX, 77627-6290 -93.987719 \n",
"2 1521 S STAPLES ST STE 700 CORPUS CHRISTI, TX, 78404-3160 -97.401965 \n",
"3 2821 MICHAELANGELO DR STE 201 EDINBURG, TX, 78539-1406 -98.203039 \n",
"4 1510 E GRANDE BLVD TYLER, TX, 75703-3978 -95.282651 \n",
"\n",
" LATITUDE GEORESULT CONFIDENCE_CODE COUNTRY_ACCESS_CODE TEL_NUM \\\n",
"0 29.730764 S8HPNTSCZA HIGH 1.0 (713) 333-0151 \n",
"1 29.956197 S8HPNTSCZA HIGH 1.0 (409) 729-5400 \n",
"2 27.776169 S8HPNTSCZA HIGH 1.0 (361) 888-8271 \n",
"3 26.254586 S8HPNTSCZA HIGH 1.0 (956) 421-4500 \n",
"4 32.278874 S8HPNTSCZA HIGH 1.0 (903) 533-0290 \n",
"\n",
" FAXNUM EMAIL HTTP OPEN_24H \\\n",
"0 NaN NaN WWW.CHARLESGARCIAMD.COM NaN \n",
"1 NaN NaN NaN NaN \n",
"2 NaN NaN WWW.HEARTOFCC.COM NaN \n",
"3 NaN NaN WWW.SOUTHTEXASSINUSINSTITUTE.COM NaN \n",
"4 NaN NaN NaN NaN \n",
"\n",
" BUSINESS_LINE SIC1 SIC2 SIC8 \\\n",
"0 OFFICES AND CLINICS MEDICAL DOCTORS,NSK 8011.0 NaN 80110513 \n",
"1 OFFICES AND CLINICS MEDICAL DOCTORS,NSK 8011.0 NaN 80110519 \n",
"2 OFFICES AND CLINICS MEDICAL DOCTORS,NSK 8011.0 NaN 80110101 \n",
"3 OFFICES AND CLINICS MEDICAL DOCTORS,NSK 8011.0 NaN 80119901 \n",
"4 OFFICES AND CLINICS MEDICAL DOCTORS,NSK 8011.0 8082.0 80110516 \n",
"\n",
" SIC8_DESCRIPTION ALT_INDUSTRY_CODE \\\n",
"0 OPTHALMOLOGIST 621111.0 \n",
"1 RADIOLOGIST 621111.0 \n",
"2 CARDIOLOGIST AND CARDIO-VASCULAR SPECIALIST 621111.0 \n",
"3 GENERAL AND FAMILY PRACTICE, PHYSICIAN/SURGEON 621111.0 \n",
"4 PEDIATRICIAN 621111.0 \n",
"\n",
" MICODE TRADE_DIVISION GROUP \\\n",
"0 10942513 DIVISION I. - SERVICES HEALTH SERVICES \n",
"1 10942519 DIVISION I. - SERVICES HEALTH SERVICES \n",
"2 10942101 DIVISION I. - SERVICES HEALTH SERVICES \n",
"3 10230302 DIVISION I. - SERVICES HEALTH SERVICES \n",
"4 10942516 DIVISION I. - SERVICES HEALTH SERVICES \n",
"\n",
" CLASS \\\n",
"0 OFFICES AND CLINICS OF DOCTORS OF MEDICINE \n",
"1 OFFICES AND CLINICS OF DOCTORS OF MEDICINE \n",
"2 OFFICES AND CLINICS OF DOCTORS OF MEDICINE \n",
"3 OFFICES AND CLINICS OF DOCTORS OF MEDICINE \n",
"4 OFFICES AND CLINICS OF DOCTORS OF MEDICINE \n",
"\n",
" SUB_CLASS EMPLOYEE_HERE EMPLOYEE_COUNT \\\n",
"0 OFFICES AND CLINICS OF MEDICAL DOCTORS 75.0 75.0 \n",
"1 OFFICES AND CLINICS OF MEDICAL DOCTORS 11.0 11.0 \n",
"2 OFFICES AND CLINICS OF MEDICAL DOCTORS 100.0 100.0 \n",
"3 OFFICES AND CLINICS OF MEDICAL DOCTORS 13.0 13.0 \n",
"4 OFFICES AND CLINICS OF MEDICAL DOCTORS 23.0 200.0 \n",
"\n",
" YEAR_START SALES_VOLUME_LOCAL SALES_VOLUME_US_DOLLARS CURRENCY_CODE \\\n",
"0 2019.0 515248.0 515248.0 20.0 \n",
"1 1995.0 1386157.0 1386157.0 20.0 \n",
"2 1976.0 11077130.0 11077130.0 20.0 \n",
"3 2008.0 1464692.0 1464692.0 20.0 \n",
"4 2002.0 11307457.0 11307457.0 20.0 \n",
"\n",
" AGENT_CODE LEGAL_STATUS_CODE STATUS_CODE SUBSIDIARY_INDICATOR \\\n",
"0 G 13.0 0.0 0.0 \n",
"1 G 12.0 1.0 0.0 \n",
"2 G 3.0 1.0 0.0 \n",
"3 G 0.0 0.0 0.0 \n",
"4 G 12.0 1.0 0.0 \n",
"\n",
" PARENT_BUSINESS_NAME \\\n",
"0 NaN \n",
"1 SOUTHEAST TEXAS IMAGING LLP \n",
"2 CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI \n",
"3 NaN \n",
"4 TINSLEY ASSOCIATES, L.L.P. \n",
"\n",
" PARENT_ADDRESS \\\n",
"0 NaN \n",
"1 1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762... \n",
"2 1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX... \n",
"3 NaN \n",
"4 1510 E GRANDE BLVD, TYLER, TEXAS, 757033978, USA \n",
"\n",
" PARENT_STREET_ADDRESS PARENT_AREANAME3 PARENT_AREANAME1 PARENT_COUNTRY \\\n",
"0 NaN NaN NaN NaN \n",
"1 1323 S 27TH ST STE 700 NEDERLAND TEXAS USA \n",
"2 1521 S STAPLES ST STE 700 CORPUS CHRISTI TEXAS USA \n",
"3 NaN NaN NaN NaN \n",
"4 1510 E GRANDE BLVD TYLER TEXAS USA \n",
"\n",
" PARENT_POSTCODE DOMESTIC_ULTIMATE_BUSINESS_NAME \\\n",
"0 NaN NaN \n",
"1 776276290.0 SOUTHEAST TEXAS IMAGING LLP \n",
"2 784043160.0 CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI \n",
"3 NaN NaN \n",
"4 757033978.0 TINSLEY ASSOCIATES, L.L.P. \n",
"\n",
" DOMESTIC_ULTIMATE_ADDRESS \\\n",
"0 NaN \n",
"1 1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762... \n",
"2 1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX... \n",
"3 NaN \n",
"4 1510 E GRANDE BLVD, TYLER, TEXAS, 757033978 \n",
"\n",
" DOMESTIC_ULTIMATE_STREET_ADDRESS DOMESTIC_ULTIMATE_AREANAME3 \\\n",
"0 NaN NaN \n",
"1 1323 S 27TH ST STE 700 NEDERLAND \n",
"2 1521 S STAPLES ST STE 700 CORPUS CHRISTI \n",
"3 NaN NaN \n",
"4 1510 E GRANDE BLVD TYLER \n",
"\n",
" DOMESTIC_ULTIMATE_AREANAME1 DOMESTIC_ULTIMATE_POSTCODE \\\n",
"0 NaN NaN \n",
"1 TEXAS 776276290.0 \n",
"2 TEXAS 784043160.0 \n",
"3 NaN NaN \n",
"4 TEXAS 757033978.0 \n",
"\n",
" GLOBAL_ULTIMATE_INDICATOR GLOBAL_ULTIMATE_BUSINESS_NAME \\\n",
"0 N NaN \n",
"1 Y SOUTHEAST TEXAS IMAGING LLP \n",
"2 Y CARDIOLOGY ASSOCIATES OF CORPUS CHRISTI \n",
"3 N NaN \n",
"4 Y TINSLEY ASSOCIATES, L.L.P. \n",
"\n",
" GLOBAL_ULTIMATE_ADDRESS \\\n",
"0 NaN \n",
"1 1323 S 27TH ST STE 700, NEDERLAND, TEXAS, 7762... \n",
"2 1521 S STAPLES ST STE 700, CORPUS CHRISTI, TEX... \n",
"3 NaN \n",
"4 1510 E GRANDE BLVD, TYLER, TEXAS, 757033978, USA \n",
"\n",
" GLOBAL_ULTIMATE_STREET_ADDRESS GLOBAL_ULTIMATE_AREANAME3 \\\n",
"0 NaN NaN \n",
"1 1323 S 27TH ST STE 700 NEDERLAND \n",
"2 1521 S STAPLES ST STE 700 CORPUS CHRISTI \n",
"3 NaN NaN \n",
"4 1510 E GRANDE BLVD TYLER \n",
"\n",
" GLOBAL_ULTIMATE_AREANAME1 GLOBAL_ULTIMATE_COUNTRY GLOBAL_ULTIMATE_POSTCODE \\\n",
"0 NaN NaN NaN \n",
"1 TEXAS USA 7.76276e+08 \n",
"2 TEXAS USA 7.84043e+08 \n",
"3 NaN NaN NaN \n",
"4 TEXAS USA 7.57034e+08 \n",
"\n",
" FAMILY_MEMBERS HIERARCHY_CODE TICKER_SYMBOL EXCHANGE_NAME \\\n",
"0 0.0 0.0 NaN NaN \n",
"1 2.0 1.0 NaN NaN \n",
"2 3.0 1.0 NaN NaN \n",
"3 0.0 0.0 NaN NaN \n",
"4 2.0 1.0 NaN NaN \n",
"\n",
" geom \n",
"0 POINT (-95.39143 29.73076) \n",
"1 POINT (-93.98772 29.95620) \n",
"2 POINT (-97.40197 27.77617) \n",
"3 POINT (-98.20304 26.25459) \n",
"4 POINT (-95.28265 32.27887) "
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sql_query = \"\"\"\n",
" SELECT * except(do_label) FROM $dataset$ \n",
" WHERE SUB_CLASS = 'OFFICES AND CLINICS OF MEDICAL DOCTORS' \n",
" AND STABB = 'TX'\n",
" AND CAST(do_date AS date) >= (SELECT MAX(CAST(do_date AS date)) from $dataset$)\n",
"\"\"\"\n",
"pois = poi_dataset.to_dataframe(sql_query=sql_query)\n",
"pois.columns = list(map(str.lower, pois.columns))\n",
"pois['geom'] = list(map(wkt.loads, pois['geom']))\n",
"pois = gpd.GeoDataFrame(pois, geometry='geom', crs='epsg:4326')\n",
"pois.head()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(59554, 74)"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pois.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1.3 Visualize data"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Map([Layer(tx_boundary,\n",
" style=basic_style(opacity=0, stroke_color='#11A579', stroke_width=5),\n",
" legends=basic_legend('Texas Boundary')),\n",
" Layer(pois, \n",
" style=basic_style(color='#F2B701', size=2, opacity=0.9, stroke_width=0),\n",
" popup_hover=[popup_element('name', 'Client'),\n",
" popup_element('employee_here', '# Employees')],\n",
" legends=basic_legend('Client Locations'))], \n",
" basemap=basemaps.darkmatter)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Discretize space. H3 grid\n",
"\n",
"A fundamental step in territory management is to discretize space. Territory management algorithms are computationally complex and hence it is crucial to leverage the spatial component to reduce complexity. We can do this by working at an aggregated level instead of considering each client location independently.\n",
"\n",
"We first need to identify the smallest spatial aggregation that makes sense for your business, our **geographic support**. This can be census block groups, zip codes or counties, or you can be interested in using a standard grid, in which case it would ideally be a hierarchical spatial index such as [Quadkey grid](https://docs.microsoft.com/en-us/azure/azure-maps/zoom-levels-and-tile-grid?tabs=csharp) or [H3 grid](https://eng.uber.com/h3/).\n",
"\n",
"In this notebook we will use an H3 grid of resolution 4. We can easily discretize space by performing a polyfill of the Texas boundary polygon. \n",
"\n",
"*Note* a buffer has been applied because H3 will fill the polygon with all hexagons of resolution 4 whose centroid lies within the polygon to be filled and we want to make sure the whole territory is covered."
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
" hex_id geometry \\\n",
"0 84489edffffffff POLYGON ((-97.03425 29.34525, -96.79420 29.479... \n",
"1 8426d31ffffffff POLYGON ((-102.58398 34.37842, -102.33428 34.5... \n",
"2 8448f29ffffffff POLYGON ((-104.42876 30.05621, -104.18949 30.2... \n",
"3 84446ddffffffff POLYGON ((-95.82009 29.75644, -95.57955 29.887... \n",
"4 84489c5ffffffff POLYGON ((-98.02512 29.30940, -97.78501 29.445... \n",
"\n",
" poi_count employee_avg \n",
"0 15 4.250000 \n",
"1 1 2.000000 \n",
"2 0 0.000000 \n",
"3 1108 4.801538 \n",
"4 496 4.854093 "
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pois_g = gpd.sjoin(pois, grid, how='right').groupby('hex_id').agg({'geoid':'count', 'employee_here':'mean'}).\\\n",
" reset_index().rename(columns={'geoid':'poi_count', 'employee_here':'employee_avg'})\n",
"pois_g[['poi_count', 'employee_avg']] = pois_g[['poi_count', 'employee_avg']].fillna(0)\n",
"areas = grid.merge(pois_g, on='hex_id')\n",
"areas = gpd.GeoDataFrame(areas, crs='epsg:4326')\n",
"areas.head()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 402.000000\n",
"mean 148.144279\n",
"std 709.392374\n",
"min 0.000000\n",
"25% 0.000000\n",
"50% 4.000000\n",
"75% 40.500000\n",
"95% 496.950000\n",
"max 8268.000000\n",
"Name: poi_count, dtype: float64"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"areas['poi_count'].describe(percentiles=[0.25, 0.5, 0.75, 0.95])"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"breaks=[1, 4, 40, 500]\n",
"\n",
"Map(Layer(areas, \n",
" style=color_bins_style('poi_count', breaks=breaks),\n",
" legends=color_bins_legend('Number of Clients')))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Territory Optimization\n",
"\n",
"Once we have our data aggregated, it's time to start working on building balanced territories.\n",
"\n",
"We will explore two different techniques:\n",
"- [KMeans clustering](https://en.wikipedia.org/wiki/K-means_clustering).\n",
"- [Max-p spatial clustering algorithm](https://region.readthedocs.io/en/latest/users/max-p-regions/). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.1 Approach 1. KMeans clustering\n",
"\n",
"This is a very well known and broadly used technique. However, this technique doesn't allow you to incorporate balancing criteria and usually generates low quality results for territory management."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.1.1. Calculate clusters"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done!\n"
]
}
],
"source": [
"no_territories = 5\n",
"kmeans = KMeans(no_territories, random_state=111)\n",
"\n",
"kmeans.fit_predict(list(map(lambda point:[point.x, point.y], areas.to_crs('epsg:26914').centroid)), \n",
" sample_weight=areas['poi_count'])\n",
"print('Done!')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.1.2. Label grid cells\n",
"\n",
"We assign each cell to the cluster it belongs to.\n",
"\n",
"In order to have comparable results with other techniques, we identify 5 representative cells, each of which being far enough from each other to make sure they are in different clusters. This way, we can compare how each cluster changes."
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"areas['kmeans_cluster'] = kmeans.labels_\n",
"areas['kmeans_cluster'] += 1"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"# This dictionary contains the representative cells with the cluster they represent.\n",
"trans_dict={'8448c69ffffffff':1,\n",
" '8426d47ffffffff':2,\n",
" '8426cdbffffffff':3,\n",
" '84446edffffffff':4,\n",
" '844880dffffffff':5}"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"areas['kmeans_cluster_aux'] = -1\n",
"for hex_id in trans_dict:\n",
" areas.loc[areas['kmeans_cluster'] == areas.loc[areas['hex_id'] == hex_id, 'kmeans_cluster'].iloc[0], 'kmeans_cluster_aux'] = trans_dict[hex_id]\n",
"areas['kmeans_cluster'] = areas['kmeans_cluster_aux']\n",
"areas.drop(columns='kmeans_cluster_aux', inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We create a string variable with a more descriptive name for our visualization"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"areas['kmeans_cluster_cat'] = list(map(lambda v:f'Cluster_{v}', areas['kmeans_cluster']))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.1.3. Visualize and analyze results"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"KMeans calculates nice compact clusters (see map). However, we get very unbalanced clusters with Cluster_3 having 10 times more clients than Cluster_1, as can be seen in the chart below."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Map(Layer(areas, \n",
" style=color_category_style('kmeans_cluster_cat', cat=sorted(areas['kmeans_cluster_cat'].unique())),\n",
" legends=color_category_legend('KMeans Clustering')))"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1gAAAEYCAYAAABBWFftAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3de1xUdeL/8TcXQRHLKBQoL22UDx65iJnpKr/MC15IRDHNS0TeS9O+uVqhqKjkpTTTrFitDCu/upGFbKYRbVaUZmXmCpq3TFQwRdEBdWDm/P5wm2+Ul1HPOA6+nn8xZ875nPcMn4benst4GYZhCAAAAABw2bzdHQAAAAAAqgsKFgAAAACYhIIFAAAAACahYAEAAACASShYAAAAAGASX3cHqC6+//571apVy90xAIfTp0/L39/f3TGAKpiXuNowJ3G1YU56jtOnTysqKupPyylYJvHy8lJERIS7YwAOBQUFzElcdZiXuNowJ3G1YU56joKCgrMu5xRBAAAAADAJBQsAAAAATELBAgAAAACTULBM4ufn5+4Il63CWunuCAAAAIBH4yYXJvH29lZylzR3x7gsM9emuDsCAAAA4NE4ggUAAAAAJqFgAQAAAIBJKFgAAAAAYBIKFgA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9V4sWLTRv3jwlJiaqTZs2SkpKUqNGjaqsU15ero0bN6q0tFTz5893LNu2bZsiIyPl6+urqKioC+5r4sSJSkxM1B133KH//Oc/l5UbAAAAwLXlggUrJydHFRUVio2N1bFjxzRx4kQVFBSoQ4cOeuaZZ+Tre/4hIiMjtXv3blkslipHsYqLizVp0iQFBAScdTur1er4uUGDBsrJydGGDRu0fv16DRo0SCkpKeratatjHbvdLsMwtHz5ctWqVUuSVFJSIn9/fx09elR+fn4XzFpUVKRvv/1We/bs0ZtvvqnS0lKdOHFCw4YN0+LFiy/0VgEAAAC4xp33FMHMzEylpKSovLxckjR9+nQVFRVp8uTJ2rNnj1555ZUL7qB+/fqKi4vThAkTZLFYJEkWi0WpqamqW7euatas6Vg3KChIW7ZskXSm2P1m2bJlSk5OVnR0tMaPH6/o6Gjt2LFDkuTj46PKykoFBgYqKipKS5YskSQdP35c/fv3V25urtNvRkhIiL788ktlZWUpKytLY8aM0d133025AgAAAOCU8xast99+WwsXLtQDDzygkydP6uOPP9a4ceN03333afLkycrKynJqJ1OmTFF4eLj69eun+Ph49enTR+Hh4UpLS6uyXkpKiqZNm6ZevXopPz9fwcHBkqSePXvKZrMpNjZWCQkJslgsevjhhyVJMTExGjBggH766SfNmTNHmzdvVlxcnPr06aPu3burR48el/K+AAAAAMBF8zIMwzjXky1atNB3330nSdqwYYOGDRumb7/9Vn5+fpKk5s2ba9OmTVcm6VWuoKBA9h+7uTsGAADAVevOB392d4SrXkFBgSIiItwdA0441+/qvBcl+fj4yGq1ys/PT998842aNWvmKFclJSWOa508icVi0cCBA8/6XO3atbVs2bIrnAgAAABAdXHegnX33XfrjTfeUPfu3bVq1SolJiY6nktPT1fLli1dHtBsgYGBTp/aCAAAAAAX47wF66mnntLQoUM1f/583XPPPerXr58kqVOnTiovL+doDwAAAAD8znkLVuPGjZWTk6OjR48qKCjIsXzs2LFq06aN6tat6/KAAAAAAOApLvg9WF5eXlXKlSTFxsa6LBAAAAAAeKrz3qYdAAAAA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2xQXF591vN+2Ac7lbPPSZrOpbdu2euqpp3Tq1CkNHz5cgYGBuv/++y/ps/Fc8xz4o1tuucXx+WYYhmbOnKkOHTro0KFDio6Odqx3uX+vmZNXL45gXQa73X7W5b//nwnALA0aNNDixYv1l7/8RV5eXhoyZIh++eWXs85Db2/vc85PHx8f5i5MdbYjnxeag+fa5nzjARejb9++SklJkZ+fn6677joNGjRIn3zyySV/NjIvcbHKy8v1xBNP6JdfflFaWtolfVYyJz0Tv4XLEBoaql9//dXxuLi4WNdff70CAgLcmArV1bZt2/TBBx9UWWYYhsLCwv40D0NCQnTjjTdKkkpLS6s8V79+feYuTHW2+RQSEqKwsDAdPny4yv8E/PZcaGioDh069KflZxvv0KFDVf4FF3DGBx98oG3btjkeG4YhX1/fS/5sPNc8B87mwIED6tevn3x8fLR06VJdd91155xDzMnqh4J1GaKjo7V582b9/PPPkqTly5erY8eO7g2Fasvb21vPPvus9u3bJ0latmyZmjRpoo4dO2rVqlUqLy+X1WrVypUr1alTJ/n6+uq+++7TihUrJJ0paLt27VKrVq2YuzBVx44d9d5776myslLHjx/Xhx9+qE6dOikkJEQNGzbU6tWrJUlffPGFvL29dccdd6hDhw769NNPdeTIERmGoRUrVqhTp06O8X6bt0VFRfriiy/Uvn17t70+eKYdO3ZowYIFstlsOnXqlN555x3FxsZe8mfjueY58EfHjh3TQw89pM6dO2vevHmqWbOmJJn+95o5efXyMriq/bKsW7dOc+fOVUVFhRo2bKjZs2erbt267o6FaiorK0uLFy+WzWZTSEiInn32WYWFhSk9PV3Z2dmqqKhQx44d9dRTT8nLy0uHDx9WSkqKCgsL5eXlpaefftpx/jdzF5fjmWee0e23364hQ4aosrJSs2fP1ldffaWKigo9+OCDGjJkiKQzt2mfNGmSjh49Kj8/P02fPl133nmnJOm9997TkiVLVFFRoWbNmmn69Ony9/dXWVmZUlNTlZ+fL5vNpscee0zx8fHufLnwEL+flydPntS0adO0efNmVVZWqmvXrnryyScv+bPxfPMc+L1XX31VCxYs0B133FFl+ZtvvqkVK1aY9veaOXn1omABAAAAgEk4RRAAAAAATELBAgAAAACTULAAAAAAwCQULAAAAAAwCQULAAAAAExCwQIAwAP89h14AICrGwULAID/Gjp0qOPLPq8m+fn56t+/v7tjAACc4OvuAAAAXC1ee+01d0c4qxMnTqiiosLdMQAATuAIFgCg2iksLFTz5s318ssvq2XLloqOjlZGRoYkae/evRoxYoRatmypjh07avHixTIMQ5KUmJiot99++4Ljnz59WmlpaWrdurVatWql5ORknT59WpKUl5enhIQE3XXXXYqPj9e6desc2zVp0kQ//fST4/GYMWP00ksvOfY9b948xcfHq3nz5nrooYdUWFioI0eOaNiwYTp27JiaN2+uo0ePmvY+AQDMR8ECAFRL5eXl2r59u9atW6f09HQtXLhQubm5GjRokG677Tbl5eVp0aJFWrFihZYvX35RY7/00kv64YcflJWVpdzcXO3fv18vv/yyduzYoccee0yPPvqovvnmG40dO1ZPPPGEtm/f7tS4H374oRYuXKjPP/9chmFo0aJFuvHGG7V48WLVrVtXmzZt0g033HApbwcA4AqhYAEAqq2JEycqICBATZs2Vc+ePTVt2jSdOHFCY8eOlZ+fn2677TYNHTpU77///kWN++GHH+rRRx9V/fr1FRgYqOeee04PPPCAPvzwQ7Vp00adO3eWr6+v2rVrpw4dOig7O9upcXv06KEGDRqoTp06iomJ0c8//3wJrxoA4E5cgwUAqJb8/f1Vv359x+OQkBAdOXJEjRs3lq/v//35CwsLU1FR0UWNffjwYYWEhFQZW5JKSkoUFhZWZd2LGT8oKMjxs6+vr+PURQCA5+AIFgCgWjp9+rRKS0sdjw8cOKDmzZuruLhYlZWVjuWFhYW66aabLmrs+vXrq7i42PF4y5YteuuttxQaGqr9+/dXWff343t7e1e5WQXXUwFA9UPBAgBUW3PnzpXVatWPP/6orKwsjR49WjfddJNeeOEFWa1W7dq1S6+//rri4uIuaty4uDgtWrRIhw8f1okTJzR37lwdPnxYsbGx2rBhgz7++GPZbDatW7dOn376qWJjYyVJjRs3Vm5urgzDUF5enn744Qen9ufn5yer1Sqr1XrR7wEA4MriFEEAQLVVu3Zt3XfffapZs6YmTpyoe+65R+np6UpLS1Pbtm1Vs2ZNDRgwQElJSRc17mOPPaaTJ0+qZ8+eqqysVNeuXTVq1Cj5+fnp5Zdf1pw5c/T000/r5ptv1ty5cxUZGSlJmjRpkmbOnKk333xTrVq1Uvfu3Z3aX5MmTRQeHq5WrVrpgw8+UKNGjS76vQAAXBleBid4AwCqmcLCQnXs2FHff/+9ateu7e44AIBrCKcIAgAAAIBJOEUQAIA/aNu2rcrLy8/6XFxcnKZNm3aFEwEAPAWnCAIAAACASThFEAAAAABMQsECAAAAAJNQsAAAAADAJBQsAAAAADAJBQsAAAAATPL/AX9lv5z7l3+RAAAAAElFTkSuQmCC\n",
"text/plain": [
"
"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plot_clinic_balance(sorted(areas['kmeans_cluster_cat'].unique()), areas, 'kmeans_cluster_cat', poi_count='sum')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3.2 Approach 2. Max-p\n",
"\n",
"Let's now try to balance the number of clients per cluster while maintaining connected clusters as compact as possible.\n",
"\n",
"We will use [Pysal's implementation of the Max-p algorithm](https://github.com/pysal/spopt). Max-p is a spatial clustering algorithm that calculates spatially connected clusters, with similar properties, while balancing one criterion, or mixed or criteria."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.2.1. Weights. Adjacency matrix\n",
"\n",
"The first thing we need to do is calculate the adjacency matrix which will tell the algorithm which cells are contiguous.\n",
"\n",
"We will use [Rook weights](https://pysal.org/libpysal/generated/libpysal.weights.Rook.html) which considers two polygons to be contiguous if they share one edge."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"wgt = Rook.from_dataframe(areas, geom_col='geometry')"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(2, 6), (3, 27), (4, 34), (5, 33), (6, 302)]"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wgt.histogram"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.2.2 Balancing criteria\n",
"\n",
"We would like to balance clusters based on total number of clients. Normally we are not looking for a perfect balance, especially when dealing with more than one multiple criteria, and a balance tolerance is introduced. In our case, we will consider a tolerance of 20%, which means that we allow clusters to be as much as 20% below the perfect balance.\n",
"\n",
"*Note* we are only using the number of clients to balance, but this dataset also has the number of employees per client and you might even have other data you might be interested in using. The good news is Max-p allows you to do that."
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"# Trick to help the algorithm find compact areas\n",
"areas['poi_count'] += 10"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Minimum number of clients per cluster 10171\n"
]
}
],
"source": [
"balance_tolerance = 0.2 # 5%\n",
"perfect_balance = areas['poi_count'].sum()/5\n",
"threshold = int(np.floor(perfect_balance * (1-balance_tolerance)))\n",
"print('Minimum number of clients per cluster', threshold)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 3.2.3. Similarity criteria\n",
"\n",
"Max-p also allows you to set similarity criteria. These are variables that you want to have a similar behavior **within clusters**. For example, you might be interested in having clusters with similar demographic or socioeconomic characteristics.\n",
"\n",
"In this case, we don't have any specific criteria, so we will use the gris cell centroid coordinates as similarity criteria in order to get clusters as compact as possible. You can try removing these or only adding one of the coordinates to clearly see whats the effect of these similarity criteria."
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"