art.estimators.regression.scikitlearn

This module implements the regressors for scikit-learn models.

Base Class Scikit-learn

class art.estimators.regression.scikitlearn.ScikitlearnRegressor(model: sklearn.base.BaseEstimator, clip_values: CLIP_VALUES_TYPE | None = None, preprocessing_defences: Preprocessor | List[Preprocessor] | None = None, postprocessing_defences: Postprocessor | List[Postprocessor] | None = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))

Wrapper class for scikit-learn regression models.

clone_for_refitting() ScikitlearnRegressor

Create a copy of the classifier that can be refit from scratch.

Returns:

new estimator

compute_loss(x: ndarray, y: ndarray, **kwargs) ndarray

Compute the MSE loss of the regressor for samples x.

Return type:

ndarray

Parameters:
  • x (ndarray) – Input samples.

  • y (ndarray) – Target values.

Returns:

Loss values.

compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray

Compute the MSE loss of the regressor for predictions pred.

Return type:

ndarray

Parameters:
  • pred (ndarray) – Model predictions.

  • y (ndarray) – Target values.

Returns:

Loss values.

fit(x: ndarray, y: ndarray, **kwargs) None

Fit the regressor on the training set (x, y).

Parameters:
  • x (ndarray) – Training data.

  • y (ndarray) – Target values.

  • kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn regressor 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(x: ndarray, **kwargs) ndarray

Perform prediction for a batch of inputs.

Return type:

ndarray

Parameters:

x (ndarray) – Input samples.

Returns:

Array of predictions.

Raises:

ValueError – If the regressor does not have the method predict

reset() None

Resets the weights of the classifier so that it can be refit from scratch.

save(filename: str, path: str | None = 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.

Scikit-learn Decision Tree Regressor

class art.estimators.regression.scikitlearn.ScikitlearnDecisionTreeRegressor(model: sklearn.tree.DecisionTreeRegressor, clip_values: CLIP_VALUES_TYPE | None = None, preprocessing_defences: Preprocessor | List[Preprocessor] | None = None, postprocessing_defences: Postprocessor | List[Postprocessor] | None = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0))

Wrapper class for scikit-learn Decision Tree Regressor models.

__init__(model: sklearn.tree.DecisionTreeRegressor, clip_values: CLIP_VALUES_TYPE | None = None, preprocessing_defences: Preprocessor | List[Preprocessor] | None = None, postprocessing_defences: Postprocessor | List[Postprocessor] | None = None, preprocessing: PREPROCESSING_TYPE = (0.0, 1.0)) None

Create a Regressor instance from a scikit-learn Decision Tree Regressor model.

Parameters:
  • model – scikit-learn Decision Tree Regressor 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: CLIP_VALUES_TYPE | None

Return the clip values of the input samples.

Returns:

Clip values (min, max).

clone_for_refitting() ScikitlearnRegressor

Create a copy of the classifier that can be refit from scratch.

Returns:

new estimator

compute_loss(x: ndarray, y: ndarray, **kwargs) ndarray

Compute the MSE loss of the regressor for samples x.

Return type:

ndarray

Parameters:
  • x (ndarray) – Input samples.

  • y (ndarray) – Target values.

Returns:

Loss values.

compute_loss_from_predictions(pred: ndarray, y: ndarray, **kwargs) ndarray

Compute the MSE loss of the regressor for predictions pred.

Return type:

ndarray

Parameters:
  • pred (ndarray) – Model predictions.

  • y (ndarray) – Target values.

Returns:

Loss values.

fit(x: ndarray, y: ndarray, **kwargs) None

Fit the regressor on the training set (x, y).

Parameters:
  • x (ndarray) – Training data.

  • y (ndarray) – Target values.

  • kwargs – Dictionary of framework-specific arguments. These should be parameters supported by the fit function in sklearn regressor and will be passed to this function as such.

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.

predict(x: ndarray, **kwargs) ndarray

Perform prediction for a batch of inputs.

Return type:

ndarray

Parameters:

x (ndarray) – Input samples.

Returns:

Array of predictions.

Raises:

ValueError – If the regressor does not have the method predict

reset() None

Resets the weights of the classifier so that it can be refit from scratch.

save(filename: str, path: str | None = 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.