class_docstr: Encodes categorical features.
  
  Several strategies are possible (supervised or not). Works for both
  classification and regression tasks.
  
  Parameters
  ----------
  strategy : str, default = "label_encoding"
      The strategy to encode categorical features.
      Available strategies = {"label_encoding", "dummification",
      "random_projection", entity_embedding"}
  verbose : bool, default = False
      Verbose mode. Useful for entity embedding strategy.
class_docstr_tok: ["Encodes","categorical","features",".","Several","strategies","are","possible","(","supervised","or","not",")",".","Works","for","both","classification","and","regression","tasks",".","Parameters","--","--","--","--","--","strategy",":","str",",","default","=","``","label_encoding","''","The","strategy","to","encode","categorical","features",".","Available","strategies","=","{","``","label_encoding","''",",","``","dummification","''",",","``","random_projection","''",",","entity_embedding","''","}","verbose",":","bool",",","default","=","False","Verbose","mode",".","Useful","for","entity","embedding","strategy","."]
docstr: Transform categorical variable of df dataset.
  
  Transform df DataFrame encoding categorical features with the strategy
  parameter if self.__fitOK is set to True.
  
  Parameters
  ----------
  df : pandas.Dataframe of shape = (n_train, n_features)
      The training dataset with numerical and categorical features.
      NA values are allowed.
  
  Returns
  -------
  pandas.Dataframe of shape = (n_train, n_features)
      The dataset with numerical and encoded categorical features.
docstr_tok: ["Transform","categorical","variable","of","df","dataset",".","Transform","df","DataFrame","encoding","categorical","features","with","the","strategy","parameter","if","self.__fitOK","is","set","to","True",".","Parameters","--","--","--","--","--","df",":","pandas.Dataframe","of","shape","=","(","n_train",",","n_features",")","The","training","dataset","with","numerical","and","categorical","features",".","NA","values","are","allowed",".","Returns","--","--","--","-","pandas.Dataframe","of","shape","=","(","n_train",",","n_features",")","The","dataset","with","numerical","and","encoded","categorical","features","."]
code: def transform(self, df):
      """Transform categorical variable of df dataset.
  
      Transform df DataFrame encoding categorical features with the strategy
      parameter if self.__fitOK is set to True.
  
      Parameters
      ----------
      df : pandas.Dataframe of shape = (n_train, n_features)
          The training dataset with numerical and categorical features.
          NA values are allowed.
  
      Returns
      -------
      pandas.Dataframe of shape = (n_train, n_features)
          The dataset with numerical and encoded categorical features.
  
      """
      if self.__fitOK:
          if len(self.__Lcat) == 0:
              return df[self.__Lnum]
  
          else:
              #################################################
              #                Label Encoding
              #################################################
  
              if self.strategy == "label_encoding":
                  for col in self.__Lcat:
                      # Handling unknown levels
                      unknown_levels = list(
                          set(df[col].values)
                          - set(self.__Enc[col].keys())
                      )
  
                      if len(unknown_levels) != 0:
                          new_enc = len(self.__Enc[col])
  
                          for unknown_level in unknown_levels:
                              d = self.__Enc[col]
                              # TODO: make sure no collisions introduced
                              d[unknown_level] = new_enc
                              self.__Enc[col] = d
  
                  if len(self.__Lnum) == 0:
                      return pd.concat(
                          [
                              pd.DataFrame(
                                  df[col]
                                  .apply(lambda x: self.__Enc[col][x])
                                  .values,
                                  columns=[col],
                                  index=df.index,
                              )
                              for col in self.__Lcat
                          ],
                          axis=1,
                      )[df.columns]
                  else:
                      return pd.concat(
                          [df[self.__Lnum]]
                          + [
                              pd.DataFrame(
                                  df[col]
                                  .apply(lambda x: self.__Enc[col][x])
                                  .values,
                                  columns=[col],
                                  index=df.index,
                              )
                              for col in self.__Lcat
                          ],
                          axis=1,
                      )[df.columns]
  
              #################################################
              #                 Dummification
              #################################################
  
              elif self.strategy == "dummification":
                  sub_var = []
                  missing_var = []
  
                  for col in self.__Lcat:
                      # Handling unknown and missing levels
                      unique_levels = set(df[col].values)
                      sub_levels = unique_levels & set(self.__Enc[col])
                      missing_levels = [
                          col + "_" + str(s)
                          for s in list(
                              set(self.__Enc[col]) - sub_levels
                          )
                      ]
                      sub_levels = [
                          col + "_" + str(s) for s in list(sub_levels)
                      ]
  
                      sub_var = sub_var + sub_levels
                      missing_var = missing_var + missing_levels
  
                  if len(missing_var) != 0:
                      return pd.SparseDataFrame(
                          pd.concat(
                              [
                                  pd.get_dummies(df, sparse=True)[
                                      list(self.__Lnum) + sub_var
                                  ]
                              ]
                              + [
                                  pd.DataFrame(
                                      np.zeros(
                                          (
                                              df.shape[0],
                                              len(missing_var),
                                          )
                                      ),
                                      columns=missing_var,
                                      index=df.index,
                                  )
                              ],
                              axis=1,
                          )[
                              list(self.__Lnum)
                              + sorted(missing_var + sub_var)
                          ]
                      )
  
                  else:
                      return pd.get_dummies(df, sparse=True)[
                          list(self.__Lnum) + sorted(sub_var)
                      ]
  
              #################################################
              #               Entity Embedding
              #################################################
  
              elif self.strategy == "entity_embedding":
  
                  def get_embeddings(x, col, i):
                      if (
                          int(self.__Enc[col][x])
                          < np.shape(self.__weights[i])[0]
                      ):
                          return self.__weights[i][
                              int(self.__Enc[col][x]), :
                          ]
                      return np.mean(self.__weights[i], axis=0)
  
                  for col in self.__Lcat:
                      # Handling unknown levels
                      unknown_levels = list(
                          set(df[col].values)
                          - set(self.__Enc[col].keys())
                      )
  
                      if len(unknown_levels) != 0:
                          new_enc = len(self.__Enc[col])
  
                          for unknown_level in unknown_levels:
                              d = self.__Enc[col]
                              d[unknown_level] = new_enc
                              self.__Enc[col] = d
  
                  if len(self.__Lnum) == 0:
                      return pd.concat(
                          [
                              pd.DataFrame(
                                  df[col]
                                  .apply(
                                      lambda x: get_embeddings(
                                          x, col, i
                                      )
                                  )
                                  .tolist(),
                                  columns=[
                                      col + "_emb" + str(k + 1)
                                      for k in range(self.__K[col])
                                  ],
                                  index=df.index,
                              )
                              for i, col in enumerate(self.__Lcat)
                          ],
                          axis=1,
                      )
                  else:
                      return pd.concat(
                          [df[self.__Lnum]]
                          + [
                              pd.DataFrame(
                                  df[col]
                                  .apply(
                                      lambda x: get_embeddings(
                                          x, col, i
                                      )
                                  )
                                  .tolist(),
                                  columns=[
                                      col + "_emb" + str(k + 1)
                                      for k in range(self.__K[col])
                                  ],
                                  index=df.index,
                              )
                              for i, col in enumerate(self.__Lcat)
                          ],
                          axis=1,
                      )
  
              #################################################
              #               Random Projection
              #################################################
  
              else:
                  for col in self.__Lcat:
                      unknown_levels = list(
                          set(df[col].values)
                          - set(self.__Enc[col].keys())
                      )
  
                      if len(unknown_levels) != 0:
                          new_enc = len(self.__Enc[col])
  
                          for unknown_level in unknown_levels:
                              d = self.__Enc[col]
                              d[unknown_level] = [
                                  new_enc for _ in range(self.__K[col])
                              ]
                              self.__Enc[col] = d
  
                  if len(self.__Lnum) == 0:
                      return pd.concat(
                          [
                              pd.DataFrame(
                                  df[col]
                                  .apply(lambda x: self.__Enc[col][x])
                                  .tolist(),
                                  columns=[
                                      col + "_proj" + str(k + 1)
                                      for k in range(self.__K[col])
                                  ],
                                  index=df.index,
                              )
                              for col in self.__Lcat
                          ],
                          axis=1,
                      )
                  else:
                      return pd.concat(
                          [df[self.__Lnum]]
                          + [
                              pd.DataFrame(
                                  df[col]
                                  .apply(lambda x: self.__Enc[col][x])
                                  .tolist(),
                                  columns=[
                                      col + "_proj" + str(k + 1)
                                      for k in range(self.__K[col])
                                  ],
                                  index=df.index,
                              )
                              for col in self.__Lcat
                          ],
                          axis=1,
                      )
  
      else:
          raise ValueError("Call fit or fit_transform function before")
  
file_path: MLBox/mlbox/encoding/categorical_encoder.py
imports: import numpy
  import pandas
  import warnings
  import os
  from tensorflow.keras.layers import Dense, Reshape, Dropout, Embedding, concatenate, Input
  from tensorflow.keras.models import Model
label_desc: Use image node_id 6 for calling the Categorical_encoder obj's underlying member method code with example usage: obj.transform(df) and returns: df, pd, pd, pd, pd, np, pd, pd, pd, pd, self