jenkspy

repository·master·Indexed 42 days ago

https://github.com/mthh/jenkspy

A 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.

Tokens
837
Snippets
3
Records
4
Agent score
4%

What's inside jenkspy

  1. What is the Fisher-Jenks algorithm?

    master
    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.
  2. Install jenkspy

    master

    You can install jenkspy via PyPI, Conda, or from source.

    Via pip

    pip install jenkspy

    Via Conda

    conda install -c conda-forge jenkspy

    From source

    git clone http://github.com/mthh/jenkspy
    cd jenkspy/
    pip install .

    Requirements for building from source:

    • C compiler
    • Python C headers
    • setuptools
    • Cython
    pip install jenkspy
  3. Use the JenksNaturalBreaks class

    master

    The JenksNaturalBreaks class provides a scikit-learn inspired interface for computing natural breaks. Unlike jenks_breaks, the .fit and .group methods handle values outside the original training range by using inner_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 input x.
    • .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)]
  4. Compute natural breaks with jenks_breaks()

    master

    The jenkspy.jenks_breaks function computes the Fisher-Jenks natural breaks. It accepts a list, tuple, array.array, or numpy.ndarray of 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