Skip to content
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

Genetic Synthetic Data #570

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions synth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Differentially private synthesizers for tabular data. Package includes:
* PATE-CTGAN
* PATE-GAN
* AIM
* GSD

## Installation

Expand Down
1 change: 1 addition & 0 deletions synth/samples/gsd_sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
downloaded_datasets
676 changes: 676 additions & 0 deletions synth/samples/gsd_sample/All on sklearn.ipynb

Large diffs are not rendered by default.

655 changes: 655 additions & 0 deletions synth/samples/gsd_sample/GSD Adult.ipynb

Large diffs are not rendered by default.

845 changes: 845 additions & 0 deletions synth/samples/gsd_sample/GSD test.ipynb

Large diffs are not rendered by default.

692 changes: 692 additions & 0 deletions synth/samples/gsd_sample/GSD vs AIM using Adult.ipynb

Large diffs are not rendered by default.

1,946 changes: 1,946 additions & 0 deletions synth/samples/gsd_sample/MNIST GSD (Image Data).ipynb

Large diffs are not rendered by default.

1,280 changes: 1,280 additions & 0 deletions synth/samples/gsd_sample/MNIST GSD 2 (Image Data).ipynb

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions synth/samples/gsd_sample/datasets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"nursery": {
"name": "nursery",
"url": "https://archive.ics.uci.edu/ml/machine-learning-databases/nursery/nursery.data",
"categorical_columns": "parents,has_nurs,form,children,housing,finance,social,health",
"columns":"parents,has_nurs,form,children,housing,finance,social,health",
"target": "health",
"header": "f",
"sep": ",",
"imbalanced": "f"
},
"wine": {
"name": "wine",
"url": "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv",
"categorical_columns": "quality",
"columns":"fixed,volatile,citric,residual,chlorides,free,total,density,ph,sulphates,alc,quality",
"target": "quality",
"header": "t",
"sep": ";",
"imbalanced": "f"
},
"mushroom": {
"name": "mushroom",
"url": "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data",
"categorical_columns": "edible,cap-shape,cap-surface,cap-color,bruises,odor,gill-attach,gill-spacing,gill-size,gill-color,stalk-shape,stalk-root,stalk-sar,stalk-sbr,stalk-car,stalk-cbr,veil-type,veil-color,ring-number,ring-type,spore-print,population,habitat",
"columns":"edible,cap-shape,cap-surface,cap-color,bruises,odor,gill-attach,gill-spacing,gill-size,gill-color,stalk-shape,stalk-root,stalk-sar,stalk-sbr,stalk-car,stalk-cbr,veil-type,veil-color,ring-number,ring-type,spore-print,population,habitat",
"target": "edible",
"header": "f",
"sep": ",",
"imbalanced": "f"
},
"car": {
"name": "car",
"url": "https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data",
"categorical_columns": "buying,maint,doors,persons,lug_boot,safety,class",
"columns":"buying,maint,doors,persons,lug_boot,safety,class",
"target": "class",
"header": "f",
"sep": ",",
"imbalanced": "f"
},
"adult": {
"name": "adult",
"url": "http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",
"categorical_columns": "workclass,education,marital-status,occupation,relationship,race,sex,native-country,earning-class",
"columns":"age,workclass,fnlwgt,education,education-num,marital-status,occupation,relationship,race,sex,capital-gain,capital-loss,hours-per-week,native-country,earning-class",
"target": "earning-class",
"header": "f",
"sep": ",",
"imbalanced": "t"
}
}
79 changes: 79 additions & 0 deletions synth/samples/gsd_sample/load_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os.path

import numpy as np
import pandas as pd

# NOTE: Temporary
# We add a memory cap here for now, which
# forces a subsampling of particularly large
# datasets in order to not overwhelm
# joblib
# MEM_CAP = 1500000 # 500KB

def load_data(req_datasets):
"""
Takes in optional dataset list. Otherwise grabs them
from the conf.py file.

Returns a dictionary of datasets.
{
'dset': pd.DataFrame,
'dset': pd.DataFrame
}
"""
import requests
import io
import json

