@@ -110,6 +110,7 @@ def __init__(self, features, default=False, sparse=False, df_out=False,
110
110
self .df_out = df_out
111
111
self .input_df = input_df
112
112
self .transformed_names_ = []
113
+ self .transformed_cols_ = []
113
114
114
115
if (df_out and (sparse or default )):
115
116
raise ValueError ("Can not use df_out with sparse or default" )
@@ -268,6 +269,7 @@ def transform(self, X):
268
269
"""
269
270
extracted = []
270
271
self .transformed_names_ = []
272
+ self .transformed_cols_ = []
271
273
for columns , transformers , options in self .built_features :
272
274
input_df = options .get ('input_df' , self .input_df )
273
275
# columns could be a string or list of
@@ -282,6 +284,10 @@ def transform(self, X):
282
284
alias = options .get ('alias' )
283
285
self .transformed_names_ += self .get_names (
284
286
columns , transformers , Xt , alias )
287
+
288
+ self .transformed_cols_ += [
289
+ (columns , transformers ,
290
+ self .get_names (columns , transformers , Xt , alias )) ]
285
291
286
292
# handle features not explicitly selected
287
293
if self .built_default is not False :
@@ -328,3 +334,34 @@ def transform(self, X):
328
334
index = index )
329
335
else :
330
336
return stacked
337
+
338
+
339
+ def inverse_transform (self , X ):
340
+ """
341
+ Inverse transform the given data. Assumes that fit has already been called.
342
+
343
+ X the data to inverse transform
344
+ """
345
+
346
+ X_inv = pd .DataFrame ()
347
+ # We will populate the inverse transformed dataframe column by column
348
+
349
+ # Let's keep track of the column we've processed
350
+ prev_col = 0
351
+ for columns , transformers , transformed_cols in self .transformed_cols_ :
352
+ # Determine the column number of the last column in X corresponding to
353
+ # the original column we're computing
354
+ last_col = prev_col + len (transformed_cols )
355
+
356
+ # Inverse transform the columns in X for the current transformer
357
+ col_inv = pd .DataFrame (transformers .inverse_transform (X [:, prev_col :last_col ]),
358
+ columns = [columns ])
359
+
360
+ # Append the inverse transformed column to the output data frame
361
+ X_inv = pd .concat ([X_inv , col_inv ], axis = 1 )
362
+
363
+ # For the next iteration, update the last column processed
364
+ prev_col = last_col
365
+
366
+
367
+ return X_inv
0 commit comments