secml.ml.classifiers.multiclass

CClassifierMulticlass

class secml.ml.classifiers.multiclass.c_classifier_multi.CClassifierMulticlass(classifier, preprocess=None, n_jobs=1, **clf_params)[source]

Bases: secml.ml.classifiers.c_classifier.CClassifier

Generic interface for Multiclass Classifiers.

Parameters
classifierCClassifier.__class__

Unbound (not initialized) CClassifier subclass.

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.

n_jobsint

Number of parallel workers to use for training the classifier. Default 1. Cannot be higher than processor’s number of cores.

clf_paramskwargs

Any other construction parameter for the binary classifiers.

Attributes
class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

classifier

Returns the class of the binary classifier used.

logger

Logger for current object.

n_classes

Number of classes of training dataset.

n_features

Number of features (before preprocessing).

n_jobs
num_classifiers

Returns the number of instanced binary classifiers.

preprocess

Inner preprocessor (if any).

verbose

Verbosity level of logger output.

Methods

apply_method(self, method, *args, **kwargs)

Apply input method to all trained classifiers.

backward(self[, w])

Returns the preprocessor gradient wrt data.

binarize_dataset(class_idx, dataset)

Returns the dataset needed by the class_idx binary classifier.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

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, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

prepare(self, num_classes)

Creates num_classes copies of the binary classifier.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

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}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

apply_method(self, method, *args, **kwargs)[source]

Apply input method to all trained classifiers.

Useful to perform a routine after training (e.g. reduction, optim)

method is an unbound method to apply, e.g. CClassiferSVM.set Any other argument for method can be passed in.

abstract static binarize_dataset(class_idx, dataset)[source]

Returns the dataset needed by the class_idx binary classifier.

Parameters
class_idxint

Index of the target class.

datasetCDataset

Dataset to binarize.

Returns
bin_datasetCDataset

Binarized dataset.

property classifier

Returns the class of the binary classifier used.

estimate_parameters(self, dataset, parameters, splitter, metric, pick='first', perf_evaluator='xval')[source]

Estimate parameter that give better result respect a chose metric.

Parameters
datasetCDataset

Dataset to be used for evaluating parameters.

parametersdict

Dictionary with each entry as {parameter: list of values to test}. Example: {‘C’: [1, 10, 100], ‘gamma’: list(10.0 ** CArray.arange(-4, 4))}

splitterCDataSplitter or str

Object to use for splitting the dataset into train and validation. A splitter type can be passed as string, in this case all default parameters will be used. For data splitters, num_folds is set to 3 by default. See CDataSplitter docs for more information.

metricCMetric or str

Object with the metric to use while evaluating the performance. A metric type can be passed as string, in this case all default parameters will be used. See CMetric docs for more information.

pick{‘first’, ‘last’, ‘random’}, optional

Defines which of the best parameters set pick. Usually, ‘first’ correspond to the smallest parameters while ‘last’ correspond to the biggest. The order is consistent to the parameters dict passed as input.

perf_evaluatorCPerfEvaluator or str, optional

Performance Evaluator to use. Default ‘xval’.

Returns
best_parametersdict

Dictionary of best parameters found through performance evaluation.

get_state(self)[source]

Returns the object state dictionary.

Returns
dict

Dictionary containing the state of the object. The state of the attributes of binary classifiers is a tuple, with one value for each binary classifier.

property num_classifiers

Returns the number of instanced binary classifiers.

Returns 1 until .fit(dataset) or .prepare(num_classes) is called.

prepare(self, num_classes)[source]

Creates num_classes copies of the binary classifier.

Creates enough deepcopies of the binary classifier until num_classes binary classifiers are instanced. If num_classes < self.num_classifiers, classifiers in excess are deleted.

Parameters
num_classesint

Number of binary classifiers to instance.

set(self, param_name, param_value, copy=False)[source]

Set a parameter that has a specific name to a specific value.

Only parameters, i.e. PUBLIC or READ/WRITE attributes, can be set.

If setting is performed before training, the parameter to set must be a known .classifier attribute or a known attribute of any parameter already set during or after construction.

If possible, a reference to the parameter to set is assigned. Use copy=True to always make a deepcopy before set.

Parameters
param_namestr

Name of the parameter to set.

param_valueany

