SafeRandomForest

An example Python Notebook is available Here

Privacy protected Random Forest classifier.

class aisdc.safemodel.classifiers.saferandomforestclassifier.SafeRandomForestClassifier(**kwargs: Any)[source]

Privacy protected Random Forest classifier.

Attributes:
estimators_samples_

The subset of drawn samples for each base estimator.

feature_importances_

The impurity-based feature importances.

Methods

additional_checks(curr_separate, saved_separate)

Random Forest-specific checks would benefit from refactoring into simpler blocks perhaps.

apply(X)

Apply trees in the forest to X, return leaf indices.

decision_path(X)

Return the decision path in the forest.

examine_seperate_items(curr_vals, saved_vals)

Comparison of more complex structures in the super class we just check these model-specific items exist in both current and saved copies.

fit(x, y)

Do fit and then store model dict.

get_current_and_saved_models()

Makes a copy of self.__dict__ and splits it into dicts for the current and saved versions.

get_k_anonymity(x)

Calculates the k-anonymity of a random forest model as the minimum of the anonymity for each record.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Gets dictionary of parameter values restricted to those expected by base classifier.

posthoc_check()

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

predict(X)

Predict class for X.

predict_log_proba(X)

Predict class log-probabilities for X.

predict_proba(X)

Predict class probabilities for X.

preliminary_check([verbose, apply_constraints])

Checks whether current model parameters violate the safe rules.

request_release(path, ext[, target])

Saves model to filename specified and creates a report for the TRE output checkers.

run_attack([target, attack_name, ...])

Runs a specified attack on the trained model and saves a report to file.

save([name])

Writes model to file in appropriate format.

score(X, y[, sample_weight])

Return the mean accuracy on the given test data and labels.

set_fit_request(*[, x])

Request metadata passed to the fit method.

set_params(**params)

Set the parameters of this estimator.

set_score_request(*[, sample_weight])

Request metadata passed to the score method.

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

Random Forest-specific checks would benefit from refactoring into simpler blocks perhaps. NOTE that this is never called if the model has not been fitted.

fit(x: ndarray, y: ndarray) None[source]

Do fit and then store model dict.

get_k_anonymity(x: ndarray) int[source]

Calculates the k-anonymity of a random forest model as the minimum of the anonymity for each record. That is defined as the size of the set of records which appear in the same leaf as the record in every tree.

set_fit_request(*, x: bool | None | str = '$UNCHANGED$') SafeRandomForestClassifier

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

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

Metadata routing for x parameter in fit.

Returns:
selfobject

The updated object.

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

Request metadata passed to the score method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see 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.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a Pipeline. Otherwise it has no effect.

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.