Compute Natural Breaks (Fisher-Jenks algorithm)
Project description
Jenkspy: Fast Fisher-Jenks breaks for Python
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.7+
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 alist
/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 byscikit-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/
pip install .
- For anaconda users
conda install -c conda-forge jenkspy
Requirements
-
Only for building from source: C compiler, Python C headers, setuptools and 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.4.1-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb12ceb98a124d8f1bee6ed438e0e3d63e126459865834af6dac5aaf9bdcc60c |
|
MD5 | 5908c4c7ae2855c04a708697c967b2be |
|
BLAKE2b-256 | b18f90491811e4000bb751b3f9f10bef30ea35969381abfedf1e60d64554ba4e |
Hashes for jenkspy-0.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 277ce49e82cec01a809e5c9e1c63efac2baca1949d07ece53324bb7451a26cac |
|
MD5 | c619c149dac551ab9406cdd519a97f3f |
|
BLAKE2b-256 | 2ada76455dca608c4a286e9b0253e8f2de9f90951cc52f71098827465c05ab16 |
Hashes for jenkspy-0.4.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 988f7261e82fe88770f95d5712bf2663ee73e1630e873ad016588cb1e5234d69 |
|
MD5 | 2cb5c3287cb3b7b9aea458ca0b6b31ff |
|
BLAKE2b-256 | efcb74466013039bbce311d16e91764bdcccbbdef1ac4c5794952c16772f3978 |
Hashes for jenkspy-0.4.1-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c49cd705c38acd7e8fb6afd3a998bc0ba754685d9110c239233b30e6da6a769 |
|
MD5 | 22e4399f46b9296f7ce0b338d4c5c9c8 |
|
BLAKE2b-256 | a1eec4a8af2dd0e63b60d3141683887c0ae79b84cc8bee53832a334744f94d36 |
Hashes for jenkspy-0.4.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b09beca37d1d7bb2b3857394bd046e2aba864f88b085607b89ff4269c2e6ccb |
|
MD5 | 4f3123a59c51a10783a53bf9604a9cf2 |
|
BLAKE2b-256 | 9e9417b62b9b4d0e143587bad142e7d31c1aa6823f0f10ca42129d2e186ef7ed |
Hashes for jenkspy-0.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b27df244b7377b8cb54c48d887aea94f374d50afa8bd92c96115e88805ca6e12 |
|
MD5 | 7c32024f0c964acfe375e44ffac079b0 |
|
BLAKE2b-256 | c0b47a3602f0e2251c51618c6c0f6a7d9b6e3bf5110144c2942f834acc07e280 |
Hashes for jenkspy-0.4.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2fcb61df4bbed3bd924e85f3d6ee2ac9e11572ef135873996750f67cd755116 |
|
MD5 | 78380bdde44edd11d27b7f4c56c6fa2f |
|
BLAKE2b-256 | 3c6373abdb2a11b0f5cc3e2284835c79174560af499ccf52021a32a1d32517f4 |
Hashes for jenkspy-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee46e3d92a93fd7c4d3a880fdbc9c2d7e082d4d2316f93012f473bf04d45997f |
|
MD5 | 14a680787d7d89ab541e3d7ece4a6c43 |
|
BLAKE2b-256 | 82bafb6e076bd017ff9a25bfd0bcf9df98917e2c3ad3e624929714e9dd8955b3 |
Hashes for jenkspy-0.4.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5039bddb1465f0ada5c465921628aa9f7fd96cb84cb2fbd1aaf397f0c1e398c1 |
|
MD5 | 4693b1188d97a1d9830c2fc7cc4dd13d |
|
BLAKE2b-256 | 81e9258e0665fe6a586c0ac33fce78848b1c19b0c5090d8963d42353648be7d6 |
Hashes for jenkspy-0.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7bf9b2b9d04239971c01ff862a84bb1b05ef216a1a6723f23a99e411f18a990 |
|
MD5 | 27bef752fe34010bf2640c66577868fc |
|
BLAKE2b-256 | 90f67509d13884421a6be1351b08cdadec65dab0b09a8590a874b3f983d4bd48 |
Hashes for jenkspy-0.4.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3269be0dee8681f3ef51bf0d05e0954d9cb812e004e5124900c2245d20d4c14 |
|
MD5 | e27dfb00fa16c60d5c676263a2d22bfe |
|
BLAKE2b-256 | 61b0390b9df76a16db2bfce0cc1b507f8ab24b3350888e1d6a1677a6ae2f0c39 |
Hashes for jenkspy-0.4.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f13c4247605cdcb9c3a5147172c0a865344ca411b5fee740e05a3e98f2c3af2 |
|
MD5 | 234d2938a293f219abbb58a11e39d115 |
|
BLAKE2b-256 | bb3ed7bf6684f09d69a0f9bc183ff30523e72e105e5e72f83614ab6eba0d3de7 |
Hashes for jenkspy-0.4.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3c1c7a67e615ecc43c78537d9d341cf6282db0307b19c5d27f3cdb6af26a1bc |
|
MD5 | 9fec23950bcf06904a05430f24d3aac7 |
|
BLAKE2b-256 | 33be5c40e9299cd936327b8d303e0d7ef1513f198127ded5e1942c5dad7b125e |
Hashes for jenkspy-0.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c409a9f10d5deca571830de0368db721803cd3f20905a9640a42efb9c77ef572 |
|
MD5 | d2c652d4e05d7300aaf9cb367e7b3e93 |
|
BLAKE2b-256 | 3c21790b5174def7c287c5500cf38d9ced7920b1bd770b9447d82a89c4813e8e |
Hashes for jenkspy-0.4.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b932f907f922d2fbccd9326e5a2de352d993bcb5b1b27c9de414467b97632e46 |
|
MD5 | 519c8b81d6052e3eeef58321aa4a526e |
|
BLAKE2b-256 | 604545bdc3f13c1bdfe33a0695d123b3b166ddb50d83be89f7bf15e13814ed0c |
Hashes for jenkspy-0.4.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 562fa39a5b0d57572a0a86672eab3d1f192c334610fb98e8a7bc67521d7ba79d |
|
MD5 | b6ccbd32de4dd8dbb97b768dd4bbccec |
|
BLAKE2b-256 | 690f83c3bfa4df25347aa4114518f3661bfef5227f28986def3f2a02e49517d5 |
Hashes for jenkspy-0.4.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d7b2df0275cfa2c4971b0b76e45d02f1336a44e5ac86339cbcdd1c8bc439317 |
|
MD5 | 57a40ac59b6d96fd1029d15a694829b5 |
|
BLAKE2b-256 | e0edf6aa1c92c7d29b9d91346071664291c4aba9d82ef85c2b53930976954689 |
Hashes for jenkspy-0.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36a336a761f96feee7c0da61f7b35ca34dbfe3796298212e4f678b2549f69f02 |
|
MD5 | b3679297fc8047fcf8496cdafeded61d |
|
BLAKE2b-256 | afe0492055f2747516243aefe92aa92bb5bf9f513cf2789101038ba0107ae2aa |
Hashes for jenkspy-0.4.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aad2d881ad24f6be218c73ec77055e66f0bea34c28eb68e516f30541f756ce32 |
|
MD5 | 3caa7651ba318df151ca98b7edfe4fa9 |
|
BLAKE2b-256 | 9283a406c2db7e514e6eac150f73c449d27a7c18c133516753a07588c77a09be |
Hashes for jenkspy-0.4.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cbcd758c5719d2bd69794be564f2c1f5800befb63923f8df14d09f26b21df82 |
|
MD5 | 5a4340898f7c51f34294fb950bddbebb |
|
BLAKE2b-256 | 9f4a3a7f6432a98b7b5a318f6aa445ebe0b5d5d16fb26287fe6c50db6dbb1fbd |
Hashes for jenkspy-0.4.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a50cefc866f34482f141b07b892201ed2c59151d18af46b4edad7fc108132700 |
|
MD5 | dbc8394b10b36b8d4568a8fe64b3d151 |
|
BLAKE2b-256 | d05b9252601ec0aa2ce487090273e3c44c1e2a5a761564cabd0f473246775ffe |
Hashes for jenkspy-0.4.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4636e5decabf4aae7624af669625fcefd29b54d70a8f6bd938ce087a5b64cbc |
|
MD5 | 3a7be9c8a200f3c421132a4e53f83afc |
|
BLAKE2b-256 | f900891c9b6dd7074cf8c6e388130e30ab34d2b27336f5353841b33850e72314 |
Hashes for jenkspy-0.4.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c783dc51b4af9c970303d59c298c3f7d40ddc1d0e8602b165aa7440a52942061 |
|
MD5 | afe573efd9ce737cfb15a452dd3c02a3 |
|
BLAKE2b-256 | b612e818048a0a5adacbebfcf24c63c88d73212fc110b022470d2d7d0793d00c |