Compute Natural Breaks (Fisher-Jenks algorithm)
Project description
Compute “natural breaks” (Fisher-Jenks algorithm) on list / tuple / array / numpy.ndarray of integers/floats.
The algorithm implemented by this library is also sometimes referred to as Fisher-Jenks algorithm, Jenks Optimisation Method or Fisher exact optimization method. This is a deterministic method to calculate the optimal class boundaries.
Intended compatibility: CPython 3.6+
Wheels are provided via PyPI for Windows / MacOS / Linux users - Also available on conda-forge channel for Anaconda users.
Usage
Two ways of using jenkspy are available:
by using the jenks_breaks function which takes as input a list / tuple / array.array / numpy.ndarray of integers or floats and returns a list of values that correspond to the limits of the classes (starting with the minimum value of the series - the lower bound of the first class - and ending with its maximum value - the upper bound of the last class).
>>> import jenkspy
>>> import json
>>> with open('tests/test.json', 'r') as f:
... # Read some data from a JSON file
... data = json.loads(f.read())
...
>>> jenkspy.jenks_breaks(data, n_classes=5) # Asking for 5 classes
[0.0028109620325267315, 2.0935479691252112, 4.205495140049607, 6.178148351609707, 8.09175917180255, 9.997982932254672]
# ^ ^ ^ ^ ^ ^
# Lower bound Upper bound Upper bound Upper bound Upper bound Upper bound
# 1st class 1st class 2nd class 3rd class 4th class 5th class
# (Minimum value) (Maximum value)
by using the JenksNaturalBreaks class that is inspired by scikit-learn classes.
The .fit and .group behavior is slightly different from jenks_breaks, by accepting value outside the range of the minimum and maximum value of breaks_, retaining the input size. It means that fit and group will use only the inner_breaks_. All value below the min bound will be included in the first group and all value higher than the max bound will be included in the last group.
>>> from jenkspy import JenksNaturalBreaks
>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> jnb = JenksNaturalBreaks(4) # Asking for 4 clusters
>>> jnb.fit(x) # Create the clusters according to values in 'x'
>>> print(jnb.labels_) # Labels for fitted data
... print(jnb.groups_) # Content of each group
... print(jnb.breaks_) # Break values (including min and max)
... print(jnb.inner_breaks_) # Inner breaks (ie breaks_[1:-1])
[0 0 0 1 1 1 2 2 2 3 3 3]
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
[0.0, 2.0, 5.0, 8.0, 11.0]
[2.0, 5.0, 8.0]
>>> print(jnb.predict(15)) # Predict the group of a value
3
>>> print(jnb.predict([2.5, 3.5, 6.5])) # Predict the group of several values
[1 1 2]
>>> print(jnb.group([2.5, 3.5, 6.5])) # Group the elements into there groups
[array([], dtype=float64), array([2.5, 3.5]), array([6.5]), array([], dtype=float64)]
Installation
From pypi
pip install jenkspy
From source
git clone http://github.com/mthh/jenkspy
cd jenkspy/
python setup.py install
For anaconda users
conda install -c conda-forge jenkspy
Requirements :
Only for building from source: C compiler, Python C headers and optionally Cython.
Motivation :
Making a painless installing C extension so it could be used more easily as a dependency in an other package (and so learning how to build wheels using appveyor / travis at first - now it uses GitHub Actions).
Getting the break values! (and fast!). No fancy functionality provided, but contributions/forks/etc are welcome.
Other python implementations are currently existing but not as fast or not available on PyPi.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for jenkspy-0.3.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80173517988b27b97e9a27702f593c494db821a5848ee133faca4a14ff250189 |
|
MD5 | 6b8c666289e571f435df22dbcd5f7afa |
|
BLAKE2b-256 | 9fe53b898fc6a468f40fa1d4faf9da8e4e0d8660fbe31002eea101b973c14c2b |
Hashes for jenkspy-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d53c8501f291de28ee9775154c05f5c7124c172b010501d468cee1fddf681d0 |
|
MD5 | 3f765ac5875815aeeb59b4be164094e2 |
|
BLAKE2b-256 | e84c952a709eee2158d20015dc2d4673e3c9999eff9936b3e79dd8586f3296d2 |
Hashes for jenkspy-0.3.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c3ff8ef567a61cfc0fec0ab43a1826b199558159ecc36f44969c5436d82161c |
|
MD5 | 831c459f11aaeaaaed5b0c830b96f150 |
|
BLAKE2b-256 | 12fc453740111ea8a5f85689c19bd4c9d1b195612b24790a73eab2e6338c8d51 |
Hashes for jenkspy-0.3.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 469e0522a1a8bd945f795d4fa490974f16202b38e3d25d5de9fcb7100ec2f179 |
|
MD5 | dec8c2bfa9c053bc3203055e23299640 |
|
BLAKE2b-256 | 6e89bc61c64bfaac699d556ea2e597021322b0c9df81ccca8d0b82ed0f8a5628 |
Hashes for jenkspy-0.3.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8b2cdd66466be7e1f38eb9f559045a114e3af6daf36bd6e1c4e36022824b6e4 |
|
MD5 | 58b776e9e85268aec4cff7158f82ce37 |
|
BLAKE2b-256 | 381af64c3b32650ad2e23ad657b471cf8803b6febe582af509d8167c58fe6cbd |
Hashes for jenkspy-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91b34305ad836b0e07027dee7667907e0a656b3df5d7288a63f012d0c545e052 |
|
MD5 | c7ac5866b36a16c93f6aa065990f0d5d |
|
BLAKE2b-256 | a9451af6f4306a13c5cd98517aa3fb6a7d75b724985d290add0e88986105c2eb |
Hashes for jenkspy-0.3.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16748127b9dbfae5a3cd3c9dea14b5aa0e186b8e3af9fcd1262b430eccf9d3c4 |
|
MD5 | ce0d80a5ad6f6268727b52068e1bd002 |
|
BLAKE2b-256 | 8d1bc4d44f9dcf3561fc79811784d5dd0088868636b12b6ed8539475efb41c9f |
Hashes for jenkspy-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ded80eb0bd04ddb626173b6a2c9beb77aacf282d5c449fa6191f294c897c746 |
|
MD5 | f23931ca859d5672c86d48c5cab82829 |
|
BLAKE2b-256 | 677af2568eb3881d23cf9ae6bc41e115a5b958a73908a9d5bf83e57cb1a407b5 |
Hashes for jenkspy-0.3.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8511259f0ae7581e075aed3eac3344aa45db5b862ba6a8db2e9710a844845c78 |
|
MD5 | bb4d49801d72d2f0003b48f5512fc85a |
|
BLAKE2b-256 | 5c5d4ac18f44fa20141d09f90230ba89926e816b025e97f58b387f135727cb29 |
Hashes for jenkspy-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81c16faa53ced7a98363d82e81ef23f6580bf528b906bd61eb5a15305e27dfd7 |
|
MD5 | 47da2bfb82315241043a812c9bd3ef21 |
|
BLAKE2b-256 | ec302244d35d2cbb6813413229cc067ca0d3a522505bfdb24798652543b375dc |
Hashes for jenkspy-0.3.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2927b793d96c8db967ad497f6c81b8767a50f4432f0226c320ed33c67774d857 |
|
MD5 | b0a0651c944437fce1eaa7ca8d61988c |
|
BLAKE2b-256 | 9c0b7e6b9c42d2f65bc16be1e6ee591e0f24aeec6586254569f9668d7dc546ec |
Hashes for jenkspy-0.3.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8b590c4dd18a67377ffbdc6d582c11c578ddc17b449880bc7e5135458a1aff6 |
|
MD5 | bd1a4c92f7e77912eeaa52706678a299 |
|
BLAKE2b-256 | 1486d19352112746d26458b253c14c05a471dbf85ce95538d03f9094d782cb69 |
Hashes for jenkspy-0.3.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 296485d6127cdacc26e16bea89ea02b345762194eca1949269f5d50c826af694 |
|
MD5 | ac566597363990e22ef66c906be20758 |
|
BLAKE2b-256 | c31143fb951c54c09cce02e10bd2f90dc9556b07ac776760c3822dfe9648eab2 |
Hashes for jenkspy-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa5a08fa5a66c2702c42fa5f907e231fbe96b8e9bbd352369ff6aa47c5785d5a |
|
MD5 | ad5e2116c6f0335c9ba1ab9d9ffbe0ba |
|
BLAKE2b-256 | 2fd7e70b4a1336e79da97fa1b25e95cb922108def30cd9e6b0aa360dbbf2ad5e |
Hashes for jenkspy-0.3.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9039b4a7c9486b61ba37de1bcd43a1b3e07ef7474cf6b163348f8bf01cbe755 |
|
MD5 | 3867ded1439d0c9fd7049595cd2b05bc |
|
BLAKE2b-256 | 778671d7b171ae6aac5af882f7a2f11183601b4d5cbb619866bdd14611c6092f |
Hashes for jenkspy-0.3.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c54b235d6c66ad4bb732b4450778d5e99e749038730f4cdf20a7813ef4718bfb |
|
MD5 | 85aabffd75a70027f07ae7cf79a12d15 |
|
BLAKE2b-256 | 7d0f61b4f2d13fcf46500eab9246a6f0c4aac97febd44a60e3c6299dcb91f1a6 |
Hashes for jenkspy-0.3.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91d4a2120dcb6493c5e1fe31f6f4092fccf37654f79899ab71cc03c4cd3198d6 |
|
MD5 | 28ff2f964096b5ef1fa03fa6c99e75b5 |
|
BLAKE2b-256 | d1ef64c4b7551e04d147820350810834e32385657a72d7951df73d152b0448a8 |
Hashes for jenkspy-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5e39cb387e33a1b7287019943671a5aa70403f126d49ab33839380379225091 |
|
MD5 | c51925f15c15e41e666299421df019e2 |
|
BLAKE2b-256 | 877f79d796d0dc2b162cdfed4791a017877fbf09ead15448655cc0c4e1b145d5 |
Hashes for jenkspy-0.3.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e54787d9b497222c0e4c40ac95ecd3892b0185971835c64b5c2a2db60c12ac2 |
|
MD5 | 9dd8aa951a844551dbb7209f548738f4 |
|
BLAKE2b-256 | c75b1028faf1f90a97b565e403b2f039277e0d9245ee40160f24fb00a648aba4 |
Hashes for jenkspy-0.3.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9e45710bfa8cbddfe8f7374410b3c0b56c983bd5df4484f50e2a007f7c400a4 |
|
MD5 | b7178b4466e7a65efa67a73dc979dd06 |
|
BLAKE2b-256 | 1eb4e05559a3e3cf218279682e5cc411c749865cbaa4a1524094446d4df23736 |
Hashes for jenkspy-0.3.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc23efb6d0d287bda1c08b8aa538c661bd6ac24e14d24ef3cc1d5170fa55f99e |
|
MD5 | c83653562692ec7b8c9b2cf42fd75154 |
|
BLAKE2b-256 | d4a8ce7e700f6215945f371fe7cacfcabd8fc5c046f2bcf0a83974a6c0c85234 |
Hashes for jenkspy-0.3.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46dbbf7b764d764f58b58e11eb22fefa84ee88af77acf28d30f74722a29b25b0 |
|
MD5 | 5d0aefa48100427c642646afbecefa38 |
|
BLAKE2b-256 | 4de9e2e455b8dab8cd322e03057ec813ef4cb74385646a265ea844c19057610e |