MRCpy.AMRC¶
- class MRCpy.AMRC(n_classes, loss='0-1', deterministic=True, random_state=None, phi='linear', unidimensional=False, delta=0.05, order=1, W=200, N=100, fit_intercept=False, max_iters=2000, **phi_kwargs)[source]¶
Adaptive Minimax Risk Classifier
This class implements Adaptive Minimax Risk Classifiers (AMRCs) proposed in [1] for online learning with streaming data. Training samples are fed sequentially and the classification rule is updated every time a new sample is provided.
At each time step \(t\), the classifier solves the minimax risk problem:
\[\mathrm{h}_t^{\mathcal{U}_t} \in \arg\min_{\mathrm{h}} \max_{\mathrm{p} \in \mathcal{U}_t} \ell(\mathrm{h}, \mathrm{p})\]which finds the classifier \(\mathrm{h}_t\) that minimizes the worst-case expected 0-1 loss over a time-varying uncertainty set \(\mathcal{U}_t\) of distributions.
The uncertainty set \(\mathcal{U}_t\) follows the same form as \(\mathcal{U}_1\) (no marginal constraint), with time-varying parameters:
\[\mathcal{U}_t = \left\{ \mathrm{p} : \left| \mathbb{E}_{\mathrm{p}}[\Phi(x,y)] - \boldsymbol{\tau}_t \right| \leq \boldsymbol{\lambda}_t \right\}\]where \(\boldsymbol{\tau}_t\) and \(\boldsymbol{\lambda}_t\) are updated at each time step via Kalman filtering to track the time-varying underlying distribution.
AMRC provides adaptation to concept drift (change in the underlying distribution of the data) by means of a multivariate and high-order tracking of the distribution. The mean vector estimates \(\boldsymbol{\tau}_t\) and confidence vectors \(\boldsymbol{\lambda}_t\) are obtained from a linear dynamical system model with Kalman filter recursions. The kinematic model order (controlled by the
orderparameter) determines the complexity of the tracking: order 0 corresponds to a zero-order model, order 1 to white noise acceleration, and order 2 to Wiener process acceleration.AMRC provides computable tight performance guarantees at learning. The instantaneous error probability satisfies \(R(\mathrm{h}_t) \leq R(\mathcal{U}_t) + \alpha_t\), and the accumulated mistakes are bounded using the sum of minimax risks and a confidence term controlled by the
deltaparameter.It implements the 0-1 loss function and can be used with linear and Random Fourier features.
See [1] for further.
- Parameters:
- n_classes
int Number of different possible labels for an instance.
- deterministic
bool, default =True Whether the prediction of the labels should be done in a deterministic way (given a fixed
random_statein the case of using random Fourier features).- loss
str{‘0-1’}, default = ‘0-1’ Type of loss function to use for the risk minimization. AMRC supports 0-1 loss only. 0-1 loss quantifies the probability of classification error at a certain example for a certain rule.
- unidimensional
bool, default =False Whether to model change in the variables unidimensionally or not. When
True, the kinematic model order is forced to 0 and tracking is performed independently per dimension. Available for comparison purposes.- delta
float, default =0.05 Significance level for the upper bound on accumulated mistakes. Lower values produce higher (more conservative) bounds.
- order
int, default =1 Order of the kinematic model used for tracking the time-varying distribution. Controls the complexity of the Kalman filter:
0: Zero-order model (constant state).1: White noise acceleration model.2: Wiener process acceleration model.
Ignored when
unidimensional=True(forced to 0).- W
int, default =200 Window size for estimating label probabilities. The model uses the last
Wsamples for sliding-window probability estimation.- N
int, default =100 Maximum number of subgradients retained for the local approximation of \(\varphi(\cdot)\) in the optimization.
- max_iters
int, default =2000 Maximum number of iterations for the accelerated subgradient method used to solve the minimax risk optimization.
- phi
strorBasePhiinstance, default = ‘linear’ Type of feature mapping function to use for mapping the input data. The currently available feature mapping methods are ‘fourier’ and ‘linear’. The users can also implement their own feature mapping object (should be a
BasePhiinstance) and pass it to this argument. Note that when using ‘fourier’ feature mapping, training and testing instances are expected to be normalized. To implement a feature mapping, please go through the Feature Mappings section.- ‘linear’
It uses the identity feature map referred to as Linear feature map. See class
BasePhi.- ‘fourier’
It uses Random Fourier Feature map. See class
RandomFourierPhi.
- random_state
int, RandomState instance, default =None Random seed used when using ‘fourier’ for feature mappings to produce the random weights.
- fit_intercept
bool, default =False Whether to calculate the intercept for MRCs. If set to false, no intercept will be used in calculations (i.e. data is expected to be already centered).
- **phi_kwargsAdditional parameters for feature mappings.
Groups the multiple optional parameters for the corresponding feature mappings(
phi).For example in case of fourier features, the number of features is given by
n_componentsparameter which can be passed as argumentAMRC(n_classes=2, phi='fourier', n_components=500)The list of arguments for each feature mappings class can be found in the corresponding documentation.
- n_classes
See also
MRCpy.MRCMRC using uncertainty set \(\mathcal{U}_1\) without marginal constraints [2].
MRCpy.CMRCCMRC using uncertainty set \(\mathcal{U}_2\) with marginal constraints [3].
References
[1] (1,2)Álvarez, V., Mazuelas, S., & Lozano, J.A. (2022). Minimax Classification under Concept Drift with Multidimensional Adaptation and Performance Guarantees. In Proceedings of the 39th International Conference on Machine Learning, pp. 486-499.
[2]Mazuelas, S., Zanoni, A., & Pérez, A. (2020). Minimax Classification with 0-1 Loss and Performance Guarantees. Advances in Neural Information Processing Systems, 33, 302-312.
[3]Mazuelas, S., Shen, Y., & Pérez, A. (2022). Generalized Maximum Entropy for Supervised Classification. IEEE Transactions on Information Theory, 68(4), 2530-2550.
- Attributes:
- is_fitted_
bool Whether the classifier is fitted i.e., the parameters are learnt.
- mu
array-like of shape (m, 1) Classifier parameters learnt by the minimax risk optimization.
- varphi
float Value of the \(\varphi\) function at the current solution.
- sample_counter
int Number of samples processed so far.
- params_
dict Dictionary storing optimization state including Kalman filter parameters, subgradient approximation matrices, and upper bounds.
- is_fitted_
Methods
error(X, Y)Return the mean error obtained for the given test data and labels.
fit(x, y[, X_])Fit the AMRC model.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
Returns the upper bound on the expected loss for the fitted classifier.
Returns the upper bound on the accumulated mistakes of the fitted classifier.
minimax_risk(x, tau_, lambda_, n_classes)Learn classifier parameters via minimax risk optimization.
predict(X)Predict the class for a new instance using the fitted model.
Compute conditional probabilities for each class.
score(X, y[, sample_weight])Return the mean accuracy on the given test data and labels.
set_fit_request(*[, X_, x])Request metadata passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_predict_proba_request(*[, x])Request metadata passed to the
predict_probamethod.set_score_request(*[, sample_weight])Request metadata passed to the
scoremethod.- __init__(n_classes, loss='0-1', deterministic=True, random_state=None, phi='linear', unidimensional=False, delta=0.05, order=1, W=200, N=100, fit_intercept=False, max_iters=2000, **phi_kwargs)[source]¶
Initialize self. See help(type(self)) for accurate signature.
- error(X, Y)¶
Return the mean error obtained for the given test data and labels.
- Parameters:
- Xarray-like of shape (n_samples, n_dimensions)
Test instances for which the labels are to be predicted by the MRC model.
- Yarray-like of shape (n_samples, 1), default=None
Labels corresponding to the testing instances used to compute the error in the prediction.
- Returns:
- errorfloat
Mean error of the learned MRC classifier
- fit(x, y, X_=None)[source]¶
Fit the AMRC model.
Computes the parameters required for the minimax risk optimization and then calls the
minimax_riskfunction to solve the optimization. Designed for online learning where samples are fed sequentially.- Parameters:
- x
array-like of shape (n_dimensions,) Training instance used in
Calculating the expectation estimates that constrain the uncertainty set for the minimax risk classification
Solving the minimax risk optimization problem.
- y
int Label corresponding to the training instance used only to compute the expectation estimates.
- X_None
Unused in AMRC. Kept for API compatibility with BaseMRC.
- x
- Returns:
- self :
Fitted estimator
- 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.
- get_upper_bound()[source]¶
Returns the upper bound on the expected loss for the fitted classifier.
- Returns:
- upper_bound
float Upper bound of the expected loss for the fitted classifier.
- upper_bound
- get_upper_bound_accumulated()[source]¶
Returns the upper bound on the accumulated mistakes of the fitted classifier.
- Returns:
- upper_bound_accumulated
float Upper bound of the accumulated for the fitted classifier.
- upper_bound_accumulated
- minimax_risk(x, tau_, lambda_, n_classes)[source]¶
Learn classifier parameters via minimax risk optimization.
This function efficiently learns classifier parameters by solving the minimax risk optimization problem using a subgradient approach.
- Parameters:
- x
array-like of shape (n_dimensions,) Training instance used for solving the minimax risk optimization problem.
- tau_
array-like of shape (m, 1) Mean estimates for the expectations of feature mappings.
- lambda_
array-like of shape (m, 1) Variance in the mean estimates for the expectations of the feature mappings.
- n_classes
int Number of labels in the dataset.
- x
- Returns:
- self :
Fitted estimator with updated
mu,is_fitted_,varphi, andparams_attributes.
- predict(X)[source]¶
Predict the class for a new instance using the fitted model.
Returns the predicted class for the given instance in
Xusing the probabilities given by the functionpredict_proba.
- predict_proba(x)[source]¶
Compute conditional probabilities for each class.
- Parameters:
- x
array-like of shape (n_dimensions,) Testing instance for which the prediction probabilities are calculated for each class.
- x
- Returns:
- h
ndarrayof shape (n_classes,) Conditional probabilities \(p(y|x)\) corresponding to the predictions for each class.
- h
- score(X, y, sample_weight=None)¶
Return the mean accuracy on the given test 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_: Union[bool, None, str] = '$UNCHANGED$', x: Union[bool, None, str] = '$UNCHANGED$') → MRCpy.amrc.AMRC¶
Request metadata passed to the
fitmethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see 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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- X_str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
X_parameter infit.- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
xparameter infit.
- 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: Union[bool, None, str] = '$UNCHANGED$') → MRCpy.amrc.AMRC¶
Request metadata passed to the
predict_probamethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
xparameter inpredict_proba.
- Returns:
- selfobject
The updated object.
- set_score_request(*, sample_weight: Union[bool, None, str] = '$UNCHANGED$') → MRCpy.amrc.AMRC¶
Request metadata passed to the
scoremethod.Note that this method is only relevant if
enable_metadata_routing=True(seesklearn.set_config()). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.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.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline. Otherwise it has no effect.- Parameters:
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weightparameter inscore.
- Returns:
- selfobject
The updated object.
Examples using
MRCpy.AMRC¶
Example: Use of AMRC (Adaptative MRC) for Online Learning
Example: Use of AMRC (Adaptative MRC) for Online Learning