jenkspy

0.4.1 · active · verified Thu Apr 16

Jenkspy is a Python library providing a fast implementation of the Fisher-Jenks algorithm for computing 'natural breaks'. It's designed for 1-dimensional clustering on lists, tuples, arrays, or NumPy ndarrays of integers/floats to determine optimal class boundaries. Widely used in cartography and data analysis, the library is currently at version 0.4.1 and is actively maintained with recent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both the functional (`jenks_breaks`) and object-oriented (`JenksNaturalBreaks`) ways to compute natural breaks. The functional approach directly returns the break points, while the class-based API provides a scikit-learn-like interface with `fit` and `groups_` methods for more complex workflows.

import jenkspy
import random

# Generate some sample data
data = [random.uniform(0, 100) for _ in range(100)]

# Compute natural breaks with 5 classes using the function API
breaks_func = jenkspy.jenks_breaks(data, n_classes=5)
print(f"Jenks breaks (function API): {breaks_func}")

# Alternatively, use the scikit-learn inspired class API
from jenkspy import JenksNaturalBreaks
classifier = JenksNaturalBreaks(n_classes=5)
classifier.fit(data)

# Get the breaks and group labels
breaks_class = classifier.breaks_
groups = classifier.groups_ # Groups elements into corresponding class indices

print(f"Jenks breaks (class API): {breaks_class}")
print(f"First 10 group labels: {groups[:10]}")

view raw JSON →