Skip to content

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
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions sklearn_pandas/dataframe_mapper.py
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_ += [
Copy link
Collaborator

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 and transformers at self.built_features, and can get the names from self.transformed_names_.

(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_:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be replaced by:

for built_feature, transformed_cols in zip(self.built_features, self.transformed_names_):
    transformed_cols = self.get_names(columns, transformers, X, alias)
    columns, transformers, _ = built_feature

# 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
33 changes: 32 additions & 1 deletion tests/test_dataframe_mapper.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import (
Imputer, StandardScaler, OneHotEncoder, LabelBinarizer)
Imputer, StandardScaler, OneHotEncoder, LabelBinarizer, LabelEncoder)
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.base import BaseEstimator, TransformerMixin
import sklearn.decomposition
@@ -829,3 +829,34 @@ def test_direct_cross_validation(iris_dataframe):
scores = sklearn_cv_score(pipeline, data, labels)
assert scores.mean() > 0.96
assert (scores.std() * 2) < 0.04


def test_inverse_transform_simple():
df = pd.DataFrame({'colA': list('ynyyn'), 'colB': list('abcab')})
mapper = DataFrameMapper([
('colA', LabelEncoder()),
('colB', LabelEncoder()),
])

transformed = mapper.fit_transform(df)
restored = mapper.inverse_transform(transformed)

assert isinstance(restored, pd.DataFrame)
assert restored.equals(df)


def test_inverse_transform_multicolumn():
df = pd.DataFrame({'colA': list('ynyyn'),
'colB': list('abcab'),
'colC': list('sttts')})
mapper = DataFrameMapper([
('colA', LabelEncoder()),
('colB', LabelBinarizer()),
('colC', LabelEncoder()),
])

transformed = mapper.fit_transform(df)
restored = mapper.inverse_transform(transformed)

assert isinstance(restored, pd.DataFrame)
assert restored.equals(df)