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.3-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c6af3d5e62827e2f5fd5ac72701aad487eff10df9cbcc18f99e6183ddfaefc6 |
|
MD5 | b626e44bf379f36ba78b12e9180d4255 |
|
BLAKE2b-256 | 6e1abc2f4af2e9f998b95a64ba043735b60cd2d8c0b21f9b988a2e1e8beae94c |
Hashes for jenkspy-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 285e67daf469f43f1a1e805947caab626860f752329ddf384a82e4855a0cf611 |
|
MD5 | 6578b7ac3203a20fba5f061dfd85fcc1 |
|
BLAKE2b-256 | ce4b763afbd833a4122212902ada5c4b7f165f22f56586fdd01dcc9743b25f2c |
Hashes for jenkspy-0.3.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 927a6ab7347b7ce83f5a5910b305527c70a1d59269a4a8fd86fae15a32d2e05d |
|
MD5 | e6cc8cc4da7af0700d61c862e488df17 |
|
BLAKE2b-256 | 6a6521e60ada71e2e249bc6f4962bd2cd9adc13fc4b79518187ecb73e80ce3a2 |
Hashes for jenkspy-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d4778d235de67e24fe40b05e274e024db55f5c5996cefc178a0a1eabed248af |
|
MD5 | 8833c800eb8d80000acef5174269ac64 |
|
BLAKE2b-256 | 0be76863c538509d8b37769307e9bbca5fd470ccc2d1d587fdb770b5b4d88c2a |
Hashes for jenkspy-0.3.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5eeae447bfb3680b8600c455f76e730bc9dc53fc62d014c07ea449d1cacf1986 |
|
MD5 | f5c608aba9089f75bb45bb002d619a3b |
|
BLAKE2b-256 | e713cf7df98e88a786cb92c081cc560120d59c1fa06922a99385a707b6a9f10a |
Hashes for jenkspy-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14167093a74d06029eb1f6a263ee7b82d518b66be55bebfefee223d612f5951c |
|
MD5 | 7f5a2eb6d58650346607def1874af61c |
|
BLAKE2b-256 | e496e1f65d0fe61595eaa05d7c64c30b6cedd7be56a0c622019a11618e575f8d |
Hashes for jenkspy-0.3.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8ff7a76b6f8326e0ec55823bf59996ef4206bdf64d63ac60519c48693472b4f |
|
MD5 | 172d9f2cbe529a896f5d68897ff877a4 |
|
BLAKE2b-256 | 4c48d544a61f40fae78358f903f510adeb003fdf440a49bceb6f9a8a8671a750 |
Hashes for jenkspy-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98daa1f814640330b7c4d083635792cc3b3d77ea2615899f118e34dddc724ceb |
|
MD5 | a835c84cb55b83927642382b61b3a066 |
|
BLAKE2b-256 | 1a924a52e4a1fda6972a9261644271580f16af16e020d4768e3680bb85aec7e6 |
Hashes for jenkspy-0.3.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab6c7f64e3646d3a1b9e983bcdbb8ad5833fac3bfb2e00d2d8366887eecbad23 |
|
MD5 | a2e6e2f00cf3a56761068b49ca126297 |
|
BLAKE2b-256 | 74ef70d20a0ed3dd481d6f934c564f06f380718fb8184e7b87517fcd578cf128 |
Hashes for jenkspy-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be3d43339171dceb2b4a5f8e44b06de044a26ef8366a4a499cc31ac70c95da77 |
|
MD5 | 5023df1b6082bc9346cdc8dc5da7d1aa |
|
BLAKE2b-256 | 7d683babb3c4a119d6c38b2ebdf30daf094d0ca3db6d448a9b8bbdbf4d028a7c |
Hashes for jenkspy-0.3.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df983315dbfb35a3f9558d3102ca0d095a51a88371910e8233b282f75cd3edac |
|
MD5 | 7725d91ead8ab308ddb966743ebef70e |
|
BLAKE2b-256 | 157b13d3eb1ca82c3cde6eef18685725a30b9e465750c7c9ce477e7210277bbc |
Hashes for jenkspy-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0496ce5abcadb3bd47aba531e6f53163aae5be376b1b386ea8b7234c32c1001e |
|
MD5 | d6362fd4cfb077f5e39dbcf8ff74256b |
|
BLAKE2b-256 | 4618edb5ec6a6826b33433a87a1647859b8dcc17fd2cb36822ebacf15dc49054 |
Hashes for jenkspy-0.3.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7902e05ac0ad93740d3f0cfd650100f0ecdee868aadf024e822982f2533fcde5 |
|
MD5 | a5ba8a6d69059d763d1ec26675d0db19 |
|
BLAKE2b-256 | abd64be88c6311537b9bb73971443a80f64725464913b14683b3a6111ef4b11a |
Hashes for jenkspy-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc5708e6da7d374fa2f6b5cc299758fa6088aba78b73a7ffeffd5602f801ff60 |
|
MD5 | 4b53cb881064abacb7d4109b23ea7964 |
|
BLAKE2b-256 | 7360f3d519725edea7e498abe239574646d1d460a4fe11d7f1e0ed17968a54bc |
Hashes for jenkspy-0.3.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91bc187071d599966309174eebf156eea624dac72702333c1c00197ef42f52d5 |
|
MD5 | cd25a7a5a380c2cf36c205eb9487e450 |
|
BLAKE2b-256 | 5033519d5a93603358a68b458a063a45d077628a0c0f99c8a29ff06776332235 |
Hashes for jenkspy-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1b5ece60461adb2f8fc5f973bd4a0777d30cdd0843f739fb26cb1ceef8fd693 |
|
MD5 | ecd37d99acb304d3a3544275df95718e |
|
BLAKE2b-256 | 6bc415d42be40b85b6a30a956d8466efa87371c6c99bd349d8d36691653d918c |
Hashes for jenkspy-0.3.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a40aaf88ada5e19d8d5d4c6fc79b0e1e365bbde70a425f5e0d404f05e650cb2 |
|
MD5 | 1f8792a8a0df50f2d7cbc57488441102 |
|
BLAKE2b-256 | 8f655c596b423d4b3abf6fb0eaa9e0b994976e462154171c1b50631ff7783f83 |
Hashes for jenkspy-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32478c59f1379d73a88a0c02a9ad9a5c2bdac017fceafe6c6d6c4b5ff330f52e |
|
MD5 | 1613eaadeefe6f10f8c9ba620c64774d |
|
BLAKE2b-256 | 8af5f6d0fe6bb4793deda53be4221803bafd6c849dfff3e2700a16b557394450 |
Hashes for jenkspy-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81559edbf7bd70aabe934aab59202f916cdd33d31a3f5dc1ece0224f86af0d86 |
|
MD5 | c47c42ca2fbfd3ef7fd1081ce481dbe5 |
|
BLAKE2b-256 | 2ad080f08a17d9d5d7ad64aa831b8b17182d67577d6bfd8269f03228e22e868a |
Hashes for jenkspy-0.3.3-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d24c595fb06d57319c5cfe365961bba891cc10bd486defe18eeed4571b867928 |
|
MD5 | 2cf6135c1f54925cde8ef348b8b02107 |
|
BLAKE2b-256 | fefbf593cca7393d76e8e9acd3560ef0def410cd5bff141183ffc0be8403b80d |
Hashes for jenkspy-0.3.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f972470b8567b7e18387026c4450d2ea365b84fe232e4911f5b8d824b6e4e2e |
|
MD5 | cfb12a32f9d4eb92ee6ae0c9f530094e |
|
BLAKE2b-256 | 108ab6aaebc3a6dc6ee0da8cbf46a82e3b201a93d83d85e631d108f78eeb7c1d |
Hashes for jenkspy-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92e1b5df9595e8a1317404665a68d066640c37c760c60d884febfeb9d871ca31 |
|
MD5 | 6354ed58254a9623e92fb76414578c10 |
|
BLAKE2b-256 | 4d15e0850392046b6de0ff22feeb59d7492150bef914dac012445babcdecfd23 |