art.estimators.classification.scikitlearn
¶
This module implements the classifiers for scikit-learn models.
Base Class Scikit-learn¶
- class art.estimators.classification.scikitlearn.ScikitlearnClassifier(model: sklearn.base.BaseEstimator, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0), use_logits: bool = False)¶
Class for scikit-learn classifier models.
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn DecisionTreeClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnDecisionTreeClassifier(model: sklearn.tree.DecisionTreeClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Decision Tree Classifier models.
- __init__(model: sklearn.tree.DecisionTreeClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Decision Tree Classifier model.
- Parameters
model – scikit-learn Decision Tree Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_classes_at_node(node_id: int) ndarray ¶
Returns the classification for a given node.
- Returns
Major class in node.
- get_decision_path(x: ndarray) ndarray ¶
Returns the path through nodes in the tree when classifying x. Last one is leaf, first one root node.
- Returns
The indices of the nodes in the array structure of the tree.
- get_feature_at_node(node_id: int) int ¶
Returns the feature of given id for a node.
- Returns
Feature index of feature split in this node.
- get_left_child(node_id: int) int ¶
Returns the id of the left child node of node_id.
- Returns
The indices of the left child in the tree.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- get_right_child(node_id: int) int ¶
Returns the id of the right child node of node_id.
- Returns
The indices of the right child in the tree.
- get_samples_at_node(node_id: int) int ¶
Returns the number of training samples mapped to a node.
- Returns
Number of samples mapped this node.
- get_threshold_at_node(node_id: int) float ¶
Returns the threshold of given id for a node.
- Returns
Threshold value of feature split in this node.
- get_values_at_node(node_id: int) ndarray ¶
Returns the feature of given id for a node.
- Returns
Normalized values at node node_id.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn ExtraTreeClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnExtraTreeClassifier(model: sklearn.tree.ExtraTreeClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Extra TreeClassifier Classifier models.
- __init__(model: sklearn.tree.ExtraTreeClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Extra TreeClassifier Classifier model.
- Parameters
model – scikit-learn Extra TreeClassifier Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_classes_at_node(node_id: int) ndarray ¶
Returns the classification for a given node.
- Returns
Major class in node.
- get_decision_path(x: ndarray) ndarray ¶
Returns the path through nodes in the tree when classifying x. Last one is leaf, first one root node.
- Returns
The indices of the nodes in the array structure of the tree.
- get_feature_at_node(node_id: int) int ¶
Returns the feature of given id for a node.
- Returns
Feature index of feature split in this node.
- get_left_child(node_id: int) int ¶
Returns the id of the left child node of node_id.
- Returns
The indices of the left child in the tree.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- get_right_child(node_id: int) int ¶
Returns the id of the right child node of node_id.
- Returns
The indices of the right child in the tree.
- get_samples_at_node(node_id: int) int ¶
Returns the number of training samples mapped to a node.
- Returns
Number of samples mapped this node.
- get_threshold_at_node(node_id: int) float ¶
Returns the threshold of given id for a node.
- Returns
Threshold value of feature split in this node.
- get_values_at_node(node_id: int) ndarray ¶
Returns the feature of given id for a node.
- Returns
Normalized values at node node_id.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn AdaBoostClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnAdaBoostClassifier(model: sklearn.ensemble.AdaBoostClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn AdaBoost Classifier models.
- __init__(model: sklearn.ensemble.AdaBoostClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn AdaBoost Classifier model.
- Parameters
model – scikit-learn AdaBoost Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn BaggingClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnBaggingClassifier(model: sklearn.ensemble.BaggingClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Bagging Classifier models.
- __init__(model: sklearn.ensemble.BaggingClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Bagging Classifier model.
- Parameters
model – scikit-learn Bagging Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn ExtraTreesClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnExtraTreesClassifier(model: sklearn.ensemble.ExtraTreesClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Extra Trees Classifier models.
- __init__(model: sklearn.ensemble.ExtraTreesClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Create a Classifier instance from a scikit-learn Extra Trees Classifier model.
- Parameters
model – scikit-learn Extra Trees Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- get_trees() List[Tree] ¶
Get the decision trees.
- Returns
A list of decision trees.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn GradientBoostingClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnGradientBoostingClassifier(model: sklearn.ensemble.GradientBoostingClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Gradient Boosting Classifier models.
- __init__(model: sklearn.ensemble.GradientBoostingClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Gradient Boosting Classifier model.
- Parameters
model – scikit-learn Gradient Boosting Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- get_trees() List[Tree] ¶
Get the decision trees.
- Returns
A list of decision trees.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn RandomForestClassifier Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnRandomForestClassifier(model: sklearn.ensemble.RandomForestClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Random Forest Classifier models.
- __init__(model: sklearn.ensemble.RandomForestClassifier, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Random Forest Classifier model.
- Parameters
model – scikit-learn Random Forest Classifier model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- get_trees() List[Tree] ¶
Get the decision trees.
- Returns
A list of decision trees.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn LogisticRegression Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnLogisticRegression(model: sklearn.linear_model.LogisticRegression, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn Logistic Regression models.
- __init__(model: sklearn.linear_model.LogisticRegression, clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn Logistic Regression model.
- Parameters
model – scikit-learn LogisticRegression model
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- class_gradient(x: ndarray, label: Optional[Union[int, List[int]]] = None, **kwargs) ndarray ¶
Compute per-class derivatives w.r.t. x.
Paper link: http://cs229.stanford.edu/proj2016/report/ItkinaWu-AdversarialAttacksonImageRecognition-report.pdfTypo in https://arxiv.org/abs/1605.07277 (equation 6)- Return type
ndarray
- Parameters
x (
ndarray
) – Sample input with shape as expected by the model.label – Index of a specific per-class derivative. If an integer is provided, the gradient of that class output is computed for all samples. If multiple values as provided, the first dimension should match the batch size of x, and each value will be used as target for its corresponding sample in x. If None, then gradients for all classes will be computed for each sample.
- Returns
Array of gradients of input features w.r.t. each class in the form (batch_size, nb_classes, input_shape) when computing for all classes, otherwise shape becomes (batch_size, 1, input_shape) when label parameter is specified.
- Raises
ValueError – If the model has not been fitted prior to calling this method or if the number of classes in the classifier is not known.
TypeError – If the requested label cannot be processed.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- static get_trainable_attribute_names() Tuple[str, str] ¶
Get the names of trainable attributes.
- Returns
A tuple of trainable attributes.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- loss_gradient(x: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the gradient of the loss function w.r.t. x.
- Return type
ndarray
- Parameters
x (
ndarray
) – Sample input with shape as expected by the model.y (
ndarray
) – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape (nb_samples,).
- Returns
Array of gradients of the same shape as x.
- Raises
ValueError – If the model has not been fitted prior to calling this method.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- Raises
ValueError – If the classifier does not have methods predict or predict_proba.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.
Scikit-learn SVC Classifier¶
- class art.estimators.classification.scikitlearn.ScikitlearnSVC(model: Union[sklearn.svm.SVC, sklearn.svm.LinearSVC], clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))¶
Class for scikit-learn C-Support Vector Classification models.
- __init__(model: Union[sklearn.svm.SVC, sklearn.svm.LinearSVC], clip_values: Optional[CLIP_VALUES_TYPE] = None, preprocessing_defences: Optional[Union[Preprocessor, List[Preprocessor]]] = None, postprocessing_defences: Optional[Union[Postprocessor, List[Postprocessor]]] = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None ¶
Create a Classifier instance from a scikit-learn C-Support Vector Classification model.
- Parameters
model – scikit-learn C-Support Vector Classification model.
clip_values – Tuple of the form (min, max) representing the minimum and maximum values allowed for features.
preprocessing_defences – Preprocessing defence(s) to be applied by the classifier.
postprocessing_defences – Postprocessing defence(s) to be applied by the classifier.
preprocessing – Tuple of the form (subtrahend, divisor) of floats or np.ndarray of values to be used for data preprocessing. The first value will be subtracted from the input. The input will then be divided by the second one.
- class_gradient(x: ndarray, label: Optional[Union[int, List[int]]] = None, **kwargs) ndarray ¶
Compute per-class derivatives w.r.t. x.
- Return type
ndarray
- Parameters
x (
ndarray
) – Sample input with shape as expected by the model.label – Index of a specific per-class derivative. If an integer is provided, the gradient of that class output is computed for all samples. If multiple values as provided, the first dimension should match the batch size of x, and each value will be used as target for its corresponding sample in x. If None, then gradients for all classes will be computed for each sample.
- Returns
Array of gradients of input features w.r.t. each class in the form (batch_size, nb_classes, input_shape) when computing for all classes, otherwise shape becomes (batch_size, 1, input_shape) when label parameter is specified.
- property clip_values: Optional[CLIP_VALUES_TYPE]¶
Return the clip values of the input samples.
- Returns
Clip values (min, max).
- clone_for_refitting() ScikitlearnClassifier ¶
Create a copy of the classifier that can be refit from scratch.
- Returns
new estimator
- compute_loss(x: ndarray, y: Any, **kwargs) ndarray ¶
Compute the loss of the estimator for samples x.
- Parameters
x (
ndarray
) – Input samples.y – Target values.
- Returns
Loss values.
- Return type
Format as expected by the model
- compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the loss of the estimator for predictions pred.
- Return type
ndarray
- Parameters
pred (
ndarray
) – Model predictions.y (
ndarray
) – Target values.
- Returns
Loss values.
- fit(*args, **kwargs)¶
Fit the classifier on the training set (x, y).
- Parameters
x – Training data.
y – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes).
kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn classifier and will be passed to this function as such.
- get_params() Dict[str, Any] ¶
Get all parameters and their values of this estimator.
- Returns
A dictionary of string parameter names to their value.
- property input_shape: Tuple[int, ...]¶
Return the shape of one input sample.
- Returns
Shape of one input sample.
- loss_gradient(x: ndarray, y: ndarray, **kwargs) ndarray ¶
Compute the gradient of the loss function w.r.t. x. Following equation (1) with lambda=0.
- Return type
ndarray
- Parameters
x (
ndarray
) – Sample input with shape as expected by the model.y (
ndarray
) – Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape (nb_samples,).
- Returns
Array of gradients of the same shape as x.
- property model¶
Return the model.
- Returns
The model.
- property nb_classes: int¶
Return the number of output classes.
- Returns
Number of classes in the data.
- predict(*args, **kwargs)¶
Perform prediction for a batch of inputs.
- Parameters
x – Input samples.
- Returns
Array of predictions of shape (nb_inputs, nb_classes).
- q_submatrix(rows: ndarray, cols: ndarray) ndarray ¶
Returns the q submatrix of this SVM indexed by the arrays at rows and columns.
- Return type
ndarray
- Parameters
rows (
ndarray
) – The row vectors.cols (
ndarray
) – The column vectors.
- Returns
A submatrix of Q.
- reset() None ¶
Resets the weights of the classifier so that it can be refit from scratch.
- save(filename: str, path: Optional[str] = None) None ¶
Save a model to file in the format specific to the backend framework.
- Parameters
filename (
str
) – Name of the file where to store the model.path – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library ART_DATA_PATH.
- set_params(**kwargs) None ¶
Take a dictionary of parameters and apply checks before setting them as attributes.
- Parameters
kwargs – A dictionary of attributes.
- property use_logits: bool¶
Return the Boolean for using logits.
- Returns
Boolean for using logits.