1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
# mapclassify: Classification Schemes for Choropleth Maps
[](https://github.com/pysal/mapclassify/actions/workflows/testing.yml)
[](https://codecov.io/gh/pysal/mapclassify)
[](https://badge.fury.io/py/mapclassify)
[](https://zenodo.org/badge/latestdoi/88918063)
[](https://opensource.org/licenses/BSD-3-Clause)
[](https://github.com/astral-sh/ruff)
[](https://mybinder.org/v2/gh/pysal/mapclassify/main)
`mapclassify` implements a family of classification schemes for choropleth maps.
Its focus is on the determination of the number of classes, and the assignment
of observations to those classes. It is intended for use with upstream mapping
and geovisualization packages (see
[geopandas](https://geopandas.org/mapping.html))
that handle the rendering of the maps.
For further theoretical background see [Rey, S.J., D. Arribas-Bel, and L.J. Wolf (2020) "Geographic Data Science with PySAL and the PyData Stackā](https://geographicdata.science/book/notebooks/05_choropleth.html).
## Using `mapclassify`
Load built-in example data reporting employment density in 58 California counties:
```python
>>> import mapclassify
>>> y = mapclassify.load_example()
>>> y.mean()
125.92810344827588
>>> y.min(), y.max()
(0.13, 4111.4499999999998)
```
## Map Classifiers Supported
### BoxPlot
```python
>>> mapclassify.BoxPlot(y)
BoxPlot
Interval Count
--------------------------
( -inf, -52.88] | 0
( -52.88, 2.57] | 15
( 2.57, 9.36] | 14
( 9.36, 39.53] | 14
( 39.53, 94.97] | 6
( 94.97, 4111.45] | 9
```
### EqualInterval
```python
>>> mapclassify.EqualInterval(y)
EqualInterval
Interval Count
--------------------------
[ 0.13, 822.39] | 57
( 822.39, 1644.66] | 0
(1644.66, 2466.92] | 0
(2466.92, 3289.19] | 0
(3289.19, 4111.45] | 1
```
### FisherJenks
```python
>>> import numpy as np
>>> np.random.seed(123456)
>>> mapclassify.FisherJenks(y, k=5)
FisherJenks
Interval Count
--------------------------
[ 0.13, 75.29] | 49
( 75.29, 192.05] | 3
( 192.05, 370.50] | 4
( 370.50, 722.85] | 1
( 722.85, 4111.45] | 1
```
### FisherJenksSampled
```python
>>> np.random.seed(123456)
>>> x = np.random.exponential(size=(10000,))
>>> mapclassify.FisherJenks(x, k=5)
FisherJenks
Interval Count
----------------------
[ 0.00, 0.64] | 4694
( 0.64, 1.45] | 2922
( 1.45, 2.53] | 1584
( 2.53, 4.14] | 636
( 4.14, 10.61] | 164
>>> mapclassify.FisherJenksSampled(x, k=5)
FisherJenksSampled
Interval Count
----------------------
[ 0.00, 0.70] | 5020
( 0.70, 1.63] | 2952
( 1.63, 2.88] | 1454
( 2.88, 5.32] | 522
( 5.32, 10.61] | 52
```
### HeadTailBreaks
```python
>>> mapclassify.HeadTailBreaks(y)
HeadTailBreaks
Interval Count
--------------------------
[ 0.13, 125.93] | 50
( 125.93, 811.26] | 7
( 811.26, 4111.45] | 1
```
### JenksCaspall
```python
>>> mapclassify.JenksCaspall(y, k=5)
JenksCaspall
Interval Count
--------------------------
[ 0.13, 1.81] | 14
( 1.81, 7.60] | 13
( 7.60, 29.82] | 14
( 29.82, 181.27] | 10
( 181.27, 4111.45] | 7
```
### JenksCaspallForced
```python
>>> mapclassify.JenksCaspallForced(y, k=5)
JenksCaspallForced
Interval Count
--------------------------
[ 0.13, 1.34] | 12
( 1.34, 5.90] | 12
( 5.90, 16.70] | 13
( 16.70, 50.65] | 9
( 50.65, 4111.45] | 12
```
### JenksCaspallSampled
```python
>>> mapclassify.JenksCaspallSampled(y, k=5)
JenksCaspallSampled
Interval Count
--------------------------
[ 0.13, 12.02] | 33
( 12.02, 29.82] | 8
( 29.82, 75.29] | 8
( 75.29, 192.05] | 3
( 192.05, 4111.45] | 6
```
### MaxP
```python
>>> mapclassify.MaxP(y)
MaxP
Interval Count
--------------------------
[ 0.13, 8.70] | 29
( 8.70, 16.70] | 8
( 16.70, 20.47] | 1
( 20.47, 66.26] | 10
( 66.26, 4111.45] | 10
```
### [MaximumBreaks](notebooks/maximum_breaks.ipynb)
```python
>>> mapclassify.MaximumBreaks(y, k=5)
MaximumBreaks
Interval Count
--------------------------
[ 0.13, 146.00] | 50
( 146.00, 228.49] | 2
( 228.49, 546.67] | 4
( 546.67, 2417.15] | 1
(2417.15, 4111.45] | 1
```
### NaturalBreaks
```python
>>> mapclassify.NaturalBreaks(y, k=5)
NaturalBreaks
Interval Count
--------------------------
[ 0.13, 75.29] | 49
( 75.29, 192.05] | 3
( 192.05, 370.50] | 4
( 370.50, 722.85] | 1
( 722.85, 4111.45] | 1
```
### Quantiles
```python
>>> mapclassify.Quantiles(y, k=5)
Quantiles
Interval Count
--------------------------
[ 0.13, 1.46] | 12
( 1.46, 5.80] | 11
( 5.80, 13.28] | 12
( 13.28, 54.62] | 11
( 54.62, 4111.45] | 12
```
### Percentiles
```python
>>> mapclassify.Percentiles(y, pct=[33, 66, 100])
Percentiles
Interval Count
--------------------------
[ 0.13, 3.36] | 19
( 3.36, 22.86] | 19
( 22.86, 4111.45] | 20
```
### PrettyBreaks
```python
>>> np.random.seed(123456)
>>> x = np.random.randint(0, 10000, (100,1))
>>> mapclassify.PrettyBreaks(x)
Pretty
Interval Count
----------------------------
[ 300.00, 2000.00] | 23
( 2000.00, 4000.00] | 15
( 4000.00, 6000.00] | 18
( 6000.00, 8000.00] | 24
( 8000.00, 10000.00] | 20
```
### StdMean
```python
>>> mapclassify.StdMean(y)
StdMean
Interval Count
--------------------------
( -inf, -967.36] | 0
(-967.36, -420.72] | 0
(-420.72, 672.57] | 56
( 672.57, 1219.22] | 1
(1219.22, 4111.45] | 1
```
### UserDefined
```python
>>> mapclassify.UserDefined(y, bins=[22, 674, 4112])
UserDefined
Interval Count
--------------------------
[ 0.13, 22.00] | 38
( 22.00, 674.00] | 18
( 674.00, 4112.00] | 2
```
## Alternative API
As of version 2.4.0 the API has been extended. A `classify` function is now
available for a streamlined interface:
```python
>>> classify(y, 'boxplot')
BoxPlot
Interval Count
--------------------------
( -inf, -52.88] | 0
( -52.88, 2.57] | 15
( 2.57, 9.36] | 14
( 9.36, 39.53] | 14
( 39.53, 94.97] | 6
( 94.97, 4111.45] | 9
```
## Use Cases
### Creating and using a classification instance
```python
>>> bp = mapclassify.BoxPlot(y)
>>> bp
BoxPlot
Interval Count
--------------------------
( -inf, -52.88] | 0
( -52.88, 2.57] | 15
( 2.57, 9.36] | 14
( 9.36, 39.53] | 14
( 39.53, 94.97] | 6
( 94.97, 4111.45] | 9
>>> bp.bins
array([ -5.28762500e+01, 2.56750000e+00, 9.36500000e+00,
3.95300000e+01, 9.49737500e+01, 4.11145000e+03])
>>> bp.counts
array([ 0, 15, 14, 14, 6, 9])
>>> bp.yb
array([5, 1, 2, 3, 2, 1, 5, 1, 3, 3, 1, 2, 2, 1, 2, 2, 2, 1, 5, 2, 4, 1, 2,
2, 1, 1, 3, 3, 3, 5, 3, 1, 3, 5, 2, 3, 5, 5, 4, 3, 5, 3, 5, 4, 2, 1,
1, 4, 4, 3, 3, 1, 1, 2, 1, 4, 3, 2])
```
### Binning new data
```python
>>> bp = mapclassify.BoxPlot(y)
>>> bp
BoxPlot
Interval Count
--------------------------
( -inf, -52.88] | 0
( -52.88, 2.57] | 15
( 2.57, 9.36] | 14
( 9.36, 39.53] | 14
( 39.53, 94.97] | 6
( 94.97, 4111.45] | 9
>>> bp.find_bin([0, 7, 3000, 48])
array([1, 2, 5, 4])
```
Note that `find_bin` does not recalibrate the classifier:
```python
>>> bp
BoxPlot
Interval Count
--------------------------
( -inf, -52.88] | 0
( -52.88, 2.57] | 15
( 2.57, 9.36] | 14
( 9.36, 39.53] | 14
( 39.53, 94.97] | 6
( 94.97, 4111.45] | 9
```
### Apply
```python
>>> import mapclassify
>>> import pandas
>>> from numpy import linspace as lsp
>>> data = [lsp(3,8,num=10), lsp(10, 0, num=10), lsp(-5, 15, num=10)]
>>> data = pandas.DataFrame(data).T
>>> data
0 1 2
0 3.000000 10.000000 -5.000000
1 3.555556 8.888889 -2.777778
2 4.111111 7.777778 -0.555556
3 4.666667 6.666667 1.666667
4 5.222222 5.555556 3.888889
5 5.777778 4.444444 6.111111
6 6.333333 3.333333 8.333333
7 6.888889 2.222222 10.555556
8 7.444444 1.111111 12.777778
9 8.000000 0.000000 15.000000
>>> data.apply(mapclassify.Quantiles.make(rolling=True))
0 1 2
0 0 4 0
1 0 4 0
2 1 4 0
3 1 3 0
4 2 2 1
5 2 1 2
6 3 0 4
7 3 0 4
8 4 0 4
9 4 0 4
```
## Development Notes
Because we use `geopandas` in development, and geopandas has stable `mapclassify` as a dependency, setting up a local development installation involves creating a conda environment, then replacing the stable `mapclassify` with the development version of `mapclassify` in the development environment. This can be accomplished with the following steps:
```
conda-env create -f environment.yml
conda activate mapclassify
conda remove -n mapclassify mapclassify
pip install -e .
```
|