ml_jenks_breaks

ml_jenks_breaks(x, n_clusters, is_data_sorted=False)

Jenks natural breaks classification.

Mirrors the C# JenksNaturalBreaks class: partitions a one-dimensional sample into n_clusters classes minimizing the within-class sum of squared deviations.

Upstream fails on fully degenerate input: if every value of x is identical, the underlying algorithm computes a negative class boundary and errors. Any spread at all is fine, including heavily tied data.

Parameters

Name Type Description Default
x array_like The values to classify. required
n_clusters int The number of classes. Must be at least 1 and no more than len(x). required
is_data_sorted bool Set True to skip the internal sort when x is already ascending. False

Returns

Name Type Description
dict breaks (each class’s maximum value), clusters (an n_clusters by 8 array with columns start_index, end_index, count, min, max, sum, average, variance; the two index columns are 0-based positions into the SORTED data), and gvf, the goodness-of-variance-fit measure, which approaches 1 for a better fit.

Examples

>>> from corehydropy import ml_jenks_breaks
>>> x = [1, 1.2, 1.4, 8, 8.3, 8.6, 30, 31, 32]
>>> len(ml_jenks_breaks(x, n_clusters=3)["breaks"])
3