-
Notifications
You must be signed in to change notification settings - Fork 418
DataFrameMapper.inverse_transform() for simple transformations #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+69
−1
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,7 @@ def __init__(self, features, default=False, sparse=False, df_out=False, | |
self.df_out = df_out | ||
self.input_df = input_df | ||
self.transformed_names_ = [] | ||
self.transformed_cols_ = [] | ||
|
||
if (df_out and (sparse or default)): | ||
raise ValueError("Can not use df_out with sparse or default") | ||
|
@@ -268,6 +269,7 @@ def transform(self, X): | |
""" | ||
extracted = [] | ||
self.transformed_names_ = [] | ||
self.transformed_cols_ = [] | ||
for columns, transformers, options in self.built_features: | ||
input_df = options.get('input_df', self.input_df) | ||
# columns could be a string or list of | ||
|
@@ -283,6 +285,10 @@ def transform(self, X): | |
self.transformed_names_ += self.get_names( | ||
columns, transformers, Xt, alias) | ||
|
||
self.transformed_cols_ += [ | ||
(columns, transformers, | ||
self.get_names(columns, transformers, Xt, alias))] | ||
|
||
# handle features not explicitly selected | ||
if self.built_default is not False: | ||
unsel_cols = self._unselected_columns(X) | ||
|
@@ -328,3 +334,34 @@ def transform(self, X): | |
index=index) | ||
else: | ||
return stacked | ||
|
||
def inverse_transform(self, X): | ||
""" | ||
Inverse transform the given data. Assumes that fit has already been | ||
called. | ||
X the data to inverse transform | ||
""" | ||
|
||
X_inv = pd.DataFrame() | ||
# We will populate the inverse transformed dataframe column by column | ||
|
||
# Let's keep track of the column we've processed | ||
prev_col = 0 | ||
for columns, transformers, transformed_cols in self.transformed_cols_: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be replaced by:
|
||
# Determine the column number of the last column in X | ||
# corresponding to the original column we're computing | ||
last_col = prev_col + len(transformed_cols) | ||
|
||
# Inverse transform the columns in X for the current transformer | ||
col_inv = pd.DataFrame(transformers.inverse_transform( | ||
X[:, prev_col:last_col]), | ||
columns=[columns]) | ||
|
||
# Append the inverse transformed column to the output data frame | ||
X_inv = pd.concat([X_inv, col_inv], axis=1) | ||
|
||
# For the next iteration, update the last column processed | ||
prev_col = last_col | ||
|
||
return X_inv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we really need to store this. We already have the
columns
andtransformers
atself.built_features
, and can get the names fromself.transformed_names_
.