BdeClassifier#

class bde.BdeClassifier(n_members: int = 2, hidden_layers: list[int] | None = None, seed: int = 0, loss: BaseLoss | None = None, activation: str = 'relu', epochs: int = 20, patience: int | None = None, validation_split: float = 0.15, n_samples: int = 10, warmup_steps: int = 50, lr: float = 0.001, weight_decay: float = 0.0001, n_thinning: int = 2, desired_energy_var_start: float = 0.5, desired_energy_var_end: float = 0.1, step_size_init: float | None = None, prior_family: str | PriorDist = 'standardnormal', prior_kwargs: dict[str, Any] | None = None)#

Classification wrapper with label encoding helpers.

Methods

fit(x, y)

Fit the classification ensemble on the provided dataset.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

history()

Return training history from the builder.

predict(x[, raw])

Predict class labels or return raw logits.

predict_proba(x)

Predict class probabilities for each sample.

score(X, y[, sample_weight])

Return accuracy on provided data and labels.

set_fit_request(*[, x])

Configure whether metadata should be requested to be passed to the fit method.

set_params(**params)

Set the parameters of this estimator.

set_predict_proba_request(*[, x])

Configure whether metadata should be requested to be passed to the predict_proba method.

set_predict_request(*[, raw, x])

Configure whether metadata should be requested to be passed to the predict method.

set_score_request(*[, sample_weight])

Configure whether metadata should be requested to be passed to the score method.

__init__(n_members: int = 2, hidden_layers: list[int] | None = None, seed: int = 0, loss: BaseLoss | None = None, activation: str = 'relu', epochs: int = 20, patience: int | None = None, validation_split: float = 0.15, n_samples: int = 10, warmup_steps: int = 50, lr: float = 0.001, weight_decay: float = 0.0001, n_thinning: int = 2, desired_energy_var_start: float = 0.5, desired_energy_var_end: float = 0.1, step_size_init: float | None = None, prior_family: str | PriorDist = 'standardnormal', prior_kwargs: dict[str, Any] | None = None)#

Initialise the classifier with architecture, optimisation, and sampling settings.

Parameters:
n_membersint, default=2

Number of deterministic networks in the ensemble.

hidden_layerslist[int] | None, default= None

Hidden layer widths; defaults to [4, 4] internally when None.

seedint, default=0

Shared PRNG seed for member initialisation and sampling.

lossBaseLoss | None

Custom training loss; defaults to bde.loss.GaussianNLL.

activationstr, default=’relu’

Activation function applied to each hidden layer.

epochsint, default=20

Maximum training epochs during the deterministic phase.

patienceint | None, optional

Early-stopping patience measured in epochs; None disables it.

validation_splitfloat, default=0.15

Fraction of data reserved for validation when early stopping is enabled. Must lie in (0, 1); when None, all data is used and early stopping is skipped.

n_samplesint, default=10

Posterior samples retained for each ensemble member.

warmup_stepsint, default=50

Number of warm-up iterations for the MCMC sampler.

lrfloat, default=1e-3

Learning rate for the Adam optimiser used in pre-sampling training.

weight_decay: float

Weight decay parameter for the AdamW optimiser applied during member training.

n_thinningint, default=2

Thinning interval applied to posterior samples.

desired_energy_var_startfloat, default=0.5

Target energy variance at the start of warm-up.

desired_energy_var_endfloat, default=0.1

Target energy variance at the end of warm-up.

step_size_initfloat | None, optional

Override for the sampler’s initial step size; falls back to lr.

prior_familystr or PriorDist

Prior distribution for network weights; accepts a PriorDist enum or string key. Defaults to "standardnormal".

prior_kwargsdict[str, Any] | None

Optional keyword arguments forwarded to the chosen prior_family (e.g. {"scale": 0.1} for a wider or narrower Normal or Laplace).

fit(x: Array | ndarray | bool_ | number | bool | int | float | complex, y: Array | ndarray | bool_ | number | bool | int | float | complex)#

Fit the classification ensemble on the provided dataset.

Parameters:
xArrayLike

Feature matrix of shape (n_samples, n_features).

yArrayLike

Class labels as a one-dimensional array.

Returns:
BdeClassifier

The fitted estimator instance.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating 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.

history()#

Return training history from the builder.

Returns:
dict[str, dict[str, list[float]]]

Dictionary mapping member names to their training and validation loss histories.

predict(x: Array | ndarray | bool_ | number | bool | int | float | complex, raw: bool = False)#

Predict class labels or return raw logits.

Parameters:
xArrayLike

Feature matrix of shape (n_samples, n_features).

rawbool, default=False

When True, return the raw ensemble logits with shape (n_members, n_samples_draws, n_samples, n_classes). When False, return hard labels shaped (n_samples,) in the original label encoding (if available).

Returns:
numpy.ndarray | ArrayLike

Hard labels when raw=False, or raw logits when raw=True.

predict_proba(x: Array | ndarray | bool_ | number | bool | int | float | complex)#

Predict class probabilities for each sample.

Parameters:
xArrayLike

Feature matrix of shape (n_samples, n_features).

Returns:
numpy.ndarray

Array of shape (n_samples, n_classes) with mean class probabilities across ensemble members and posterior samples.

score(X, y, sample_weight=None)#

Return accuracy on provided data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') BdeClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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:
xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in fit.

Returns:
selfobject

The updated object.

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.

set_predict_proba_request(*, x: bool | None | str = '$UNCHANGED$') BdeClassifier#

Configure whether metadata should be requested to be passed to the predict_proba method.

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 (see sklearn.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 to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_proba.

  • 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:
xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in predict_proba.

Returns:
selfobject

The updated object.

set_predict_request(*, raw: bool | None | str = '$UNCHANGED$', x: bool | None | str = '$UNCHANGED$') BdeClassifier#

Configure whether metadata should be requested to be passed to the predict method.

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 (see sklearn.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 to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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:
rawstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for raw parameter in predict.

xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for x parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') BdeClassifier#

Configure whether metadata should be requested to be passed to the score method.

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 (see sklearn.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 to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • 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_weight parameter in score.

Returns:
selfobject

The updated object.

Examples using bde.BdeClassifier#

Usage Examples

Usage Examples