jenkspy
repository·master·Indexed 42 days ago
https://github.com/mthh/jenkspyA high-performance Python implementation of the Fisher-Jenks algorithm for computing natural breaks in datasets. It provides the jenks_breaks() function and a JenksNaturalBreaks class with a scikit-learn inspired interface for fitting and predicting clusters to minimize variance within classes.
What's inside jenkspy
- The Fisher-Jenks algorithm (also known as the Jenks Optimisation Method or Fisher exact optimization method) is a deterministic method used to calculate optimal class boundaries for a dataset. It identifies 'natural breaks' in a distribution of integers or floats to minimize the variance within classes.
Install jenkspy
masterYou can install
jenkspyvia PyPI, Conda, or from source.Via pip
pip install jenkspyVia Conda
conda install -c conda-forge jenkspyFrom source
git clone http://github.com/mthh/jenkspy cd jenkspy/ pip install .Requirements for building from source:
- C compiler
- Python C headers
setuptoolsCython
pip install jenkspyUse the JenksNaturalBreaks class
masterThe
JenksNaturalBreaksclass provides a scikit-learn inspired interface for computing natural breaks. Unlikejenks_breaks, the.fitand.groupmethods handle values outside the original training range by usinginner_breaks_. Values below the minimum bound are included in the first group, and values above the maximum bound are included in the last group.Key Attributes:
labels_: Integer labels for the fitted data.groups_: An array of arrays containing the content of each group.breaks_: The break values including the minimum and maximum.inner_breaks_: The break values excluding the minimum and maximum (breaks_[1:-1]).
Key Methods:
.fit(x): Computes the clusters based on inputx..predict(value): Predicts the group index for a single value or a list of values..group(values): Groups elements into their respective clusters, returning an array of arrays.
from jenkspy import JenksNaturalBreaks x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] jnb = JenksNaturalBreaks(4) jnb.fit(x) print(jnb.labels_) # [0 0 0 1 1 1 2 2 2 3 3 3] print(jnb.breaks_) # [0.0, 2.0, 5.0, 8.0, 11.0] print(jnb.predict(15)) # 3 (predicts group for value outside range) print(jnb.group([2.5, 3.5, 6.5])) # [array([], dtype=float64), array([2.5, 3.5]), array([6.5]), array([], dtype=float64)]Compute natural breaks with jenks_breaks()
masterThe
jenkspy.jenks_breaksfunction computes the Fisher-Jenks natural breaks. It accepts alist,tuple,array.array, ornumpy.ndarrayof integers or floats and returns a list of values representing the class limits. The returned list starts with the minimum value of the series (lower bound of the first class) and ends with the maximum value (upper bound of the last class).Parameters:
data: The input sequence of numbers.n_classes: The number of classes requested.
import jenkspy data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] breaks = jenkspy.jenks_breaks(data, n_classes=5) # Returns a list of break values including min and max