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.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5d6b803569eb728986c6e8452faff89135df65d38690dcb09201e5e9e88cfe8 |
|
MD5 | 60c3ff9f2b1b6e396b6e488aa299f3aa |
|
BLAKE2b-256 | e5754b89791828f07913b6789b6375f8c7d7946f6860fc0223363b7e0d0794d4 |
Hashes for jenkspy-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dae23f693f16a04e8cf1fd007c5d6d25850f6f259070e75ed6255ab654983e34 |
|
MD5 | b708df48a35a74560daff8c41c1c74cc |
|
BLAKE2b-256 | 840823524632764047460281499549c0683638aabb32d63989ae3a1c0aea82a0 |
Hashes for jenkspy-0.3.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b35c54ab222dfe265ad6b7b60a9b590b76b62d6d9dc205283cdcd7c7df87eda |
|
MD5 | 2a718017fda04ac817d71c38fe3a7eb3 |
|
BLAKE2b-256 | b2da02d4de72745db656c75c6e58ac4ff176912a6cc84ced3838dbf8917b9e8a |
Hashes for jenkspy-0.3.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b7fee71030777d030314dffc60aef151465c5f2e4965969b7af6800d39554f4 |
|
MD5 | c00d4e49f99ee593c82815d1309547f8 |
|
BLAKE2b-256 | 0576deb1bedf133f78f2fdd893b6d7a58f802d4611ae9cba3ff05e76fe994b17 |
Hashes for jenkspy-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 482cfc1ec7c9099523f66612155b47764acc2380321c40fc8e1c84fa352f6fb6 |
|
MD5 | dfde98d7cb2110943628f6f078089bdc |
|
BLAKE2b-256 | 48593177948e3eef2b2fff9af24e1a0b01066acb14269438807453e701ae26ab |
Hashes for jenkspy-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78ba6603686a996ce955ec4144c6e3ba7ba6dc915fe3595d7032f63fc9cdfb34 |
|
MD5 | 95c6e803847b55b4fb5ff713323af513 |
|
BLAKE2b-256 | 5df7282e98d839eeb7d8146e2a5131326e3d12096381224dc827b5cbf559a6fd |
Hashes for jenkspy-0.3.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89bd8e68eef94550ff58fc3f228c736a1f7e2b320e0f9951d184e8c085f54fc3 |
|
MD5 | 34ad6117df66937837902aa2c371e6f4 |
|
BLAKE2b-256 | c00f716fb887080a372c103fd78767dd81d35a40d3e62fbc46535502ed010c4e |
Hashes for jenkspy-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d819caac8f387d979126dcf47ae23fb17ce6fcc374e63449703cec73bfe710a1 |
|
MD5 | 5314a569fec6c710f4cd8a6380c0fdf0 |
|
BLAKE2b-256 | 86a35c37e5bc7aaa693e7f5f723b7024adc518ff8284047f30146c7e83dddbf9 |
Hashes for jenkspy-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c94737e28a96fa1a9e926e2ad759fffa12f5814f427edc62b2b8a0aa96a88c |
|
MD5 | a7da1f26c3717912ff244ab714209dd1 |
|
BLAKE2b-256 | 31e0630a4a4640f92ee9dc3b501bc925bd341278f8ab2ddf86e9000e64725cc4 |
Hashes for jenkspy-0.3.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccef4a09e93d0b742d2f2e7d4cff2aa9997088850e758b2c4eb5c35e62f9043e |
|
MD5 | 6bc7e929747e712cfcd855578568e333 |
|
BLAKE2b-256 | b5bfc6ae788ef41a1234c63e55d791da9997e9a0951e544fb7fa902513547208 |
Hashes for jenkspy-0.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5075f1d4db4c948db8b81a8124ca653c466830996f1c80fac80cfebd632c140d |
|
MD5 | b5327b0ec536bf3b26110cbbba3fed23 |
|
BLAKE2b-256 | 48ab35a81b3511f2d5f91d0150424f219169115db712dc4d5d7e9ff992aa1708 |
Hashes for jenkspy-0.3.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50e561b68a59ba86bb3429b997ac1f82de20d5a50b84df577d8f79066b73331f |
|
MD5 | e04cefb56cea1dcf676c46378eb4dcfb |
|
BLAKE2b-256 | e7abc6bd885f60b9a24ddbf0a901e26d929530e59e78b5f8e6db101a3641ddcb |
Hashes for jenkspy-0.3.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f28275ea40c9a5dba4f3ddb018a2afdc15b45c5047c6c325fa0ff1101979f7e |
|
MD5 | df5e6ddfe3e881934c941cf16c43b43e |
|
BLAKE2b-256 | 68d1b202d3c6331603a07d8964514a98d8c86d8ede61d168969abd6ac9a98591 |
Hashes for jenkspy-0.3.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d83289eec21a15a2939558b28ee299102df57533eb990e7399ffc26b26c39b |
|
MD5 | 8a927b0221057c03cd0b107b5ce5e09f |
|
BLAKE2b-256 | 92925ae4bdd9d2fa0685c5613fba913058ebf645a3233687916b7b5b46940c74 |
Hashes for jenkspy-0.3.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bb795d424c02b4b8f23c66bb363921a2639f7c9223db49708247957bf906eeb |
|
MD5 | 5e11ab1b71c53504dd25a2303d6c60d8 |
|
BLAKE2b-256 | 5e5cb32d968a43b68b66e6133d790150e6eec5ca4594f8fa8110194096d3863b |