SafeSVC#

An example Python Notebook is available Here

Privacy protected Support Vector Classifier.

class sacroml.safemodel.classifiers.safesvc.SafeSVC(C: float = 1.0, gamma: str | float = 'scale', dhat: int = 1000, eps: float = 10, **kwargs: dict)[source]#

Privacy protected Support Vector Classifier.

Attributes:
coef_

Weights assigned to the features when kernel=”linear”.

n_support_

Number of support vectors for each class.

probA_

Parameter learned in Platt scaling when probability=True.

probB_

Parameter learned in Platt scaling when probability=True.

Methods

additional_checks(curr_separate, saved_separate)

SVC specific checks.

decision_function(X)

Evaluate the decision function for the samples in X.

examine_seperate_items(curr_vals, saved_vals)

Check model-specific items exist in both current and saved copies.

fit(train_features, train_labels)

Fit model and store model dict.

get_current_and_saved_models()

Copy self.__dict__ and split into dicts for current and saved versions.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get a dictionary of parameter values restricted to those expected.

k_hat_svm(x[, y])

Define the version which is sent to sklearn.svm.

phi_hat(input_vector)

Project a single feature.

phi_hat_multi(input_features)

Compute feature space for a matrix of inputs.

posthoc_check()

Check whether model has been interfered with since fit() was last run.

predict(test_features)

Return the predictions.

predict_log_proba(X)

Compute log probabilities of possible outcomes for samples in X.

predict_proba(test_features)

Return the predictive probabilities.

preliminary_check([verbose, apply_constraints])

Check whether current model parameters violate the safe rules.

request_release(path, ext[, target])

Save model and create a report for the TRE output checkers.

run_attack(target, attack_name[, output_dir])

Run a specified attack on the trained model and save report to file.

save([name])

Write model to file in appropriate format.

score(X, y[, sample_weight])

Return accuracy on provided data and labels.

set_fit_request(*[, train_features, ...])

Configure whether metadata should be requested to be passed to the fit method.

set_params(**kwargs)

Set params.

set_predict_proba_request(*[, test_features])

Configure whether metadata should be requested to be passed to the predict_proba method.

set_predict_request(*[, test_features])

Configure whether metadata should be requested to be passed to the predict method.

set_score_request(*[, sample_weight])

Configure whether metadata should be requested to be passed to the score method.

__init__(C: float = 1.0, gamma: str | float = 'scale', dhat: int = 1000, eps: float = 10, **kwargs: dict) None[source]#

Initialise a differentially private SVC.

additional_checks(curr_separate: dict, saved_separate: dict) tuple[str, str][source]#

SVC specific checks.

decision_function(X)#

Evaluate the decision function for the samples in X.

Parameters:
Xarray-like of shape (n_samples, n_features)

The input samples.

Returns:
Xndarray of shape (n_samples, n_classes * (n_classes-1) / 2)

Returns the decision function of the sample for each class in the model. If decision_function_shape=’ovr’, the shape is (n_samples, n_classes).

Notes

If decision_function_shape=’ovo’, the function values are proportional to the distance of the samples X to the separating hyperplane. If the exact distances are required, divide the function values by the norm of the weight vector (coef_). See also this question for further details. If decision_function_shape=’ovr’, the decision function is a monotonic transformation of ovo decision function.

examine_seperate_items(curr_vals: dict, saved_vals: dict) tuple[str, bool]#

Check model-specific items exist in both current and saved copies.

fit(train_features: ndarray, train_labels: ndarray) None[source]#

Fit model and store model dict.

get_current_and_saved_models() tuple[dict, dict]#

Copy self.__dict__ and split into dicts for current and saved versions.

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep: bool = True) dict#

Get a dictionary of parameter values restricted to those expected.

k_hat_svm(x: ndarray, y: ndarray | None = None) ndarray#

Define the version which is sent to sklearn.svm.

phi_hat(input_vector: ndarray) ndarray#

Project a single feature.

phi_hat_multi(input_features: ndarray) ndarray#

Compute feature space for a matrix of inputs.

posthoc_check() tuple[str, bool]#

Check whether model has been interfered with since fit() was last run.

predict(test_features: ndarray) ndarray#

Return the predictions.

predict_log_proba(X)#

Compute log probabilities of possible outcomes for samples in X.

The model need to have probability information computed at training time: fit with attribute probability set to True.

Parameters:
Xarray-like of shape (n_samples, n_features) or (n_samples_test, n_samples_train)

For kernel=”precomputed”, the expected shape of X is (n_samples_test, n_samples_train).

Returns:
Tndarray of shape (n_samples, n_classes)

Returns the log-probabilities of the sample for each class in the model. The columns correspond to the classes in sorted order, as they appear in the attribute classes_.

Notes

The probability model is created using cross validation, so the results can be slightly different than those obtained by predict. Also, it will produce meaningless results on very small datasets.

predict_proba(test_features: ndarray) ndarray#

Return the predictive probabilities.

preliminary_check(verbose: bool = True, apply_constraints: bool = False) tuple[str, bool]#

Check whether current model parameters violate the safe rules.

Optionally fixes violations.

Parameters:
verbosebool

A boolean value to determine increased output level.

apply_constraintsbool

A boolean to determine whether identified constraints are to be upheld and applied.

Returns:
msgstring

A message string.

disclosivebool

A boolean value indicating whether the model is potentially disclosive.

request_release(path: str, ext: str, target: Target = None) None#

Save model and create a report for the TRE output checkers.

Parameters:
pathstring

Path to save the outputs.

extstr

File extension defining the model saved format, e.g., “pkl” or “sav”.

targetattacks.target.Target

Contains model and dataset information.

Notes

If target is not null, then worst case MIA and attribute inference attacks are called via run_attack.

run_attack(target: Target, attack_name: str, output_dir: str = 'outputs_safemodel') dict#

Run a specified attack on the trained model and save report to file.

Parameters:
targetTarget

The target in the form of a Target object.

attack_namestr

Name of the attack to run.

output_dirstr

Name of the directory to store JSON and PDF reports.

Returns:
dict

Metadata results.

save(name: str = 'undefined') None#

Write model to file in appropriate format.

Note this is overloaded in SafeKerasClassifer to deal with tensorflow specifics.

Parameters:
namestring

The name of the file to save.

Notes

Optimizer is deliberately excluded to prevent possible restart to training and thus possible back door into attacks.

score(X, y, sample_weight=None)#

Return accuracy on provided data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Parameters:
Xarray-like of shape (n_samples, n_features)

Test samples.

yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

Returns:
scorefloat

Mean accuracy of self.predict(X) w.r.t. y.

set_fit_request(*, train_features: bool | None | str = '$UNCHANGED$', train_labels: bool | None | str = '$UNCHANGED$') SafeSVC#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
train_featuresstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for train_features parameter in fit.

train_labelsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for train_labels parameter in fit.

Returns:
selfobject

The updated object.

set_params(**kwargs: dict) None#

Set params.

set_predict_proba_request(*, test_features: bool | None | str = '$UNCHANGED$') SafeSVC#

Configure whether metadata should be requested to be passed to the predict_proba method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict_proba if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict_proba.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
test_featuresstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for test_features parameter in predict_proba.

Returns:
selfobject

The updated object.

set_predict_request(*, test_features: bool | None | str = '$UNCHANGED$') SafeSVC#

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
test_featuresstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for test_features parameter in predict.

Returns:
selfobject

The updated object.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') SafeSVC#

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:
sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in score.

Returns:
selfobject

The updated object.

property coef_#

Weights assigned to the features when kernel=”linear”.

Returns:
ndarray of shape (n_features, n_classes)
examine_seperately_items: list[str]#
filename: str#
ignore_items: list[str]#
model_load_file: str#
model_save_file: str#
model_type: str#
property n_support_#

Number of support vectors for each class.

property probA_#

Parameter learned in Platt scaling when probability=True.

Returns:
ndarray of shape (n_classes * (n_classes - 1) / 2)
property probB_#

Parameter learned in Platt scaling when probability=True.

Returns:
ndarray of shape (n_classes * (n_classes - 1) / 2)
researcher: str#
timestamp: str#
unused_param = 'nu'#