mirror of
https://github.com/IBM/ai-privacy-toolkit.git
synced 2026-04-24 20:36:21 +02:00
Formatting (#68)
Fix most flake/lint errors and ignore a few others Signed-off-by: abigailt <abigailt@il.ibm.com>
This commit is contained in:
parent
b47ba24906
commit
d52fcd0041
16 changed files with 91 additions and 92 deletions
|
|
@ -6,4 +6,4 @@ from apt import anonymization
|
|||
from apt import minimization
|
||||
from apt import utils
|
||||
|
||||
__version__ = "0.1.0"
|
||||
__version__ = "0.1.0"
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ class Anonymize:
|
|||
|
||||
if dataset.features_names is not None:
|
||||
self.features_names = dataset.features_names
|
||||
else: # if no names provided, use numbers instead
|
||||
else: # if no names provided, use numbers instead
|
||||
self.features_names = self.features
|
||||
|
||||
if not set(self.quasi_identifiers).issubset(set(self.features_names)):
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ from sklearn.utils.validation import check_is_fitted
|
|||
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
from apt.utils.datasets import ArrayDataset, Data, DATA_PANDAS_NUMPY_TYPE
|
||||
from apt.utils.datasets import ArrayDataset, DATA_PANDAS_NUMPY_TYPE
|
||||
from apt.utils.models import Model, SklearnRegressor, ModelOutputType, SklearnClassifier
|
||||
|
||||
|
||||
|
|
@ -268,14 +268,14 @@ class GeneralizeToRepresentative(BaseEstimator, MetaEstimatorMixin, TransformerM
|
|||
if self.encoder is None:
|
||||
numeric_features = [f for f in self._features if f not in self.categorical_features]
|
||||
numeric_transformer = Pipeline(
|
||||
steps=[('imputer', SimpleImputer(strategy='constant', fill_value=0))]
|
||||
steps=[('imputer', SimpleImputer(strategy='constant', fill_value=0))]
|
||||
)
|
||||
categorical_transformer = OneHotEncoder(handle_unknown="ignore", sparse=False)
|
||||
self.encoder = ColumnTransformer(
|
||||
transformers=[
|
||||
("num", numeric_transformer, numeric_features),
|
||||
("cat", categorical_transformer, self.categorical_features),
|
||||
]
|
||||
transformers=[
|
||||
("num", numeric_transformer, numeric_features),
|
||||
("cat", categorical_transformer, self.categorical_features),
|
||||
]
|
||||
)
|
||||
self.encoder.fit(x)
|
||||
|
||||
|
|
@ -345,7 +345,6 @@ class GeneralizeToRepresentative(BaseEstimator, MetaEstimatorMixin, TransformerM
|
|||
print('Pruned tree to level: %d, new relative accuracy: %f' % (level, accuracy))
|
||||
level += 1
|
||||
|
||||
|
||||
# if accuracy below threshold, improve accuracy by removing features from generalization
|
||||
elif accuracy < self.target_accuracy:
|
||||
print('Improving accuracy')
|
||||
|
|
@ -599,8 +598,8 @@ class GeneralizeToRepresentative(BaseEstimator, MetaEstimatorMixin, TransformerM
|
|||
new_cell['ranges'][feature]['end'] = right_cell['ranges'][feature]['start']
|
||||
for feature in left_cell['categories'].keys():
|
||||
new_cell['categories'][feature] = \
|
||||
list(set(left_cell['categories'][feature]) |
|
||||
set(right_cell['categories'][feature]))
|
||||
list(set(left_cell['categories'][feature])
|
||||
| set(right_cell['categories'][feature]))
|
||||
for feature in left_cell['untouched']:
|
||||
if feature in right_cell['untouched']:
|
||||
new_cell['untouched'].append(feature)
|
||||
|
|
@ -707,8 +706,8 @@ class GeneralizeToRepresentative(BaseEstimator, MetaEstimatorMixin, TransformerM
|
|||
for feature in self._features:
|
||||
# if feature has a representative value in the cell and should not be left untouched,
|
||||
# take the representative value
|
||||
if feature in cells[i]['representative'] and ('untouched' not in cells[i] or
|
||||
feature not in cells[i]['untouched']):
|
||||
if feature in cells[i]['representative'] \
|
||||
and ('untouched' not in cells[i] or feature not in cells[i]['untouched']):
|
||||
representatives.loc[i, feature] = cells[i]['representative'][feature]
|
||||
# else, drop the feature (removes from representatives columns that do not have a
|
||||
# representative value or should remain untouched)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ def _load_iris(test_set_size: float = 0.3):
|
|||
|
||||
# Split training and test sets
|
||||
x_train, x_test, y_train, y_test = model_selection.train_test_split(data, labels, test_size=test_set_size,
|
||||
random_state=18, stratify=labels)
|
||||
random_state=18, stratify=labels)
|
||||
|
||||
return (x_train, y_train), (x_test, y_test)
|
||||
|
||||
|
|
@ -94,9 +94,6 @@ def get_german_credit_dataset_pd(test_set: float = 0.3):
|
|||
x_test = test.drop(["label"], axis=1)
|
||||
y_test = test.loc[:, "label"]
|
||||
|
||||
categorical_features = ["Existing_checking_account", "Credit_history", "Purpose", "Savings_account",
|
||||
"Present_employment_since", "Personal_status_sex", "debtors", "Property",
|
||||
"Other_installment_plans", "Housing", "Job"]
|
||||
x_train.reset_index(drop=True, inplace=True)
|
||||
y_train.reset_index(drop=True, inplace=True)
|
||||
x_test.reset_index(drop=True, inplace=True)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
from typing import Optional
|
||||
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import OneHotEncoder
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
tf.compat.v1.disable_eager_execution()
|
||||
|
||||
from sklearn.metrics import mean_squared_error
|
||||
|
||||
|
|
@ -16,6 +14,8 @@ from art.utils import check_and_transform_label_format
|
|||
from art.estimators.classification.keras import KerasClassifier as ArtKerasClassifier
|
||||
from art.estimators.regression.keras import KerasRegressor as ArtKerasRegressor
|
||||
|
||||
tf.compat.v1.disable_eager_execution()
|
||||
|
||||
|
||||
class KerasModel(Model):
|
||||
"""
|
||||
|
|
@ -23,7 +23,6 @@ class KerasModel(Model):
|
|||
"""
|
||||
|
||||
|
||||
|
||||
class KerasClassifier(KerasModel):
|
||||
"""
|
||||
Wrapper class for keras classification models.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from abc import ABCMeta, abstractmethod
|
||||
from typing import Any, Optional, Callable, Tuple, Union
|
||||
from typing import Any, Optional, Callable, Tuple, Union, TYPE_CHECKING
|
||||
from enum import Enum, auto
|
||||
import numpy as np
|
||||
|
||||
|
|
@ -7,6 +7,9 @@ from apt.utils.datasets import Dataset, Data, OUTPUT_DATA_ARRAY_TYPE
|
|||
from art.estimators.classification import BlackBoxClassifier
|
||||
from art.utils import check_and_transform_label_format
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import torch
|
||||
|
||||
|
||||
class ModelOutputType(Enum):
|
||||
CLASSIFIER_PROBABILITIES = auto() # vector of probabilities
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import numpy as np
|
|||
import torch
|
||||
from torch.utils.data import DataLoader, TensorDataset
|
||||
|
||||
from art.utils import check_and_transform_label_format, logger
|
||||
from art.utils import check_and_transform_label_format
|
||||
from apt.utils.datasets.datasets import PytorchData
|
||||
from apt.utils.models import Model, ModelOutputType
|
||||
from apt.utils.datasets import OUTPUT_DATA_ARRAY_TYPE
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
from typing import Optional
|
||||
|
||||
from sklearn.preprocessing import OneHotEncoder
|
||||
from sklearn.base import BaseEstimator
|
||||
|
||||
from apt.utils.models import Model, ModelOutputType, get_nb_classes, check_correct_model_output
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class XGBoostClassifier(XGBoostModel):
|
|||
:type unlimited_queries: boolean, optional
|
||||
"""
|
||||
def __init__(self, model: XGBClassifier, output_type: ModelOutputType, input_shape: Tuple[int, ...],
|
||||
nb_classes: int,black_box_access: Optional[bool] = True,
|
||||
nb_classes: int, black_box_access: Optional[bool] = True,
|
||||
unlimited_queries: Optional[bool] = True, **kwargs):
|
||||
super().__init__(model, output_type, black_box_access, unlimited_queries, **kwargs)
|
||||
self._art_model = ArtXGBoostClassifier(model, nb_features=input_shape[0], nb_classes=nb_classes)
|
||||
|
|
|
|||
|
|
@ -58,4 +58,4 @@ html_theme = 'pyramid'
|
|||
# Add any paths that contain custom static files (such as style sheets) here,
|
||||
# relative to this directory. They are copied after the builtin static files,
|
||||
# so a file named "default.css" will overwrite the builtin "default.css".
|
||||
html_static_path = ['_static']
|
||||
html_static_path = ['_static']
|
||||
|
|
|
|||
|
|
@ -21,4 +21,8 @@ python_requires = >=3.6
|
|||
|
||||
[options.packages.find]
|
||||
exclude =
|
||||
tests
|
||||
tests
|
||||
|
||||
[flake8]
|
||||
ignore = C901,W503
|
||||
per-file-ignores = __init__.py:F401
|
||||
|
|
@ -24,7 +24,7 @@ def test_anonymize_ndarray_iris():
|
|||
QI = [0, 2]
|
||||
anonymizer = Anonymize(k, QI, train_only_QI=True)
|
||||
anon = anonymizer.anonymize(ArrayDataset(x_train, pred))
|
||||
assert(len(np.unique(anon[:, QI], axis=0)) < len(np.unique(x_train[:, QI], axis=0)))
|
||||
assert (len(np.unique(anon[:, QI], axis=0)) < len(np.unique(x_train[:, QI], axis=0)))
|
||||
_, counts_elements = np.unique(anon[:, QI], return_counts=True)
|
||||
assert (np.min(counts_elements) >= k)
|
||||
assert ((np.delete(anon, QI, axis=1) == np.delete(x_train, QI, axis=1)).all())
|
||||
|
|
@ -60,7 +60,7 @@ def test_anonymize_pandas_adult():
|
|||
anonymizer = Anonymize(k, QI, categorical_features=categorical_features)
|
||||
anon = anonymizer.anonymize(ArrayDataset(x_train, pred, features))
|
||||
|
||||
assert(anon.loc[:, QI].drop_duplicates().shape[0] < x_train.loc[:, QI].drop_duplicates().shape[0])
|
||||
assert (anon.loc[:, QI].drop_duplicates().shape[0] < x_train.loc[:, QI].drop_duplicates().shape[0])
|
||||
assert (anon.loc[:, QI].value_counts().min() >= k)
|
||||
np.testing.assert_array_equal(anon.drop(QI, axis=1), x_train.drop(QI, axis=1))
|
||||
|
||||
|
|
@ -93,7 +93,7 @@ def test_anonymize_pandas_nursery():
|
|||
anonymizer = Anonymize(k, QI, categorical_features=categorical_features, train_only_QI=True)
|
||||
anon = anonymizer.anonymize(ArrayDataset(x_train, pred))
|
||||
|
||||
assert(anon.loc[:, QI].drop_duplicates().shape[0] < x_train.loc[:, QI].drop_duplicates().shape[0])
|
||||
assert (anon.loc[:, QI].drop_duplicates().shape[0] < x_train.loc[:, QI].drop_duplicates().shape[0])
|
||||
assert (anon.loc[:, QI].value_counts().min() >= k)
|
||||
np.testing.assert_array_equal(anon.drop(QI, axis=1), x_train.drop(QI, axis=1))
|
||||
|
||||
|
|
@ -112,7 +112,7 @@ def test_regression():
|
|||
print('Base model accuracy (R2 score): ', model.score(x_test, y_test))
|
||||
model.fit(anon, y_train)
|
||||
print('Base model accuracy (R2 score) after anonymization: ', model.score(x_test, y_test))
|
||||
assert(len(np.unique(anon[:, QI], axis=0)) < len(np.unique(x_train[:, QI], axis=0)))
|
||||
assert (len(np.unique(anon[:, QI], axis=0)) < len(np.unique(x_train[:, QI], axis=0)))
|
||||
_, counts_elements = np.unique(anon[:, QI], return_counts=True)
|
||||
assert (np.min(counts_elements) >= k)
|
||||
assert ((np.delete(anon, QI, axis=1) == np.delete(x_train, QI, axis=1)).all())
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from apt.utils.datasets import Data, DatasetWithPredictions
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import pytest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from numpy.testing import assert_almost_equal
|
||||
|
||||
from sklearn.compose import ColumnTransformer
|
||||
|
||||
|
|
@ -17,9 +16,8 @@ from tensorflow.keras.layers import Dense, Input
|
|||
from apt.minimization import GeneralizeToRepresentative
|
||||
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
||||
from apt.utils.dataset_utils import get_iris_dataset_np, get_adult_dataset_pd, get_german_credit_dataset_pd
|
||||
from apt.utils.datasets import ArrayDataset, Data
|
||||
from apt.utils.models import SklearnClassifier, ModelOutputType, SklearnRegressor, KerasClassifier, \
|
||||
BlackboxClassifierPredictions
|
||||
from apt.utils.datasets import ArrayDataset
|
||||
from apt.utils.models import SklearnClassifier, ModelOutputType, SklearnRegressor, KerasClassifier
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -50,7 +48,7 @@ def test_minimizer_params(data):
|
|||
|
||||
gen = GeneralizeToRepresentative(model, cells=cells)
|
||||
gen.fit()
|
||||
transformed = gen.transform(dataset=ArrayDataset(X, features_names=features))
|
||||
gen.transform(dataset=ArrayDataset(X, features_names=features))
|
||||
|
||||
|
||||
def test_minimizer_fit(data):
|
||||
|
|
@ -87,8 +85,8 @@ def test_minimizer_fit(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
modified_features = [f for f in features if
|
||||
f in expected_generalizations['categories'].keys() or f in expected_generalizations[
|
||||
|
|
@ -162,8 +160,8 @@ def test_minimizer_fit_pandas(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
modified_features = [f for f in features if
|
||||
f in expected_generalizations['categories'].keys() or f in expected_generalizations[
|
||||
|
|
@ -172,7 +170,7 @@ def test_minimizer_fit_pandas(data):
|
|||
ncp = gen.ncp
|
||||
if len(expected_generalizations['ranges'].keys()) > 0 or len(expected_generalizations['categories'].keys()) > 0:
|
||||
assert (ncp > 0)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) == False)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) is False)
|
||||
|
||||
rel_accuracy = model.score(ArrayDataset(preprocessor.transform(transformed), predictions))
|
||||
assert ((rel_accuracy >= target_accuracy) or (target_accuracy - rel_accuracy) <= 0.05)
|
||||
|
|
@ -279,8 +277,8 @@ def test_minimizer_fit_QI(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2], axis=1) == np.delete(X, [0, 2], axis=1)).all())
|
||||
modified_features = [f for f in features if
|
||||
|
|
@ -357,8 +355,8 @@ def test_minimizer_fit_pandas_QI(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
# assert (transformed.drop(QI, axis=1).equals(X.drop(QI, axis=1)))
|
||||
np.testing.assert_array_equal(transformed.drop(QI, axis=1), X.drop(QI, axis=1))
|
||||
|
|
@ -370,7 +368,7 @@ def test_minimizer_fit_pandas_QI(data):
|
|||
ncp = gen.ncp
|
||||
if len(expected_generalizations['ranges'].keys()) > 0 or len(expected_generalizations['categories'].keys()) > 0:
|
||||
assert (ncp > 0)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) == False)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) is False)
|
||||
|
||||
rel_accuracy = model.score(ArrayDataset(preprocessor.transform(transformed), predictions))
|
||||
assert ((rel_accuracy >= target_accuracy) or (target_accuracy - rel_accuracy) <= 0.05)
|
||||
|
|
@ -398,8 +396,8 @@ def test_minimize_ndarray_iris():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2], axis=1) == np.delete(x_train, [0, 2], axis=1)).all())
|
||||
|
||||
|
|
@ -478,8 +476,8 @@ def test_minimize_pandas_adult():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
# assert (transformed.drop(QI, axis=1).equals(x_train.drop(QI, axis=1)))
|
||||
np.testing.assert_array_equal(transformed.drop(QI, axis=1), x_train.drop(QI, axis=1))
|
||||
|
|
@ -492,7 +490,7 @@ def test_minimize_pandas_adult():
|
|||
ncp = gen.ncp
|
||||
if len(expected_generalizations['ranges'].keys()) > 0 or len(expected_generalizations['categories'].keys()) > 0:
|
||||
assert (ncp > 0)
|
||||
assert (((transformed[modified_features]).equals(x_train[modified_features])) == False)
|
||||
assert (((transformed[modified_features]).equals(x_train[modified_features])) is False)
|
||||
|
||||
rel_accuracy = model.score(ArrayDataset(preprocessor.transform(transformed), predictions))
|
||||
assert ((rel_accuracy >= target_accuracy) or (target_accuracy - rel_accuracy) <= 0.05)
|
||||
|
|
@ -538,8 +536,10 @@ def test_german_credit_pandas():
|
|||
transformed = gen.transform(dataset=ArrayDataset(x_train))
|
||||
gener = gen.generalizations
|
||||
expected_generalizations = {'ranges': {'Duration_in_month': [31.5]},
|
||||
'categories': {'Credit_history': [['A30', 'A32', 'A31', 'A34', 'A33']], 'Purpose': [
|
||||
['A41', 'A46', 'A43', 'A40', 'A44', 'A410', 'A49', 'A45', 'A48', 'A42']],
|
||||
'categories': {'Credit_history': [['A30', 'A32', 'A31', 'A34', 'A33']],
|
||||
'Purpose': [
|
||||
['A41', 'A46', 'A43', 'A40', 'A44', 'A410', 'A49', 'A45', 'A48',
|
||||
'A42']],
|
||||
'debtors': [['A101', 'A102', 'A103']],
|
||||
'Property': [['A124', 'A121', 'A122', 'A123']],
|
||||
'Other_installment_plans': [['A142', 'A141', 'A143']],
|
||||
|
|
@ -554,8 +554,8 @@ def test_german_credit_pandas():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
# assert (transformed.drop(QI, axis=1).equals(x_train.drop(QI, axis=1)))
|
||||
np.testing.assert_array_equal(transformed.drop(QI, axis=1), x_train.drop(QI, axis=1))
|
||||
|
|
@ -568,7 +568,7 @@ def test_german_credit_pandas():
|
|||
ncp = gen.ncp
|
||||
if len(expected_generalizations['ranges'].keys()) > 0 or len(expected_generalizations['categories'].keys()) > 0:
|
||||
assert (ncp > 0)
|
||||
assert (((transformed[modified_features]).equals(x_train[modified_features])) == False)
|
||||
assert (((transformed[modified_features]).equals(x_train[modified_features])) is False)
|
||||
|
||||
rel_accuracy = model.score(ArrayDataset(preprocessor.transform(transformed), predictions))
|
||||
assert ((rel_accuracy >= target_accuracy) or (target_accuracy - rel_accuracy) <= 0.05)
|
||||
|
|
@ -626,8 +626,8 @@ def test_regression():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2, 5, 8], axis=1) == np.delete(x_train, [0, 2, 5, 8], axis=1)).all())
|
||||
|
||||
|
|
@ -681,8 +681,8 @@ def test_X_y(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2], axis=1) == np.delete(X, [0, 2], axis=1)).all())
|
||||
modified_features = [f for f in features if
|
||||
|
|
@ -735,8 +735,8 @@ def test_X_y_features_names(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2], axis=1) == np.delete(X, [0, 2], axis=1)).all())
|
||||
modified_features = [f for f in features if
|
||||
|
|
@ -811,8 +811,8 @@ def test_BaseEstimator_classification(data):
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
# assert (transformed.drop(QI, axis=1).equals(X.drop(QI, axis=1)))
|
||||
np.testing.assert_array_equal(transformed.drop(QI, axis=1), X.drop(QI, axis=1))
|
||||
|
|
@ -824,7 +824,7 @@ def test_BaseEstimator_classification(data):
|
|||
ncp = gen.ncp
|
||||
if len(expected_generalizations['ranges'].keys()) > 0 or len(expected_generalizations['categories'].keys()) > 0:
|
||||
assert (ncp > 0)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) == False)
|
||||
assert (((transformed[modified_features]).equals(X[modified_features])) is False)
|
||||
|
||||
rel_accuracy = model.score(preprocessor.transform(transformed), predictions)
|
||||
assert ((rel_accuracy >= target_accuracy) or (target_accuracy - rel_accuracy) <= 0.05)
|
||||
|
|
@ -881,8 +881,8 @@ def test_BaseEstimator_regression():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
assert ((np.delete(transformed, [0, 2, 5, 8], axis=1) == np.delete(x_train, [0, 2, 5, 8], axis=1)).all())
|
||||
|
||||
|
|
@ -961,6 +961,6 @@ def test_untouched():
|
|||
for key in expected_generalizations['ranges']:
|
||||
assert (set(expected_generalizations['ranges'][key]) == set(gener['ranges'][key]))
|
||||
for key in expected_generalizations['categories']:
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]]) ==
|
||||
set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set([frozenset(sl) for sl in expected_generalizations['categories'][key]])
|
||||
== set([frozenset(sl) for sl in gener['categories'][key]]))
|
||||
assert (set(expected_generalizations['untouched']) == set(gener['untouched']))
|
||||
|
|
|
|||
|
|
@ -27,10 +27,10 @@ def test_sklearn_classifier():
|
|||
test = ArrayDataset(x_test, y_test)
|
||||
model.fit(train)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert(0.0 <= score <= 1.0)
|
||||
assert (0.0 <= score <= 1.0)
|
||||
|
||||
|
||||
def test_sklearn_regressor():
|
||||
|
|
@ -43,7 +43,7 @@ def test_sklearn_regressor():
|
|||
pred = model.predict(test)
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
model.score(test)
|
||||
|
||||
|
||||
def test_keras_classifier():
|
||||
|
|
@ -63,10 +63,10 @@ def test_keras_classifier():
|
|||
test = ArrayDataset(x_test, y_test)
|
||||
model.fit(train)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert(0.0 <= score <= 1.0)
|
||||
assert (0.0 <= score <= 1.0)
|
||||
|
||||
|
||||
def test_keras_regressor():
|
||||
|
|
@ -88,7 +88,7 @@ def test_keras_regressor():
|
|||
pred = model.predict(test)
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
model.score(test)
|
||||
|
||||
|
||||
def test_xgboost_classifier():
|
||||
|
|
@ -99,10 +99,10 @@ def test_xgboost_classifier():
|
|||
train = ArrayDataset(x_train, y_train)
|
||||
test = ArrayDataset(x_test, y_test)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert(0.0 <= score <= 1.0)
|
||||
assert (0.0 <= score <= 1.0)
|
||||
|
||||
model.fit(train)
|
||||
|
||||
|
|
@ -115,10 +115,10 @@ def test_blackbox_classifier():
|
|||
data = Data(train, test)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert(score == 1.0)
|
||||
assert (score == 1.0)
|
||||
|
||||
assert model.model_type is None
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ def test_blackbox_classifier_predictions():
|
|||
data = Data(train, test)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
assert model.model_type is None
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
|
|
@ -146,10 +146,10 @@ def test_blackbox_classifier_predictions_y():
|
|||
data = Data(train, test)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert(score == 1.0)
|
||||
assert (score == 1.0)
|
||||
|
||||
assert model.model_type is None
|
||||
|
||||
|
|
@ -161,7 +161,7 @@ def test_blackbox_classifier_mismatch():
|
|||
test = ArrayDataset(x_test, y_test)
|
||||
data = Data(train, test)
|
||||
with pytest.raises(ValueError):
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_PROBABILITIES)
|
||||
BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_PROBABILITIES)
|
||||
|
||||
|
||||
def test_blackbox_classifier_no_test():
|
||||
|
|
@ -172,7 +172,7 @@ def test_blackbox_classifier_no_test():
|
|||
data = Data(train)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(train)
|
||||
assert(pred.shape[0] == x_train.shape[0])
|
||||
assert (pred.shape[0] == x_train.shape[0])
|
||||
|
||||
score = model.score(train)
|
||||
assert (score == 1.0)
|
||||
|
|
@ -189,7 +189,7 @@ def test_blackbox_classifier_no_train():
|
|||
data = Data(test=test)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(test)
|
||||
assert(pred.shape[0] == x_test.shape[0])
|
||||
assert (pred.shape[0] == x_test.shape[0])
|
||||
|
||||
score = model.score(test)
|
||||
assert (score == 1.0)
|
||||
|
|
@ -207,7 +207,7 @@ def test_blackbox_classifier_no_test_y():
|
|||
data = Data(train, test)
|
||||
model = BlackboxClassifierPredictions(data, ModelOutputType.CLASSIFIER_SCALAR)
|
||||
pred = model.predict(train)
|
||||
assert(pred.shape[0] == x_train.shape[0])
|
||||
assert (pred.shape[0] == x_train.shape[0])
|
||||
|
||||
score = model.score(train)
|
||||
assert (score == 1.0)
|
||||
|
|
@ -216,11 +216,12 @@ def test_blackbox_classifier_no_test_y():
|
|||
unable_to_predict_test = False
|
||||
try:
|
||||
model.predict(test)
|
||||
except BaseException:
|
||||
except BaseException:
|
||||
unable_to_predict_test = True
|
||||
|
||||
assert unable_to_predict_test
|
||||
|
||||
|
||||
def test_blackbox_classifier_no_train_y():
|
||||
(x_train, _), (x_test, y_test) = dataset_utils.get_iris_dataset_np()
|
||||
|
||||
|
|
@ -243,6 +244,7 @@ def test_blackbox_classifier_no_train_y():
|
|||
|
||||
assert unable_to_predict_train
|
||||
|
||||
|
||||
def test_blackbox_classifier_probabilities():
|
||||
(x_train, _), (_, _) = dataset_utils.get_iris_dataset_np()
|
||||
y_train = np.array([[0.23, 0.56, 0.21] for i in range(105)])
|
||||
|
|
@ -300,7 +302,7 @@ def test_is_one_hot():
|
|||
(_, y_train), (_, _) = dataset_utils.get_iris_dataset_np()
|
||||
|
||||
assert (not is_one_hot(y_train))
|
||||
assert (not is_one_hot(y_train.reshape(-1,1)))
|
||||
assert (not is_one_hot(y_train.reshape(-1, 1)))
|
||||
assert (is_one_hot(to_categorical(y_train)))
|
||||
|
||||
|
||||
|
|
@ -314,7 +316,7 @@ def test_get_nb_classes():
|
|||
assert (nb_classes_test == 3)
|
||||
|
||||
# shape: (x,1) - not 1-hot
|
||||
nb_classes_test = get_nb_classes(y_test.reshape(-1,1))
|
||||
nb_classes_test = get_nb_classes(y_test.reshape(-1, 1))
|
||||
assert (nb_classes_test == 3)
|
||||
|
||||
# shape: (x,3) - 1-hot
|
||||
|
|
@ -326,4 +328,3 @@ def test_get_nb_classes():
|
|||
y_test[y_test == 0] = 4
|
||||
nb_classes = get_nb_classes(y_test)
|
||||
assert (nb_classes == 5)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
import numpy as np
|
||||
import torch
|
||||
from torch import nn, optim
|
||||
|
||||
from apt.utils.datasets import ArrayDataset
|
||||
from apt.utils.datasets.datasets import PytorchData
|
||||
from apt.utils.models import ModelOutputType
|
||||
from apt.utils.models.pytorch_model import PyTorchClassifier
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue