Skip to content

Commit

Permalink
WIp something
Browse files Browse the repository at this point in the history
  • Loading branch information
BielStela committed Nov 23, 2018
1 parent 6e47ca9 commit 6ecc96e
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions shadow_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.base import clone


class ShadowModels:
Expand All @@ -15,20 +16,41 @@ class ShadowModels:
- Instance n models (must be similar to the target model)
- Train each model on training splited data
- Run prediction on both training and test splited data
- Label the resulting prediction vector with "in"/"out"
- Label the resulting prediction vector with "in"/"out"
if it was train or test
- drop mic
"""

def __init__(self, n_models: int, data: np.ndarray, target_classes: int):
def __init__(self, n_models: int, data: np.ndarray,
target_classes: int, learner):

self.n_models = n_models
self.data = data
self.target_classes = target_classes
self.splits = self.split_data(self.data, self.n_models)
self.splits = self._split_data(self.data, self.n_models)
self.learner = learner
self.models = self._get_models(self.learner, self.n_models)

@staticmethod
def split_data(data, n_splits, shuffle=False) ->List[np.ndarray]:
if shuffle:
def _split_data(data, n_splits, shuffle=False) -> List[np.ndarray]:
if shuffle:
np.random.shuffle(data) # shufle data just in case
splited = np.vsplit(data, n_splits)
return splited

@staticmethod
def _get_model_list(learner, n) -> List:
"""
Intances n shadow models, copies of the input parameter learner
"""
models = [clone(learner) for _ in range(n)]
return models

def fit(self):

# TRAIN
for model, data_subset in zip(self.models, self.splits):
X = data_subset[:, :-1]
y = data_subset[:, -1]
X_train, X_test, y_train, y_test = train_test_split(X, y)
model.fit(X_train, y_train)

0 comments on commit 6ecc96e

Please sign in to comment.