Value to set for the parameter. Using a tuple, one value for each binary classifier can be specified.

copybool

By default (False) a reference to the parameter to assign is set. If True or a reference cannot be extracted, a deepcopy of the parameter is done first.

set_state(self, state_dict, copy=False)[source]

Sets the object state using input dictionary.

Only readable attributes of the class, i.e. PUBLIC or READ/WRITE or READ ONLY, can be set.

If possible, a reference to the attribute to set is assigned. Use copy=True to always make a deepcopy before set.

Parameters
state_dictdict

Dictionary containing the state of the object. The state of the attributes of binary classifiers must be specified as a tuple, with one value for each binary classifier.

copybool, optional

By default (False) a reference to the attribute to assign is set. If True or a reference cannot be extracted, a deepcopy of the attribute is done first.

property verbose

Verbosity level of logger output.

Available levels are:

0 = no verbose output 1 = info-level logging 2 = debug-level logging

CClassifierMulticlassOVA

class secml.ml.classifiers.multiclass.c_classifier_multi_ova.CClassifierMulticlassOVA(classifier, preprocess=None, n_jobs=1, **clf_params)[source]

Bases: secml.ml.classifiers.multiclass.c_classifier_multi.CClassifierMulticlass, secml.ml.classifiers.gradients.mixin_classifier_gradient.CClassifierGradientMixin

OVA (One-Vs-All) Multiclass Classifier.

Parameters
classifierunbound class

Unbound (not initialized) CClassifier subclass.

kwargsany

Any other construction parameter for each OVA classifier.

Attributes
class_type‘ova’

Defines class type.

Methods

apply_method(self, method, *args, **kwargs)

Apply input method to all trained classifiers.

backward(self[, w])

Returns the preprocessor gradient wrt data.

binarize_dataset(class_idx, dataset)

Returns the dataset needed by the class_idx binary classifier.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

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, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

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)

Computes the gradient of the classifier’s decision function wrt x.

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.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

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).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

prepare(self, num_classes)

Creates num_classes copies of the binary classifier.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

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}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

static binarize_dataset(class_idx, dataset)[source]

Returns the dataset needed by the class_idx binary classifier.

Parameters
class_idxint

Index of the target class.

datasetCDataset

Dataset to binarize.

Returns
bin_datasetCDataset

Binarized dataset.

CClassifierMulticlassOVO

class secml.ml.classifiers.multiclass.c_classifier_multi_ovo.CClassifierMulticlassOVO(classifier, preprocess=None, **clf_params)[source]

Bases: secml.ml.classifiers.multiclass.c_classifier_multi.CClassifierMulticlass, secml.ml.classifiers.gradients.mixin_classifier_gradient.CClassifierGradientMixin

OVO (One-Vs-One) Multiclass Classifier.

Parameters
classifierunbound class

Unbound (not initialized) CClassifier subclass.

kwargsany

Any other construction parameter for each OVA classifier.

Attributes
class_type‘ovo’

Defines class type.

Methods

apply_method(self, method, *args, **kwargs)

Apply input method to all trained classifiers.

backward(self[, w])

Returns the preprocessor gradient wrt data.

binarize_dataset(class_idx, dataset)

Returns the dataset needed by the class_idx binary classifier.

binarize_subset(tr_class_idx, vs_class_idx, …)

Returns the binary dataset tr_class_idx vs vs_class_idx.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

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, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

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)

Computes the gradient of the classifier’s decision function wrt x.

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.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

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).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

prepare(self, num_classes)

Creates num_classes copies of the binary classifier.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

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}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

static binarize_dataset(class_idx, dataset)[source]

Returns the dataset needed by the class_idx binary classifier.

Parameters
class_idxint

Index of the target class.

datasetCDataset

Dataset to binarize.

Returns
bin_datasetCDataset

Binarized dataset.

static binarize_subset(tr_class_idx, vs_class_idx, dataset)[source]

Returns the binary dataset tr_class_idx vs vs_class_idx.

Parameters
tr_class_idxint

Index of the target class.

vs_class_idx: int

Index of the opposing class.

datasetCDataset

Dataset from which the subset should be extracted.

Returns
bin_subsetCDataset

Binarized subset.

property clf_pair_idx

List with the binary classifiers’ classes (indices) pairs.