os.makedirs('downloaded_datasets', exist_ok=True)
with open('datasets.json') as j:
dsets = j.read()
archive = json.loads(dsets)

loaded_datasets = {}

def retrieve_dataset(dataset):
r = requests.get(dataset['url'])
if r.ok:
data = r.content.decode('utf8')
sep = dataset['sep']
df = pd.read_csv(io.StringIO(data), names=dataset['columns'].split(','), sep=sep, index_col=False)
if dataset['header'] == "t":
df = df.iloc[1:]
return df

raise "Unable to retrieve dataset: " + dataset

def select_column(scol):
# Zero indexed, inclusive
return scol.split(',')

def encode_categorical(df,dataset):
from sklearn.preprocessing import LabelEncoder

encoders = {}
if dataset['categorical_columns'] != "":
for column in select_column(dataset['categorical_columns']):
encoders[column] = LabelEncoder()
df[column] = encoders[column].fit_transform(df[column])

df = df.apply(pd.to_numeric, errors='ignore')
data_mem = df.memory_usage(index=True).sum()
print("Memory consumed by " + dataset['name'] + ":" + str(data_mem))
return {"data": df, "target": dataset['target'], "name": dataset['name'], "categorical_columns": dataset['categorical_columns']}

for d in req_datasets:
data_name = archive[d]['name']
data_path = f"downloaded_datasets/{data_name}.csv"
if os.path.exists(data_path):
print(f'loading {data_path}')
df = pd.read_csv(data_path, index_col=0)
else:
df = retrieve_dataset(archive[d])
print(f'saving {data_path}')
df.to_csv(data_path)
encoded_df_dict = encode_categorical(df, archive[d])
loaded_datasets[d] = encoded_df_dict

# Return dictionary of pd dataframes
return loaded_datasets
6 changes: 6 additions & 0 deletions synth/samples/gsd_sample/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pandas
numpy
matplotlib
scikit-learn
skimage
opendp-smartnoise
55 changes: 55 additions & 0 deletions synth/samples/gsd_sample/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pandas as pd


def test_real_vs_synthetic_data(real, synthetic: pd.DataFrame, test_data, model,
categorical_features,
tsne=False):
import numpy as np
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

X_real = real.iloc[:, :-1]
y_real = real.iloc[:, -1]
X_synth = synthetic.iloc[:, :-1]
y_synth = synthetic.iloc[:, -1]

X_test = test_data.iloc[:, :-1]
y_test = test_data.iloc[:, -1]

model_real = model()
model_real.fit(X_real, y_real)

model_fake = model()
model_fake.fit(X_synth, y_synth)

#Test the model
predictions = model_real.predict(X_test)
print()
print('Trained on Real Data')
print(classification_report(y_test, predictions))
print('Accuracy real: ' + str(accuracy_score(y_test, predictions)))

predictions = model_fake.predict(X_test)
print()
print('Trained on Synthetic Data')
print(classification_report(y_test, predictions))
print('Accuracy synthetic: ' + str(accuracy_score(y_test, predictions)))

# TSNE Plot
if tsne:
from sklearn.manifold import TSNE
comb = np.vstack((X_real[:500], X_synth[:500]))
embedding_1 = TSNE(n_components=2, perplexity=5.0, early_exaggeration=1.0).fit_transform(comb)
x,y = embedding_1.T
l = int(len(x) / 2)
inds = []

plt.rcParams["figure.figsize"] = (15,15)
plt.scatter(x,y,c=['purple' if i in inds else 'red' for i in range(l)]+['purple' if j in inds else 'blue' for j in range(l)])
plt.gca().legend(('Real Data','Real'))
plt.title('TSNE Plot, Real Data vs. Synthetic')
plt.show()

return model_real, model_fake

4 changes: 4 additions & 0 deletions synth/snsynth/gsd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .gsd import GSDSynthesizer


__all__ = ["GSDSynthesizer"]
Loading