Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from copy import deepcopy, copy
  4. from tqdm import tqdm
  5.  
  6. class FeatureExpander:
  7. def __init__(self, logs=False, squares=False):
  8. self.logs = logs
  9. self.squares = squares
  10.  
  11.  
  12. def __add_logs(self, X, fit_mode=False):
  13. if self.logs:
  14. X = deepcopy(X)
  15. if fit_mode:
  16. self.mins = np.min(X)
  17. added_cols = []
  18. cols = copy(X.columns)
  19. for col in tqdm(cols, position=0):
  20. X[col + '_log'] = np.nan_to_num(np.log(X[col] + self.mins[col] + 2))
  21. added_cols.append(col + '_log')
  22. return X[added_cols]
  23. return None
  24.  
  25.  
  26. def __add_sqrs(self, X, fit_mode=False):
  27. if self.squares:
  28. X = deepcopy(X)
  29. added_cols = []
  30. cols = copy(X.columns)
  31. for col1 in tqdm(cols, position=0):
  32. for col2 in cols:
  33. X[col1 + '_X_' + col2] = X[col1] * X[col2]
  34. X[col1 + '_/_' + col2] = X[col1] / X[col2]
  35. added_cols.append(col1 + '_X_' + col2)
  36. added_cols.append(col1 + '_/_' + col2)
  37. return X[added_cols]
  38. return None
  39.  
  40.  
  41. def fit(self, X, y=None):
  42. return pd.concat((X, self.__add_logs(X, True), self.__add_sqrs(X, True)), axis=1)
  43.  
  44.  
  45. def transform(self, X):
  46. return pd.concat((X, self.__add_logs(X), self.__add_sqrs(X)), axis=1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement