class_docstr: Optimized preprocessing is a preprocessing technique that learns a
  probabilistic transformation that edits the features and labels in the data
  with group fairness, individual distortion, and data fidelity constraints
  and objectives [3]_.
  
  References:
      .. [3] F. P. Calmon, D. Wei, B. Vinzamuri, K. Natesan Ramamurthy, and
         K. R. Varshney. "Optimized Pre-Processing for Discrimination
         Prevention." Conference on Neural Information Processing Systems,
         2017.
  
  Based on code available at: https://github.com/fair-preprocessing/nips2017
class_docstr_tok: ["Optimized","preprocessing","is","a","preprocessing","technique","that","learns","a","probabilistic","transformation","that","edits","the","features","and","labels","in","the","data","with","group","fairness",",","individual","distortion",",","and","data","fidelity","constraints","and","objectives","[","3","]","_.","References",":","..","[","3","]","F.","P.","Calmon",",","D.","Wei",",","B.","Vinzamuri",",","K.","Natesan","Ramamurthy",",","and","K.","R.","Varshney",".","``","Optimized","Pre-Processing","for","Discrimination","Prevention",".","''","Conference","on","Neural","Information","Processing","Systems",",","2017",".","Based","on","code","available","at",":","https",":","\/\/github.com\/fair-preprocessing\/nips2017"]
docstr: Transform the dataset to a new dataset based on the estimated
  transformation.
  
  Args:
      dataset (BinaryLabelDataset): Dataset containing labels that needs
          to be transformed.
      transform_Y (bool): Flag that mandates transformation of Y (labels).
docstr_tok: ["Transform","the","dataset","to","a","new","dataset","based","on","the","estimated","transformation",".","Args",":","dataset","(","BinaryLabelDataset",")",":","Dataset","containing","labels","that","needs","to","be","transformed",".","transform_Y","(","bool",")",":","Flag","that","mandates","transformation","of","Y","(","labels",")","."]
code: def transform(self, dataset, sep="=", transform_Y=True):
      """Transform the dataset to a new dataset based on the estimated
      transformation.
  
      Args:
          dataset (BinaryLabelDataset): Dataset containing labels that needs
              to be transformed.
          transform_Y (bool): Flag that mandates transformation of Y (labels).
      """
  
      if len(np.unique(dataset.instance_weights)) > 1:
          warn(
              "Optimized pre-processing will ignore instance_weights in "
              "the dataset during predict. The transformed dataset will "
              "have all instance weights set to 1."
          )
  
      # Convert the dataset to a dataframe and preprocess
      df, _ = dataset.convert_to_dataframe(
          de_dummy_code=True, sep=sep, set_category=True
      )
  
      # Feature names
      Y_feature_names = dataset.label_names
      D_feature_names = self.protected_attribute_names
      X_feature_names = [
          n
          for n in df.columns.tolist()
          if n not in self.Y_feature_names
          and n not in self.protected_attribute_names
      ]
  
      if (
          X_feature_names != self.X_feature_names
          or D_feature_names != self.protected_attribute_names
      ):
          raise ValueError(
              "The feature names of inputs and protected "
              "attributes must match with the training dataset."
          )
  
      if transform_Y and (Y_feature_names != self.Y_feature_names):
          raise ValueError(
              "The label names must match with that in the training dataset"
          )
  
      if transform_Y:
          # randomized mapping when Y is requested to be transformed
          dfP_withY = self.OpT.dfP.applymap(
              lambda x: 0 if x < 1e-8 else x
          )
          dfP_withY = dfP_withY.divide(dfP_withY.sum(axis=1), axis=0)
  
          df_transformed = _apply_randomized_mapping(
              df,
              dfP_withY,
              features=D_feature_names
              + X_feature_names
              + Y_feature_names,
              random_seed=self.seed,
          )
      else:
          # randomized mapping when Y is not requested to be transformed
          d1 = (
              self.OpT.dfFull.reset_index()
              .groupby(D_feature_names + X_feature_names)
              .sum()
          )
          d2 = (
              d1.transpose()
              .reset_index()
              .groupby(X_feature_names)
              .sum()
          )
          dfP_noY = d2.transpose()
          dfP_noY = dfP_noY.drop(Y_feature_names, 1)
          dfP_noY = dfP_noY.applymap(lambda x: x if x > 1e-8 else 0)
          dfP_noY = dfP_noY / dfP_noY.sum()
  
          dfP_noY = dfP_noY.divide(dfP_noY.sum(axis=1), axis=0)
  
          df_transformed = _apply_randomized_mapping(
              df,
              dfP_noY,
              features=D_feature_names + X_feature_names,
              random_seed=self.seed,
          )
  
      # Map the protected attributes to numeric values
      for idx, p in enumerate(self.protected_attribute_names):
          pmap = dataset.metadata["protected_attribute_maps"][idx]
          pmap_rev = dict(zip(pmap.values(), pmap.keys()))
          df_transformed[p] = df_transformed[p].replace(pmap_rev)
  
      # Map the labels to numeric values
      for idx, p in enumerate(Y_feature_names):
          pmap = dataset.metadata["label_maps"][idx]
          pmap_rev = dict(zip(pmap.values(), pmap.keys()))
          df_transformed[p] = df_transformed[p].replace(pmap_rev)
  
      # Dummy code and convert to a dataset
      df_dum = pd.concat(
          [
              pd.get_dummies(
                  df_transformed.loc[:, X_feature_names], prefix_sep="="
              ),
              df_transformed.loc[:, Y_feature_names + D_feature_names],
          ],
          axis=1,
      )
  
      # Create a dataset out of df_dum
      dataset_transformed = BinaryLabelDataset(
          df=df_dum,
          label_names=Y_feature_names,
          protected_attribute_names=self.protected_attribute_names,
          privileged_protected_attributes=self.privileged_protected_attributes,
          unprivileged_protected_attributes=self.unprivileged_protected_attributes,
          favorable_label=dataset.favorable_label,
          unfavorable_label=dataset.unfavorable_label,
          metadata=dataset.metadata,
      )
  
      return dataset_transformed
  
file_path: AIF360/aif360/algorithms/preprocessing/optim_preproc.py
imports: from warnings import warn
  import numpy
  import pandas
  from aif360.algorithms import Transformer
  from aif360.datasets import BinaryLabelDataset
label_desc: Use image node_id 3 for calling the OptimPreproc obj's underlying member method code with example usage: obj.transform(dataset, sep, transform_Y) and returns: dataset_transformed