Compute Natural Breaks (Jenks algorythm)
Project description
Compute “natural breaks” (Fisher-Jenks algorithm) on list / tuple / array / numpy.ndarray of integers/floats.
Intented compatibility: CPython 3.4+
Wheels are provided via PyPI for windows users - Also available on conda-forge channel for Anaconda users
Usage :
This package consists of a single function (named jenks_breaks) which takes as input a list / tuple / array.array / numpy.ndarray of integers or floats. It 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 random
>>> list_of_values = [random.random()*5000 for _ in range(12000)]
>>> breaks = jenkspy.jenks_breaks(list_of_values, nb_class=6)
>>> breaks
(0.1259707312994962, 1270.571003315598, 2527.460251085392, 3763.0374498649376, 4999.87456576267)
>>> import json
>>> with open('tests/test.json', 'r') as f:
... data = json.loads(f.read())
...
>>> jenkspy.jenks_breaks(data, nb_class=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)
This package also support a JenksNaturalBreaks (require NumPy) class as interface (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_bound_. 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()
>>> try:
... print(jnb.labels_)
... print(jnb.groups_)
... print(jnb.inner_breaks_)
>>> except:
... pass
>>> jnb.fit(x)
>>> try:
... print(jnb.labels_)
... print(jnb.groups_)
... print(jnb.inner_breaks_)
>>> except:
... pass
[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])]
[2.0, 5.0, 8.0]
>>> print(jnb.predict(15))
3
>>> print(jnb.predict([2.5, 3.5, 6.5]))
[1 1 2]
>>> print(jnb.group([2.5, 3.5, 6.5]))
[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 :
NumPy*
C compiler+
Python C headers+
* only for using JenksNaturalBreaks interface
+ only for building from source
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).
Getting the break values! (and fast!). No fancy functionnality provided, but contributions/forks/etc are welcome.
Other python implementations are currently existing but not as fast nor 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.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc8dacd518141808222ee103404f68c340b75b5ffd727bbaca84b9e49f6e2068 |
|
MD5 | 557887ce6baf787b6ffb8a297cd45ba4 |
|
BLAKE2b-256 | 68e73093193abce96da0c9370e560ac208ff5bf6530dd92a27f5689357726e9b |
Hashes for jenkspy-0.2.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c5b5b0cfbd097f7ef84a5e60be0dc3f917996626d546a09ae8e42f129373387 |
|
MD5 | 7c2a5cfab96b77508fdab243a6db28de |
|
BLAKE2b-256 | c1bc15c3f5522e18633b8f3af8f28039b5629a959e855e4483bb2ad58e9f76ba |
Hashes for jenkspy-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f185e1168bd53a365b92628303987d91e1de640a10a93d2f589e36b59bc4f48d |
|
MD5 | 81f9ecc68aa5bc1c9e5105cccbfd15f6 |
|
BLAKE2b-256 | 7d2b89ade98b078590708a451435e58799a5a6f64d7ed47c7e68a4c223bc9813 |
Hashes for jenkspy-0.2.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d0b4b1646f7533b04567a99d9d6a829282212731d1df81ace639bde22613fd9 |
|
MD5 | e5c696ac0832cb1703ea51010edf58be |
|
BLAKE2b-256 | 6e9d76947a2060621657bff32828078128751017d715566444d28ec181c918d4 |
Hashes for jenkspy-0.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44d4e4ba134280c74697d7343a1a5fad25f8ae3ee6fe6ee1fd752049a13d2803 |
|
MD5 | 5a2da8f0eb8729206b2d3231afa7c133 |
|
BLAKE2b-256 | 1d74fb6f3fb2b2b0b24b0aed1b3bdf917a3d42b53183e8aae464bf5e16b87784 |
Hashes for jenkspy-0.2.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcc35b415a0babf9a1288bf62157426f2812557484859d479a79fcfb8e26e698 |
|
MD5 | 83fff84ea2ee5d89de0a4e0146f8c870 |
|
BLAKE2b-256 | c4e49b35f9baedab820aaf9bf7b852e2959e4dfb93c7a844c7d72054a6075960 |
Hashes for jenkspy-0.2.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0c22da8c101ea672b6911d8eb03dc4b8d8df45ab6740f14b90e1680fcf58f6e |
|
MD5 | 6717e3c084fb6c02c69a6c2c0f5fe893 |
|
BLAKE2b-256 | 33b944843afef0b40bcc828c58505b28b5b050833cf1d0d93ec808fbf2e19fcf |
Hashes for jenkspy-0.2.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe6d9de027029c818b116b0d43304423df40b13f19347bc2bd73cda24760206c |
|
MD5 | 15a2159bdb75aef610d05890c2ef9313 |
|
BLAKE2b-256 | de5a8e00e76e92fd763a6d95edbae2eb53ea7532a4e8bc3c11f58ff2841c6d4a |
Hashes for jenkspy-0.2.0-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18e9c076bbb460e6be79b24aaa4600c3f57aa0970d09c434366bba1832c0420a |
|
MD5 | d16e2e497ee5ee86e4fec483570bc600 |
|
BLAKE2b-256 | f41deee12156cc0854c646ddcfafab5a45e7b6dbb60d8e90b4b7c52125ebbd68 |
Hashes for jenkspy-0.2.0-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fbc00b251e4f1a67be4b95446b9312c68533829644c7cfaa39d0e68f8a6afe1 |
|
MD5 | f145c41fba6e42715d75c64fa6e12066 |
|
BLAKE2b-256 | 5ccf0f94a0fde8b45d4dd1156783d8bd6bcce5d5583313fe8fe6f749e0f9f170 |