MDLPDiscretizer#
- class skbn.MDLPDiscretizer(max_bins=None)#
Supervised entropy discretization with the MDL stopping rule.
Implements the recursive minimum-entropy partitioning of Fayyad and Irani (1993). Each interval is split at the cut-point that minimises the class-information entropy of the induced partition, and the split is accepted only if its information gain exceeds the Minimum Description Length threshold
\[\mathrm{Gain}(A, T; S) > \frac{\log_2(N-1)}{N} + \frac{\Delta(A, T; S)}{N}, \qquad \Delta = \log_2(3^k - 2) - [k\,E(S) - k_1 E(S_1) - k_2 E(S_2)],\]where
k,k1andk2are the numbers of classes present in the interval and in its two halves. A feature for which no split is accepted is mapped to a single interval (bin 0), so the discretizer also acts as a filter for attributes with no marginal class information.Missing values (
NaN) are imputed with the per-feature median, fitted on the training data.- Parameters:
- max_binsint or None, default=None
Optional cap on the number of intervals per feature, at least 2: the cuts that remove the most class entropy are kept.
Noneapplies the pure MDL criterion.
- Attributes:
- n_features_in_int
Number of features seen during
fit().- feature_names_in_ndarray of shape (
n_features_in_,) Names of features seen during
fit(). Defined only whenXhas feature names that are all strings.- cut_points_list of ndarray
Sorted cut-points of each feature (empty if the feature was collapsed).
- imputers_list of SimpleImputer or None
Per-feature median imputers (
Noneif the feature had no NaN).
References
Fayyad, U. M. and Irani, K. B. (1993). Multi-interval discretization of continuous-valued attributes for classification learning. IJCAI, 1022-1027.
Examples
>>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> disc = MDLPDiscretizer().fit(X, y) >>> [len(c) for c in disc.cut_points_] [2, 2, 2, 2]
Methods
fit(X, y[, sample_weight])Find the MDL cut-points of every feature.
fit_transform(X[, y])Fit to data, then transform it.
get_feature_names_out([input_features])Get output feature names for transformation.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
set_fit_request(*[, sample_weight])Configure whether metadata should be requested to be passed to the
fitmethod.set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
transform(X)Map each value to the index of its interval (0-based).
- fit(X, y, sample_weight=None)#
Find the MDL cut-points of every feature.
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Training data. May contain
NaNvalues.- yarray-like of shape (n_samples,)
Class labels.
- sample_weightarray-like of shape (n_samples,), default=None
Non-negative weights of the training samples. Samples with weight 0 are ignored.
- Returns:
- selfMDLPDiscretizer
Fitted estimator.
- fit_transform(X, y=None, **fit_params)#
Fit to data, then transform it.
Fits transformer to
Xandywith optional parametersfit_paramsand returns a transformed version ofX.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters. Pass only if the estimator accepts additional params in its
fitmethod.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- get_feature_names_out(input_features=None)#
Get output feature names for transformation.
- Parameters:
- input_featuresarray-like of str or None, default=None
Input features.
If
input_featuresisNone, thenfeature_names_in_is used as feature names in. Iffeature_names_in_is not defined, then the following input feature names are generated:["x0", "x1", ..., "x(n_features_in_ - 1)"].If
input_featuresis an array-like, theninput_featuresmust matchfeature_names_in_iffeature_names_in_is defined.
- Returns:
- feature_names_outndarray of str objects
Same as input features.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
- routingMetadataRequest
A
MetadataRequestencapsulating routing information.
- get_params(deep=True)#
Get parameters for this estimator.
- Parameters:
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns:
- paramsdict
Parameter names mapped to their values.
- set_fit_request(*, sample_weight: bool | None | str = '$UNCHANGED$') MDLPDiscretizer#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter infit.
- Returns:
- selfobject
The updated object.
- 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
transformandfit_transform."default": Default output format of a transformer"pandas": DataFrame output"polars": Polars outputNone: Transform configuration is unchanged
Added in version 1.4:
"polars"option was added.
- Returns:
- selfestimator instance
Estimator instance.
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters:
- **paramsdict
Estimator parameters.
- Returns:
- selfestimator instance
Estimator instance.
- transform(X)#
Map each value to the index of its interval (0-based).
- Parameters:
- Xarray-like of shape (n_samples, n_features)
Data to discretize. May contain
NaNvalues.
- Returns:
- X_outndarray of shape (n_samples, n_features)
Integer interval indices (stored as float).