secml.ml.classifiers.reject¶
CClassifierReject¶
-
class
secml.ml.classifiers.reject.c_classifier_reject.
CClassifierReject
(preprocess=None)[source]¶ Bases:
secml.ml.classifiers.c_classifier.CClassifier
Abstract class that defines basic methods for Classifiers with reject.
A classifier assign a label (class) to new patterns using the informations learned from training set.
This interface implements a set of generic methods for training and classification that can be used for every algorithms. However, all of them can be reimplemented if specific routines are needed.
- Parameters
- preprocessstr or CNormalizer
Features preprocess to applied to input data. Can be a CNormalizer subclass or a string with the desired preprocess type. If None, input data is used as is.
- Attributes
class_type
Defines class type.
classes
Return the list of classes on which training has been performed.
logger
Logger for current object.
n_classes
Number of classes of training dataset.
n_features
Number of features (before preprocessing).
verbose
Verbosity level of logger output.
Methods
copy
(self)Returns a shallow copy of current class.
create
([class_item])This method creates an instance of a class with given type.
decision_function
(self, x[, y])Computes the decision function for each pattern in x.
deepcopy
(self)Returns a deep copy of current class.
estimate_parameters
(self, dataset, …[, …])Estimate parameter that give better result respect a chose metric.
fit
(self, dataset[, n_jobs])Trains the classifier.
get_class_from_type
(class_type)Return the class associated with input type.
get_params
(self)Returns the dictionary of class parameters.
get_subclasses
()Get all the subclasses of the calling class.
is_fitted
(self)Return True if the classifier is trained (fitted).
is_linear
(self)True for linear classifiers, False otherwise.
list_class_types
()This method lists all types of available subclasses of calling one.
load
(path)Loads class from pickle object.
predict
(self, x[, return_decision_function, …])Perform classification of each pattern in x.
save
(self, path)Save class object using pickle.
set
(self, param_name, param_value[, copy])Set a parameter that has a specific name to a specific value.
set_params
(self, params_dict[, copy])Set all parameters passed as a dictionary {key: value}.
timed
([msg])Timer decorator.
-
abstract
predict
(self, x, return_decision_function=False, n_jobs=1)[source]¶ Perform classification of each pattern in x.
If a preprocess has been specified, input is normalized before classification.
- Parameters
- xCArray
Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).
- return_decision_functionbool, optional
Whether to return the decision_function value along with predictions. Default False.
- n_jobsint, optional
Number of parallel workers to use for classification. Default 1. Cannot be higher than processor’s number of cores.
- Returns
- labelsCArray
Flat dense array of shape (n_patterns,) with the label assigned to each test pattern. The classification label is the label of the class associated with the highest score. The rejected samples have label -1.
- scoresCArray, optional
Array of shape (n_patterns, n_classes) with classification score of each test pattern with respect to each training class. Will be returned only if return_decision_function is True.
CClassifierRejectThreshold¶
-
class
secml.ml.classifiers.reject.c_classifier_reject_threshold.
CClassifierRejectThreshold
(clf, threshold, preprocess=None)[source]¶ Bases:
secml.ml.classifiers.reject.c_classifier_reject.CClassifierReject
,secml.ml.classifiers.reject.mixin_classifier_gradient_reject_threshold.CClassifierGradientRejectThresholdMixin
- Abstract class that defines basic methods for Classifiers with reject
based on a certain threshold.
A classifier assign a label (class) to new patterns using the informations learned from training set.
The samples for which the higher score is under a certain threshold are rejected by the classifier.
- Parameters
- clfCClassifier
Classifier to which we would like to apply a reject threshold.
- thresholdfloat
Rejection threshold.
- preprocessCPreProcess or str or None, optional
Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.
- Attributes
class_type
Defines class type.
classes
Return the list of classes on which training has been performed.
clf
Returns the inner classifier.
logger
Logger for current object.
n_classes
Number of classes of training dataset.
n_features
Number of features (before preprocessing).
threshold
Returns the rejection threshold.
verbose
Verbosity level of logger output.
Methods
copy
(self)Returns a shallow copy of current class.
create
([class_item])This method creates an instance of a class with given type.
decision_function
(self, x, y)Computes the decision function for each pattern in x.
deepcopy
(self)Returns a deep copy of current class.
estimate_parameters
(self, dataset, …[, …])Estimate parameter that give better result respect a chose metric.
fit
(self, dataset[, n_jobs])Trains the classifier.
get_class_from_type
(class_type)Return the class associated with input type.
get_params
(self)Returns the dictionary of class parameters.
get_subclasses
()Get all the subclasses of the calling class.
grad_f_params
(self, x, y)Derivative of the decision function w.r.t.
grad_f_x
(self, x, y, \*\*kwargs)Derivative of the classifier decision function w.r.t.
grad_loss_params
(self, x, y[, loss])Derivative of a given loss w.r.t.
grad_tr_params
(self, x, y)Derivative of the classifier training objective function w.r.t.
hessian_tr_params
(self, x, y)Hessian of the training objective w.r.t.
is_fitted
(self)Return True if the classifier is trained (fitted).
is_linear
(self)True for linear classifiers, False otherwise.
list_class_types
()This method lists all types of available subclasses of calling one.
load
(path)Loads class from pickle object.
predict
(self, x[, return_decision_function, …])Perform classification of each pattern in x.
save
(self, path)Save class object using pickle.
set
(self, param_name, param_value[, copy])Set a parameter that has a specific name to a specific value.
set_params
(self, params_dict[, copy])Set all parameters passed as a dictionary {key: value}.
timed
([msg])Timer decorator.
-
property
classes
¶ Return the list of classes on which training has been performed.
-
property
clf
¶ Returns the inner classifier.
-
decision_function
(self, x, y)[source]¶ Computes the decision function for each pattern in x.
The discriminant function of the reject class is a vector with all its values equal to :math:` heta`, being :math:` heta` the reject threshold.
If a preprocess has been specified, input is normalized before computing the decision function.
- Parameters
- xCArray
Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).
- yint
Index of the class wrt the gradient must be computed, -1 to compute it w.r.t. the reject class
- Returns
- scoreCArray
Value of the decision function for each test pattern. Dense flat array of shape (n_patterns,).
-
fit
(self, dataset, n_jobs=1)[source]¶ Trains the classifier.
If a preprocess has been specified, input is normalized before training.
- Parameters
- datasetCDataset
Training set. Must be a
CDataset
instance with patterns data and corresponding labels.- n_jobsint, optional
Number of parallel workers to use for training the classifier. Default 1. Cannot be higher than processor’s number of cores.
- Returns
- trained_clsCClassifier
Instance of the classifier trained using input dataset.
-
property
n_classes
¶ Number of classes of training dataset.
-
predict
(self, x, return_decision_function=False, n_jobs=<no value>)[source]¶ Perform classification of each pattern in x.
The score matrix of this classifier is equal to the predicted outputs plus a column (corresponding to the reject class) with all its values equal to , being the reject threshold.
The predicted class is therefore:
where correspond to the rejection class (i.e., ) only when the maximum taken over the other classes (excluding the reject one) is not greater than the reject threshold .
If a preprocess has been specified, input is normalized before classification.
- Parameters
- xCArray
Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).
- return_decision_functionbool, optional
Whether to return the decision_function value along with predictions. Default False.
- n_jobsint, optional
Number of parallel workers to use for classification. Default _NoValue. Cannot be higher than processor’s number of cores.
- Returns
- labelsCArray
Flat dense array of shape (n_patterns,) with the label assigned to each test pattern. The classification label is the label of the class associated with the highest score. The samples for which the label is equal -1 are the ones rejected by the classifier
- scoresCArray, optional
Array of shape (n_patterns, n_classes) with classification score of each test pattern with respect to each training class. Will be returned only if return_decision_function is True.
-
property
threshold
¶ Returns the rejection threshold.