ctab.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from library.interfaces import GanBaseClass
  2. from library.dataset import DataSet
  3. from model.synthesizer.ctabgan_synthesizer import CTABGANSynthesizer
  4. import pandas as pd
  5. import warnings
  6. warnings.filterwarnings("ignore")
  7. class CtabGan(GanBaseClass):
  8. """
  9. This is a toy example of a GAN.
  10. It repeats the first point of the training-data-set.
  11. """
  12. def __init__(self, epochs=10, debug=True):
  13. self.isTrained = False
  14. self.epochs = epochs
  15. self.canPredict = False
  16. def reset(self, _dataSet):
  17. """
  18. Resets the trained GAN to an random state.
  19. """
  20. self.isTrained = False
  21. self.synthesizer = CTABGANSynthesizer(epochs = self.epochs)
  22. def train(self, dataSet):
  23. """
  24. Trains the GAN.
  25. It stores the data points in the training data set and mark as trained.
  26. *dataSet* is a instance of /library.dataset.DataSet/. It contains the training dataset.
  27. We are only interested in the first *maxListSize* points in class 1.
  28. """
  29. if dataSet.data1.shape[0] <= 0:
  30. raise AttributeError("Train: Expected data class 1 to contain at least one point.")
  31. self.synthesizer.fit(train_data=pd.DataFrame(dataSet.data1))
  32. self.isTrained = True
  33. def generateDataPoint(self):
  34. """
  35. Returns one synthetic data point by repeating the stored list.
  36. """
  37. return (self.generateData(1))[0]
  38. def generateData(self, numOfSamples=1):
  39. """
  40. Generates a list of synthetic data-points.
  41. *numOfSamples* is a integer > 0. It gives the number of new generated samples.
  42. """
  43. if not self.isTrained:
  44. raise ValueError("Try to generate data with untrained Re.")
  45. return self.synthesizer.sample(numOfSamples)