"""Code for Multiple Coefficient Binning."""
# Author: Johann Faouzi <johann.faouzi@gmail.com>
# License: BSD-3-Clause
from typing import Any, Self, cast
import numpy as np
import numpy.typing as npt
from numba import njit, prange
from scipy.stats import norm
from sklearn.base import BaseEstimator
from sklearn.tree import DecisionTreeClassifier
from sklearn.utils.multiclass import check_classification_targets
from sklearn.utils.validation import check_array, check_is_fitted, check_X_y
from pyts._base import UnivariateTransformerMixin
@njit(
[
"float64[:,:](float64[:], float64[:], int64, int64)",
# The direct unit tests for this private helper exercise it with
# plain integer arrays (unlike its real caller, which always
# passes float64 after ``check_array``), so both are compiled
# eagerly rather than only covering the production call site.
"float64[:,:](int64[:], int64[:], int64, int64)",
]
)
def _uniform_bins(
timestamp_min: npt.NDArray[np.float64],
timestamp_max: npt.NDArray[np.float64],
n_timestamps: int,
n_bins: int,
) -> npt.NDArray[np.float64]:
bin_edges = np.empty((n_timestamps, n_bins - 1))
for i in prange(n_timestamps):
bin_edges[i] = np.linspace(timestamp_min[i], timestamp_max[i], n_bins + 1)[1:-1]
return bin_edges
@njit("float64[:,:](float64[:,:], float64[:], int64, int64)")
def _digitize_1d(
X: npt.NDArray[np.float64],
bins: npt.NDArray[np.float64],
n_samples: int,
n_timestamps: int,
) -> npt.NDArray[np.float64]:
X_digit = np.empty((n_samples, n_timestamps))
for i in prange(n_timestamps):
X_digit[:, i] = np.digitize(X[:, i], bins, right=True)
return X_digit
@njit("float64[:,:](float64[:,:], float64[:,:], int64, int64)")
def _digitize_2d(
X: npt.NDArray[np.float64],
bins: npt.NDArray[np.float64],
n_samples: int,
n_timestamps: int,
) -> npt.NDArray[np.float64]:
X_digit = np.empty((n_samples, n_timestamps))
for i in prange(n_timestamps):
X_digit[:, i] = np.digitize(X[:, i], bins[i], right=True)
return X_digit
def _digitize(
X: npt.NDArray[np.float64], bins: npt.NDArray[np.float64]
) -> npt.NDArray[np.int64]:
n_samples, n_timestamps = X.shape
if bins.ndim == 1:
X_binned = _digitize_1d(X, bins, n_samples, n_timestamps)
else:
X_binned = _digitize_2d(X, bins, n_samples, n_timestamps)
return X_binned.astype('int64')
[docs]
class MultipleCoefficientBinning(BaseEstimator, UnivariateTransformerMixin):
"""Bin continuous data into intervals column-wise.
Parameters
----------
n_bins : int (default = 4)
The number of bins to produce. It must be between 2 and
``min(n_samples, 26)``.
strategy : str (default = 'quantile')
Strategy used to define the widths of the bins:
- 'uniform': All bins in each sample have identical widths
- 'quantile': All bins in each sample have the same number of points
- 'normal': Bin edges are quantiles from a standard normal distribution
- 'entropy': Bin edges are computed using information gain
alphabet : None, 'ordinal' or array-like, shape = (n_bins,)
Alphabet to use. If None, the first `n_bins` letters of the Latin
alphabet are used if `n_bins` is lower than 27, otherwise the alphabet
will be defined to [chr(i) for i in range(n_bins)]. If 'ordinal',
integers are used.
Attributes
----------
bin_edges_ : array, shape = (n_bins - 1,) or (n_timestamps, n_bins - 1)
Bin edges with shape = (n_bins - 1,) if ``strategy='normal'`` or
(n_timestamps, n_bins - 1) otherwise.
References
----------
.. [1] P. Schäfer, and M. Högqvist, "SFA: A Symbolic Fourier Approximation
and Index for Similarity Search in High Dimensional Datasets",
International Conference on Extending Database Technology,
15, 516-527 (2012).
Examples
--------
>>> from pyts.approximation import MultipleCoefficientBinning
>>> X = [[0, 4],
... [2, 7],
... [1, 6],
... [3, 5]]
>>> transformer = MultipleCoefficientBinning(n_bins=2)
>>> print(transformer.fit_transform(X))
[['a' 'a']
['b' 'b']
['a' 'b']
['b' 'a']]
"""
[docs]
def __init__(
self,
n_bins: int = 4,
strategy: str = 'quantile',
alphabet: str | npt.ArrayLike | None = None,
) -> None:
self.n_bins = n_bins
self.strategy = strategy
self.alphabet = alphabet
[docs]
def fit(self, X: npt.ArrayLike, y: npt.ArrayLike | None = None) -> Self:
"""Compute the bin edges for each feature.
Parameters
----------
X : array-like, shape = (n_samples, n_timestamps)
Data to transform.
y : None or array-like, shape = (n_samples,)
Class labels for each sample. Only used if ``strategy='entropy'``.
"""
if self.strategy == 'entropy':
if y is None:
raise ValueError("y cannot be None if strategy='entropy'.")
X, y = check_X_y(X, y, dtype=np.float64)
X = cast(npt.NDArray[np.float64], X)
y = cast(npt.NDArray[Any], y)
check_classification_targets(y)
else:
X = cast(npt.NDArray[np.float64], check_array(X, dtype=np.float64))
y = cast(npt.NDArray[Any] | None, y)
n_samples, n_timestamps = X.shape
self._n_timestamps_fit = n_timestamps
self._alphabet = self._check_params(n_samples)
self._check_constant(X)
self.bin_edges_ = self._compute_bins(
X, y, n_timestamps, self.n_bins, self.strategy
)
return self
def _check_params(self, n_samples: int) -> npt.NDArray[Any] | str:
if not isinstance(self.n_bins, (int, np.integer)):
raise TypeError("'n_bins' must be an integer.")
if not 2 <= self.n_bins <= min(n_samples, 26):
raise ValueError(
"'n_bins' must be greater than or equal to 2 and lower than "
f"or equal to min(n_samples, 26) (got {self.n_bins})."
)
if self.strategy not in ['uniform', 'quantile', 'normal', 'entropy']:
raise ValueError(
"'strategy' must be either 'uniform', 'quantile',"
f" 'normal' or 'entropy' (got {self.strategy})."
)
if not (
(self.alphabet is None)
or (isinstance(self.alphabet, str) and self.alphabet == 'ordinal')
or (isinstance(self.alphabet, (list, tuple, np.ndarray)))
):
raise TypeError(
"'alphabet' must be None, 'ordinal' or array-like "
f"with shape (n_bins,) (got {self.alphabet})."
)
if self.alphabet is None:
alphabet = np.array([chr(i) for i in range(97, 97 + self.n_bins)])
elif isinstance(self.alphabet, str) and self.alphabet == 'ordinal':
alphabet = 'ordinal'
else:
alphabet = check_array(self.alphabet, ensure_2d=False, dtype=None)
if alphabet.shape != (self.n_bins,):
raise ValueError(
"If 'alphabet' is array-like, its shape must be equal to (n_bins,)."
)
return alphabet
def _check_constant(self, X: npt.NDArray[np.float64]) -> None:
if np.any(np.max(X, axis=0) - np.min(X, axis=0) == 0):
raise ValueError("At least one timestamp is constant.")
def _check_consistent_lengths(self, X: npt.NDArray[np.float64]) -> None:
if self._n_timestamps_fit != X.shape[1]:
raise ValueError(
"The number of timestamps in X must be the same as "
"the number of timestamps when `fit` was called "
f"({self._n_timestamps_fit} != {X.shape[1]})."
)
def _compute_bins(
self,
X: npt.NDArray[np.float64],
y: npt.NDArray[Any] | None,
n_timestamps: int,
n_bins: int,
strategy: str,
) -> npt.NDArray[np.float64]:
if strategy == 'normal':
bins_edges = norm.ppf(np.linspace(0, 1, self.n_bins + 1)[1:-1])
elif strategy == 'uniform':
timestamp_min, timestamp_max = np.min(X, axis=0), np.max(X, axis=0)
bins_edges = _uniform_bins(
timestamp_min, timestamp_max, n_timestamps, n_bins
)
elif strategy == 'quantile':
bins_edges = np.percentile(
X, np.linspace(0, 100, self.n_bins + 1)[1:-1], axis=0
).T
if np.any(np.diff(bins_edges, axis=1) == 0):
raise ValueError(
"At least two consecutive quantiles are equal. "
"Consider trying with a smaller number of bins or "
"removing timestamps with low variation."
)
else:
# ``fit`` already raises if strategy='entropy' and y is None.
assert y is not None
bins_edges = self._entropy_bins(X, y, n_timestamps, n_bins)
return bins_edges
def _entropy_bins(
self,
X: npt.NDArray[np.float64],
y: npt.NDArray[Any],
n_timestamps: int,
n_bins: int,
) -> npt.NDArray[np.float64]:
bins = np.empty((n_timestamps, n_bins - 1))
clf = DecisionTreeClassifier(criterion='entropy', max_leaf_nodes=n_bins)
for i in range(n_timestamps):
clf.fit(X[:, i][:, None], y)
# sklearn's Tree is a Cython extension type with no stub for
# its attributes (threshold, children_left, ...).
threshold = clf.tree_.threshold[ # pyrefly: ignore[missing-attribute]
clf.tree_.children_left != -1
]
if threshold.size < (n_bins - 1):
raise ValueError(
f"The number of bins is too high for timestamp {i}. "
"Consider trying with a smaller number of bins or "
"removing this timestamp."
)
bins[i] = threshold
return np.sort(bins, axis=1)