MRCpy.LCMRC¶
- class MRCpy.LCMRC(s=0.3, loss='0-1', n_classes=2, alpha=None, eps=0.0001, phi='linear', use_closed_form=False, **phi_kwargs)[source]¶
Marginally Constrained Cost Sensitive Minimax Risk Classifier.
The class LCMRC implements Marginally Constrained Cost Sensitive Minimax Risk Classifiers (MRC), an MRC that can use any loss defined by a loss matrix. It implements two kinds of prediction methods.
- Parameters:
- n_classesint
Number of different possible labels for an instance.
- sfloat, default=0.3
Parameter that tunes the estimation of expected values of feature mapping function. It is used to calculate \(\boldsymbol{\lambda}\) (variance in the mean estimates for the expectations of the feature mappings) in the following way
\[\boldsymbol{\lambda} = s \cdot \text{std}(\Phi(X,Y)) / \sqrt{|X|}\]where (X,Y) is the dataset of training samples and their labels respectively and \(\text{std}(\Phi(X,Y))\) stands for standard deviation of \(\phi(X,Y)\) in the supervised dataset (X,Y).
- lossstr or array-like of shape (n_classes_classification, n_classes)
If a string, one of
'0-1','ordinal','abstention','random','ordinal-squared'. Generates the corresponding loss matrix. If an array, it will be used as the transposed loss matrix.- alphafloat, optional
Penalty for abstention in that type of loss. Defaults to
1 / (n_classes + 1).- max_itersint, default=2000
Maximum number of iterations to use for finding the solution of optimization in the subgradient approach.
- phistr or BasePhi instance, 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 aBasePhiinstance) 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.
- epsfloat, default=1e-4
Threshold for comparing values. For instance, testing if two values are different is done as
abs(value1 - value2) < epsinstead ofvalue1 == value2.- use_closed_formbool, default=False
If True, and if the loss matrix is squared, then it will use the closed form method for classification.
- **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 argumentLCMRC(phi='fourier', n_components=500).The list of arguments for each feature mappings class can be found in the corresponding documentation.
Methods
error(X, Y[, mean])Return the error obtained for the given test
fit(X, Y[, X_])Fit the MRC model.
Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
minimax_risk(X, tau_, lambda_, n_classes)Solves the minimax risk problem for different the given loss matrix
predict(X)Predict classes for new instances using a fitted model.
Conditional probabilities corresponding to each class for each unlabeled input instance
score(X, y[, sample_weight])Return the mean accuracy on the given test data and labels.
set_fit_request(*[, X_])Request metadata passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_score_request(*[, sample_weight])Request metadata passed to the
scoremethod.- __init__(s=0.3, loss='0-1', n_classes=2, alpha=None, eps=0.0001, phi='linear', use_closed_form=False, **phi_kwargs)[source]¶
Initialize self. See help(type(self)) for accurate signature.
- error(X, Y, mean=True)[source]¶
- Return the error obtained for the given test
data and labels using the loss function given
- Parameters:
- X
array-like of shape (n_samples,n_dimensions) Test instances for which the labels are to be predicted by the MRC model.
- Y
array-like of shape (n_samples, 1), default =None Labels corresponding to the testing instances used to compute the error in the prediction.
- mean: `Boolean`, defaults to `True`. If true, the method returns the mean error.
If false, the method returns an array with all the errors.
- X
- Returns:
- fit(X, Y, X_=None)¶
Fit the MRC model.
Computes the parameters required for the minimax risk optimization and then calls the
minimax_riskfunction to solve the optimization.- Parameters:
- Xarray-like of shape (n_samples, n_dimensions)
Training instances used in
Calculating the expectation estimates that constrain the uncertainty set for the minimax risk classification
Solving the minimax risk optimization problem.
n_samplesis the number of training samples andn_dimensionsis the number of features.- Yarray-like of shape (n_samples, 1), default=None
Labels corresponding to the training instances used only to compute the expectation estimates.
- X_array-like of shape (n_samples2, n_dimensions), default=None
These instances are optional and when given, will be used in the minimax risk optimization. These extra instances are generally a smaller set and give an advantage in training time.
- 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.
- minimax_risk(X, tau_, lambda_, n_classes)[source]¶
Solves the minimax risk problem for different the given loss matrix
- Parameters:
- X
array-like of shape (n_samples,n_dimensions) Training instances used for solving the minimax risk optimization problem.
- tau_
array-like of shape (n_features*n_classes) Mean estimates for the expectations of feature mappings.
- lambda_
array-like of shape (n_features*n_classes) 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
- predict(X)[source]¶
Predict classes for new instances using a fitted model.
Returns the predicted classes for the given instances in
Xusing the probabilities given by the functionpredict_proba. This method overwrites the parent’s class method so that non-square loss matrices are allowed.- Parameters:
- Xarray-like of shape (n_samples, n_dimensions)
Test instances for which the labels are to be predicted by the MRC model.
- Returns:
- y_predarray-like of shape (n_samples,)
Predicted labels corresponding to the given instances.
- predict_proba(X)[source]¶
Conditional probabilities corresponding to each class for each unlabeled input instance
- Parameters:
- X
array-like of shape (n_samples,n_dimensions) Testing instances for which the prediction probabilities are calculated for each class.
- X
- Returns:
- hy_x
ndarrayof shape (n_samples,n_classes) Probabilities \((p(y|x))\) corresponding to the predictions for each class.
- hy_x
- 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$') → MRCpy.lcmrc.LCMRC¶
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.
- 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_score_request(*, sample_weight: Union[bool, None, str] = '$UNCHANGED$') → MRCpy.lcmrc.LCMRC¶
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.