secml.ml.classifiers.multiclass

CClassifierMulticlass

class secml.ml.classifiers.multiclass.c_classifier_multi.CClassifierMulticlass(classifier, preprocess=None, **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.

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

num_classifiers

Returns the number of instanced binary classifiers.

verbose

Verbosity level of logger output.

Methods

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

Apply input method to all trained classifiers.

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.

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.

prepare(self, num_classes)

Creates num_classes copies of the binary classifier.

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.

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', n_jobs=1)[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 informations.

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

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

n_jobsint, optional

Number of parallel workers to use for performance evaluation. Default 1. Cannot be higher than processor’s number of cores.

Returns
best_parametersdict

Dictionary of best parameters found through performance evaluation.

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. RW parameters must be set using their real name, e.g. use attr instead of _rw_attr.

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.

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, **clf_params)[source]

Bases: secml.ml.classifiers.multiclass.c_classifier_multi.CClassifierMulticlass, secml.ml.classifiers.multiclass.mixin_classifier_gradient_multiclass_ova.CClassifierGradientMulticlassOVAMixin

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.

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.

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.

prepare(self, num_classes)

Creates num_classes copies of the binary classifier.

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.

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.