KMeansBinning
- class binlearn.methods.KMeansBinning(n_bins: int | str | None = None, allow_fallback: bool | None = None, clip: bool | None = None, preserve_dataframe: bool | None = None, fit_jointly: bool | None = None, *, bin_edges: dict[Any, list[float]] | None = None, bin_representatives: dict[Any, list[float]] | None = None, class_: str | None = None, module_: str | None = None)[source]
Bases:
IntervalBinningBaseK-means clustering-based binning implementation for natural data groupings.
This class implements K-means binning, which uses K-means clustering to identify natural groupings in the data and creates bin boundaries at the midpoints between adjacent cluster centroids. This approach is data-adaptive and creates bins that reflect the underlying distribution of values, making it particularly effective for non-uniformly distributed data.
K-means binning is particularly effective for: - Non-uniformly distributed data with natural clusters - Creating bins that preserve data density patterns - Multimodal distributions where clusters represent different modes - Cases where traditional equal-width or equal-frequency binning is inadequate
Key Features: - Data-driven bin boundary selection based on clustering - Automatically adapts to the underlying data distribution - Creates bins with meaningful separation based on value similarity - Handles irregular data distributions better than fixed-interval methods - Support for flexible bin count specification (integer or string rules)
Algorithm: 1. Apply K-means clustering to each column independently to find n_bins centroids 2. Sort the centroids in ascending order 3. Create bin edges at the midpoints between consecutive centroids 4. Add data range boundaries (min, max) as outer edges 5. Use centroids as bin representatives
- Parameters:
n_bins – Number of bins to create, or string specification for automatic calculation. Can be: - Integer: exact number of bins (and clusters) to create - ‘sqrt’: number of bins = sqrt(n_samples) - ‘log2’: number of bins = log2(n_samples) - ‘sturges’: Sturges’ rule for histogram bins Default value can be configured globally via binlearn.config.
allow_fallback – Whether to fall back to equal-width binning when K-means clustering fails or when data has insufficient variation. If True (default), uses equal-width binning as fallback with a warning. If False, raises an error when clustering fails. Default can be configured globally.
- n_bins
Number of clusters/bins to create
- allow_fallback
Whether to fall back to equal-width binning when needed
- bin_edges_
Dictionary mapping column identifiers to lists of bin edges after fitting. Edges are positioned at midpoints between cluster centroids.
- bin_representatives_
Dictionary mapping column identifiers to lists of bin representatives (the cluster centroids).
Example
>>> import numpy as np >>> from binlearn.methods import KMeansBinning >>> >>> # Multimodal data - mixture of two normal distributions >>> X1 = np.random.normal(2, 0.5, 500) # First mode >>> X2 = np.random.normal(8, 0.5, 500) # Second mode >>> X = np.concatenate([X1, X2]).reshape(-1, 1) >>> >>> binner = KMeansBinning(n_bins=4) >>> binner.fit(X) >>> X_binned = binner.transform(X) >>> # Bins naturally separate the two modes >>> >>> # Automatic bin count based on data size >>> binner_auto = KMeansBinning(n_bins='sqrt') >>> binner_auto.fit(X) # Uses sqrt(1000) ≈ 32 bins >>> >>> # Irregular distribution >>> X_irregular = np.concatenate([ ... np.random.uniform(0, 2, 100), # Uniform region ... np.random.normal(5, 0.2, 800), # Tight cluster ... np.random.uniform(8, 10, 100) # Another uniform region ... ]).reshape(-1, 1) >>> binner_adaptive = KMeansBinning(n_bins=6) >>> binner_adaptive.fit(X_irregular) # Adapts to density variations
Note
Only works with numeric data - non-numeric columns will raise errors
Performance depends on the clustering quality and data separability
May create fewer effective bins if clusters are very close together
Requires the kmeans1d package for efficient 1D K-means clustering
Inherits clipping behavior and format preservation from IntervalBinningBase
- __init__(n_bins: int | str | None = None, allow_fallback: bool | None = None, clip: bool | None = None, preserve_dataframe: bool | None = None, fit_jointly: bool | None = None, *, bin_edges: dict[Any, list[float]] | None = None, bin_representatives: dict[Any, list[float]] | None = None, class_: str | None = None, module_: str | None = None)[source]
Initialize K-means binning.
- classmethod __init_subclass__(**kwargs)
Set the
set_{method}_requestmethods.This uses PEP-487 [1] to set the
set_{method}_requestmethods. It looks for the information available in the set default values which are set using__metadata_request__*class attributes, or inferred from method signatures.The
__metadata_request__*class attributes are used when a method does not explicitly accept a metadata through its arguments or if the developer would like to specify a request value for those metadata which are different from the defaultNone.References
- static check_data_quality(data: ndarray[Any, Any], name: str = 'data') None
Check data quality and issue warnings if needed.
- fit(X: Any, y: Any | None = None, **fit_params: Any) GeneralBinningBase
Fit the binning transformer with comprehensive orchestration.
This method orchestrates the complete fitting process, handling parameter validation, input preprocessing, column separation, and routing to the appropriate fitting strategy (joint vs independent).
- Parameters:
X – Input data to fit the binning transformer on. Can be: - pandas.DataFrame: Column names are preserved - polars.DataFrame: Column names are preserved - numpy.ndarray: Numeric column indices are used - array-like: Converted to numpy array
y – Target values for supervised binning methods. Ignored by unsupervised methods. Can be array-like or None.
**fit_params – Additional fitting parameters passed to the specific binning algorithm implementation. Common parameters include: - guidance_data: Alternative guidance data (conflicts with fit_jointly=True)
- Returns:
The fitted binning transformer instance.
- Return type:
self
- Raises:
ValueError – If parameter validation fails, inputs are invalid, or conflicting parameters are provided (e.g., fit_jointly=True with guidance_data).
BinningError – If the binning algorithm fails to fit the data.
RuntimeError – If an unexpected error occurs during fitting.
Example
>>> from binlearn import EqualWidthBinning >>> import pandas as pd >>> X = pd.DataFrame({'feature1': [1, 2, 3, 4, 5], 'feature2': [10, 20, 30, 40, 50]}) >>> binner = EqualWidthBinning(n_bins=3) >>> binner.fit(X) EqualWidthBinning(...)
Note
The method automatically handles column separation when guidance_columns is specified, routing guidance columns separately from binning columns. The fitting strategy (joint vs independent) is determined by the fit_jointly parameter.
- fit_transform(X, y=None, **fit_params)
Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Input samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).
**fit_params (dict) – Additional fit parameters.
- Returns:
X_new – Transformed array.
- Return type:
ndarray array of shape (n_samples, n_features_new)
- get_input_columns() list[Any] | None
Get input columns for data preparation.
This method should be overridden by derived classes to provide appropriate column information without exposing binning-specific concepts.
- Returns:
Column information or None if not available
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequestencapsulating routing information.- Return type:
MetadataRequest
- get_params(deep: bool = True) dict[str, Any]
Get parameters for this estimator, including fitted parameters.
This method extends sklearn’s standard get_params to include fitted parameters when the estimator is fitted, enabling complete object reconstruction through the get_params/set_params interface. This is essential for pipeline persistence and model serialization.
- Parameters:
deep – If True, returns parameters for sub-estimators (not applicable here but maintained for sklearn compatibility).
- Returns:
- Return type:
Dictionary of parameter names mapped to their values, including
Example
>>> binner = EqualWidthBinning(n_bins=5) >>> params = binner.get_params() >>> print(params) {'n_bins': 5, 'clip': None, ..., 'class_': 'EqualWidthBinning', 'module_': '...'} >>> >>> binner.fit(X) >>> fitted_params = binner.get_params() >>> # Now includes: {'bin_edges': {...}, 'bin_representatives': {...}, ...}
Note
Automatically extracts constructor parameters from __init__ signature
Includes fitted parameters only when estimator is fitted
Adds class metadata for reconstruction workflows
Excludes internal sklearn attributes like n_features_in_
class_ and module_ parameters are handled specially during set_params
- inverse_transform(X: Any) Any
Inverse transform from bin indices back to representative values.
Converts discrete bin indices back to their representative values, effectively reversing the binning transformation. This is useful for interpreting results or reconstructing approximate original values.
- Parameters:
X – Input data containing bin indices to inverse transform. Should contain only binning columns (no guidance columns). Can be: - pandas.DataFrame: Column names should match binning columns - polars.DataFrame: Column names should match binning columns - numpy.ndarray: Must have same number of binning columns - array-like: Converted to numpy array
- Returns:
Inverse transformed data where bin indices are replaced with their representative values (typically bin centers). Output format matches the preserve_dataframe setting.
- Raises:
RuntimeError – If the transformer has not been fitted yet.
ValueError – If input data has wrong number of columns or invalid format.
BinningError – If inverse transformation fails.
Example
>>> # After fitting and transforming >>> X_binned = [[0, 1], [1, 0], [2, 2]] # Bin indices >>> X_reconstructed = binner.inverse_transform(X_binned) >>> print(X_reconstructed) [[0.5, 1.5], [1.5, 0.5], [2.5, 2.5]] # Representative values
Note
For guided binning (when guidance_columns is specified), the input should only contain the binning columns, not the guidance columns. The number of input columns must match the number of binning columns.
- set_output(*, transform=None)
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
transform ({"default", "pandas", "polars"}, default=None) –
Configure output of transform and fit_transform.
”default”: Default output format of a transformer
”pandas”: DataFrame output
”polars”: Polars output
None: Transform configuration is unchanged
Added in version 1.4: “polars” option was added.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_params(**params: Any) SklearnIntegrationBase
Set the parameters of this estimator.
This method supports reconstruction workflows by handling fitted parameters that come from get_params() output (without underscores) and setting them as fitted attributes (with underscores).
- Parameters:
**params – Parameters to set. Can include: - Regular constructor parameters (n_bins, clip, etc.) - Fitted parameters from get_params (bin_edges, bin_representatives) - Class metadata (ignored during reconstruction)
- Returns:
Returns the instance itself.
- Return type:
self
- transform(X: Any) Any
Transform input data using fitted binning parameters.
Applies the fitted binning transformation to new data, converting continuous values to discrete bin indices or representatives. Handles column separation when guidance columns are present.
- Parameters:
X – Input data to transform. Must have the same structure as the data used during fitting (same number of columns). Can be: - pandas.DataFrame: Column names should match training data - polars.DataFrame: Column names should match training data - numpy.ndarray: Must have same number of columns as training - array-like: Converted to numpy array
- Returns:
Transformed data where continuous values are replaced with bin indices or representative values. The output format depends on: - preserve_dataframe setting: DataFrame vs array format - binning method: indices vs representatives - guidance_columns: only binning columns are transformed
- Raises:
RuntimeError – If the transformer has not been fitted yet.
ValueError – If the input data has incompatible structure or format.
BinningError – If transformation fails due to data issues.
Example
>>> # After fitting >>> X_new = pd.DataFrame({'feature1': [1.5, 2.5], 'feature2': [15, 25]}) >>> X_binned = binner.transform(X_new) >>> print(X_binned) [[0, 0], [1, 1]] # Bin indices
Note
When guidance_columns is specified, only the binning columns are transformed. Guidance columns are filtered out from the output. The method preserves the original data format when preserve_dataframe=True.
- static validate_array_like(data: Any, name: str = 'data', allow_none: bool = False) ndarray[Any, Any] | None
Validate and convert array-like input to numpy array.
This method provides robust validation and conversion of various input formats to numpy arrays, with comprehensive error handling and helpful suggestions for common issues.
- Parameters:
data – Input data to validate and convert. Can be: - numpy.ndarray: Used directly - pandas.DataFrame/Series: Converted to numpy array - polars.DataFrame: Converted to numpy array - list, tuple: Converted to numpy array - None: Allowed only if allow_none=True
name – Name of the data parameter for error messages. Used to provide context in error messages (e.g., “X”, “y”, “guidance_data”).
allow_none – Whether to allow None as a valid input. If True, None is returned unchanged; if False, None raises InvalidDataError.
- Returns:
Validated numpy array, or None if data is None and allow_none=True. The returned array maintains the same data content but is guaranteed to be a numpy array.
- Raises:
InvalidDataError – If validation fails: - data is None when allow_none=False - data cannot be converted to numpy array - Conversion process encounters errors
Example
>>> # Valid inputs >>> arr = ValidationMixin.validate_array_like([1, 2, 3], "X") >>> print(type(arr)) <class 'numpy.ndarray'> >>> >>> # Allow None >>> result = ValidationMixin.validate_array_like(None, "y", allow_none=True) >>> print(result) None >>> >>> # Invalid input >>> ValidationMixin.validate_array_like(None, "X", allow_none=False) InvalidDataError: X cannot be None
Note
This method focuses on format validation and conversion. Content validation (like checking for NaN values) should be done separately using other validation methods.
Examples
Basic Usage
import numpy as np
from binlearn.methods import KMeansBinning
# Create data with natural clusters
np.random.seed(42)
cluster1 = np.random.normal(0, 1, (300, 2))
cluster2 = np.random.normal(5, 1, (300, 2))
cluster3 = np.random.normal(10, 1, (300, 2))
X = np.vstack([cluster1, cluster2, cluster3])
# Apply K-means binning
kmeans_binner = KMeansBinning(n_bins=3, random_state=42)
X_binned = kmeans_binner.fit_transform(X)
print(f"Original shape: {X.shape}")
print(f"Binned shape: {X_binned.shape}")
print(f"Unique bins: {np.unique(X_binned)}")
Visualizing Clusters
import matplotlib.pyplot as plt
# Visualize original data and binning results
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# Original data
axes[0].scatter(X[:, 0], X[:, 1], alpha=0.6)
axes[0].set_title('Original Data')
axes[0].set_xlabel('Feature 1')
axes[0].set_ylabel('Feature 2')
# Binned data (colored by bin)
scatter = axes[1].scatter(X[:, 0], X[:, 1], c=X_binned[:, 0],
cmap='viridis', alpha=0.6)
axes[1].set_title('K-Means Binning Results')
axes[1].set_xlabel('Feature 1')
axes[1].set_ylabel('Feature 2')
plt.colorbar(scatter, ax=axes[1])
plt.tight_layout()
plt.show()
Comparing with Other Methods
from binlearn.methods import EqualWidthBinning, EqualFrequencyBinning
# Create multimodal data
X_multimodal = np.concatenate([
np.random.normal(-3, 0.5, (200, 1)),
np.random.normal(0, 0.3, (200, 1)),
np.random.normal(3, 0.8, (200, 1))
])
# Compare different binning methods
methods = {
'Equal-Width': EqualWidthBinning(n_bins=3),
'Equal-Frequency': EqualFrequencyBinning(n_bins=3),
'K-Means': KMeansBinning(n_bins=3, random_state=42)
}
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes = axes.ravel()
# Original data
axes[0].hist(X_multimodal, bins=50, alpha=0.7, density=True)
axes[0].set_title('Original Data Distribution')
# Binning results
for i, (name, binner) in enumerate(methods.items(), 1):
X_binned = binner.fit_transform(X_multimodal)
axes[i].hist(X_binned, bins=3, alpha=0.7, density=True)
axes[i].set_title(f'{name} Binning')
# Show bin edges
if hasattr(binner, 'bin_edges_'):
edges = binner.bin_edges_[0]
for edge in edges[1:-1]: # Skip first and last
axes[i].axvline(edge, color='red', linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()
Advanced Configuration
# Custom K-means parameters
advanced_binner = KMeansBinning(
n_bins=4,
kmeans_params={
'max_iter': 300,
'tol': 1e-4,
'n_init': 10
},
random_state=42
)
X_binned = advanced_binner.fit_transform(X)
# Access the underlying K-means model
print("K-means inertia:", advanced_binner.kmeans_models_[0].inertia_)
print("Number of iterations:", advanced_binner.kmeans_models_[0].n_iter_)
Performance Considerations
import time
# K-means binning can be slower for large datasets
X_large = np.random.rand(100000, 10)
# Time different methods
methods = [
('EqualWidthBinning', EqualWidthBinning(n_bins=5)),
('KMeansBinning', KMeansBinning(n_bins=5, random_state=42))
]
for name, binner in methods:
start_time = time.time()
binner.fit_transform(X_large)
elapsed = time.time() - start_time
print(f"{name}: {elapsed:.2f}s")
# For large datasets, consider fitting on a sample
sample_size = 10000
sample_indices = np.random.choice(len(X_large), sample_size, replace=False)
X_sample = X_large[sample_indices]
start_time = time.time()
fast_binner = KMeansBinning(n_bins=5, random_state=42)
fast_binner.fit(X_sample) # Fit on sample
X_binned = fast_binner.transform(X_large) # Transform full dataset
sample_time = time.time() - start_time
print(f"Sample-based KMeans: {sample_time:.2f}s")