-
Notifications
You must be signed in to change notification settings - Fork 2
Update forest.py #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Update forest.py #15
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,11 @@ | |
| """ | ||
| Random Forest Classifier with Differential Privacy | ||
| """ | ||
|
|
||
| from sklearn.ensemble import RandomForestRegressor | ||
| from diffprivlib.mechanisms import Laplace | ||
| from diffprivlib.utils import warn_unused_args | ||
|
|
||
| from collections import namedtuple | ||
| import warnings | ||
|
|
||
|
|
@@ -612,6 +617,39 @@ def apply(self, X): | |
|
|
||
| return out | ||
|
|
||
| class DifferentiallyPrivateRandomForestRegressor(RandomForestRegressor): | ||
| def __init__(self, n_estimators=100, epsilon=1.0, bounds=None, random_state=None, **kwargs): | ||
| super().__init__(n_estimators=n_estimators, **kwargs) | ||
| self.epsilon = epsilon | ||
| self.bounds = bounds | ||
| self.random_state = random_state | ||
|
|
||
| def _dp_mean(self, array): | ||
| epsilon = self.epsilon # Privacy parameter | ||
| bounds = self.bounds if self.bounds else (np.min(array), np.max(array)) # Bounds of the array | ||
|
|
||
| # Compute sensitivity (max possible change in output when a single record is added or removed) | ||
| sensitivity = np.abs(bounds[1] - bounds[0]) / len(array) | ||
|
|
||
| # Create Laplace noise mechanism | ||
| laplace_mechanism = Laplace(epsilon=epsilon, sensitivity=sensitivity, random_state=self.random_state) | ||
|
|
||
| # Compute differentially private mean | ||
| private_mean = np.mean(array) + laplace_mechanism.sample() | ||
| return private_mean | ||
|
|
||
| def fit(self, X, y): | ||
| super().fit(X, y) # Fit the RandomForestRegressor | ||
|
||
|
|
||
| def predict(self, X): | ||
| predictions = super().predict(X) | ||
| return predictions | ||
|
|
||
| # Example usage: | ||
| # dp_rf_regressor = DifferentiallyPrivateRandomForestRegressor(n_estimators=100, epsilon=1.0) | ||
| # dp_rf_regressor.fit(X_train, y_train) | ||
| # predictions = dp_rf_regressor.predict(X_test) | ||
|
|
||
|
|
||
| class _Node: | ||
| """Base storage structure for the nodes in a _FittingTree object.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This appears to never be called....