This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Add initial implementation of DatasetTransformer. #240
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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
49 changes: 49 additions & 0 deletions
49
src/python/nimbusml/internal/core/preprocessing/datasettransformer.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
# -------------------------------------------------------------------------------------------- | ||
# - Generated by tools/entrypoint_compiler.py: do not edit by hand | ||
""" | ||
DatasetTransformer | ||
""" | ||
|
||
__all__ = ["DatasetTransformer"] | ||
|
||
|
||
from ...entrypoints.models_datasettransformer import models_datasettransformer | ||
from ...utils.utils import trace | ||
from ..base_pipeline_item import BasePipelineItem, DefaultSignature | ||
|
||
|
||
class DatasetTransformer(BasePipelineItem, DefaultSignature): | ||
""" | ||
**Description** | ||
Applies a TransformModel to a dataset. | ||
|
||
:param transform_model: Transform model. | ||
|
||
:param params: Additional arguments sent to compute engine. | ||
|
||
""" | ||
|
||
@trace | ||
def __init__( | ||
self, | ||
transform_model, | ||
**params): | ||
BasePipelineItem.__init__( | ||
self, type='transform', **params) | ||
|
||
self.transform_model = transform_model | ||
|
||
@property | ||
def _entrypoint(self): | ||
return models_datasettransformer | ||
|
||
@trace | ||
def _get_node(self, **all_args): | ||
algo_args = dict( | ||
transform_model=self.transform_model) | ||
|
||
all_args.update(algo_args) | ||
return self._entrypoint(**all_args) |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from .fromkey import FromKey | ||
from .tokey import ToKey | ||
from .tensorflowscorer import TensorFlowScorer | ||
from .datasettransformer import DatasetTransformer | ||
|
||
__all__ = [ | ||
'FromKey', | ||
'ToKey', | ||
'TensorFlowScorer' | ||
'TensorFlowScorer', | ||
'DatasetTransformer' | ||
] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
# -------------------------------------------------------------------------------------------- | ||
# - Generated by tools/entrypoint_compiler.py: do not edit by hand | ||
""" | ||
DatasetTransformer | ||
""" | ||
|
||
__all__ = ["DatasetTransformer"] | ||
|
||
|
||
from sklearn.base import TransformerMixin | ||
|
||
from ..base_transform import BaseTransform | ||
from ..internal.core.preprocessing.datasettransformer import \ | ||
DatasetTransformer as core | ||
from ..internal.utils.utils import trace | ||
|
||
|
||
class DatasetTransformer(core, BaseTransform, TransformerMixin): | ||
""" | ||
**Description** | ||
Applies a TransformModel to a dataset. | ||
|
||
:param columns: see `Columns </nimbusml/concepts/columns>`_. | ||
|
||
:param transform_model: Transform model. | ||
|
||
:param params: Additional arguments sent to compute engine. | ||
|
||
""" | ||
|
||
@trace | ||
def __init__( | ||
self, | ||
transform_model, | ||
columns=None, | ||
**params): | ||
|
||
if columns: | ||
params['columns'] = columns | ||
BaseTransform.__init__(self, **params) | ||
core.__init__( | ||
self, | ||
transform_model=transform_model, | ||
**params) | ||
self._columns = columns | ||
|
||
def get_params(self, deep=False): | ||
""" | ||
Get the parameters for this operator. | ||
""" | ||
return core.get_params(self) |
184 changes: 184 additions & 0 deletions
184
src/python/nimbusml/tests/preprocessing/test_datasettransformer.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
# -------------------------------------------------------------------------------------------- | ||
import os | ||
import unittest | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from nimbusml import Pipeline, FileDataStream | ||
from nimbusml.datasets import get_dataset | ||
from nimbusml.feature_extraction.categorical import OneHotVectorizer | ||
from nimbusml.linear_model import LogisticRegressionBinaryClassifier, OnlineGradientDescentRegressor | ||
from nimbusml.preprocessing import DatasetTransformer | ||
from nimbusml.preprocessing.filter import RangeFilter | ||
from nimbusml import FileDataStream | ||
|
||
seed = 0 | ||
|
||
train_data = {'c0': ['a', 'b', 'a', 'b'], | ||
'c1': [1, 2, 3, 4], | ||
'c2': [2, 3, 4, 5]} | ||
train_df = pd.DataFrame(train_data).astype({'c1': np.float64, | ||
'c2': np.float64}) | ||
|
||
test_data = {'c0': ['a', 'b', 'b'], | ||
'c1': [1.5, 2.3, 3.7], | ||
'c2': [2.2, 4.9, 2.7]} | ||
test_df = pd.DataFrame(test_data).astype({'c1': np.float64, | ||
'c2': np.float64}) | ||
|
||
|
||
class TestDatasetTransformer(unittest.TestCase): | ||
|
||
def test_same_schema_with_dataframe_input(self): | ||
train_df_updated = train_df.drop(['c0'], axis=1) | ||
test_df_updated = test_df.drop(['c0'], axis=1) | ||
|
||
rf_max = 4.5 | ||
|
||
# Create reference pipeline | ||
std_pipeline = Pipeline([ | ||
RangeFilter(min=0.0, max=rf_max) << 'c2', | ||
OnlineGradientDescentRegressor(label='c2', feature=['c1']) | ||
], random_state=seed) | ||
|
||
std_pipeline.fit(train_df_updated) | ||
result_1 = std_pipeline.predict(test_df_updated) | ||
|
||
# Create combined pipeline | ||
transform_pipeline = Pipeline([RangeFilter(min=0.0, max=rf_max) << 'c2']) | ||
transform_pipeline.fit(train_df_updated) | ||
|
||
combined_pipeline = Pipeline([ | ||
DatasetTransformer(transform_model=transform_pipeline.model), | ||
OnlineGradientDescentRegressor(label='c2', feature=['c1']) | ||
], random_state=seed) | ||
combined_pipeline.fit(train_df_updated) | ||
|
||
os.remove(transform_pipeline.model) | ||
|
||
result_2 = combined_pipeline.predict(test_df_updated) | ||
|
||
self.assertTrue(result_1.equals(result_2)) | ||
|
||
|
||
def test_different_schema_with_dataframe_input(self): | ||
# Create reference pipeline | ||
std_pipeline = Pipeline([ | ||
OneHotVectorizer() << 'c0', | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
|
||
std_pipeline.fit(train_df) | ||
result_1 = std_pipeline.predict(test_df) | ||
|
||
# Create combined pipeline | ||
transform_pipeline = Pipeline([OneHotVectorizer() << 'c0'], random_state=seed) | ||
transform_pipeline.fit(train_df) | ||
|
||
combined_pipeline = Pipeline([ | ||
DatasetTransformer(transform_model=transform_pipeline.model), | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
combined_pipeline.fit(train_df) | ||
|
||
os.remove(transform_pipeline.model) | ||
|
||
result_2 = combined_pipeline.predict(test_df) | ||
|
||
self.assertTrue(result_1.equals(result_2)) | ||
|
||
|
||
def test_different_schema_with_filedatastream_input(self): | ||
train_filename = "train-data.csv" | ||
train_df.to_csv(train_filename, index=False, header=True) | ||
train_data_stream = FileDataStream.read_csv(train_filename, sep=',', header=True) | ||
|
||
test_filename = "test-data.csv" | ||
test_df.to_csv(test_filename, index=False, header=True) | ||
test_data_stream = FileDataStream.read_csv(test_filename, sep=',', header=True) | ||
|
||
# Create reference pipeline | ||
std_pipeline = Pipeline([ | ||
OneHotVectorizer() << 'c0', | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
|
||
std_pipeline.fit(train_data_stream) | ||
result_1 = std_pipeline.predict(test_data_stream) | ||
|
||
# Create combined pipeline | ||
transform_pipeline = Pipeline([OneHotVectorizer() << 'c0'], random_state=seed) | ||
transform_pipeline.fit(train_data_stream) | ||
|
||
combined_pipeline = Pipeline([ | ||
DatasetTransformer(transform_model=transform_pipeline.model), | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
combined_pipeline.fit(train_data_stream) | ||
|
||
os.remove(transform_pipeline.model) | ||
|
||
result_2 = combined_pipeline.predict(test_data_stream) | ||
|
||
self.assertTrue(result_1.equals(result_2)) | ||
|
||
os.remove(train_filename) | ||
os.remove(test_filename) | ||
|
||
|
||
def test_combining_two_dataset_transformers(self): | ||
rf_max = 4.5 | ||
|
||
# Create reference pipeline | ||
std_pipeline = Pipeline([ | ||
RangeFilter(min=0.0, max=rf_max) << 'c2', | ||
OneHotVectorizer() << 'c0', | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
|
||
std_pipeline.fit(train_df) | ||
result_1 = std_pipeline.predict(test_df) | ||
|
||
# Create combined pipeline | ||
transform_pipeline1 = Pipeline([RangeFilter(min=0.0, max=rf_max) << 'c2']) | ||
transform_pipeline1.fit(train_df) | ||
|
||
transform_pipeline2 = Pipeline([OneHotVectorizer() << 'c0'], random_state=seed) | ||
transform_pipeline2.fit(train_df) | ||
|
||
combined_pipeline = Pipeline([ | ||
DatasetTransformer(transform_model=transform_pipeline1.model), | ||
DatasetTransformer(transform_model=transform_pipeline2.model), | ||
OnlineGradientDescentRegressor(label='c2', feature=['c0', 'c1']) | ||
], random_state=seed) | ||
combined_pipeline.fit(train_df) | ||
|
||
os.remove(transform_pipeline1.model) | ||
os.remove(transform_pipeline2.model) | ||
|
||
result_2 = combined_pipeline.predict(test_df) | ||
|
||
self.assertTrue(result_1.equals(result_2)) | ||
|
||
|
||
def test_get_fit_info(self): | ||
transform_pipeline = Pipeline([RangeFilter(min=0.0, max=4.5) << 'c2']) | ||
transform_pipeline.fit(train_df) | ||
|
||
combined_pipeline = Pipeline([ | ||
DatasetTransformer(transform_model=transform_pipeline.model), | ||
OnlineGradientDescentRegressor(label='c2', feature=['c1']) | ||
], random_state=seed) | ||
combined_pipeline.fit(train_df) | ||
|
||
info = combined_pipeline.get_fit_info(train_df) | ||
|
||
self.assertTrue(info[0][1]['name'] == 'DatasetTransformer') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
This file contains 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
This file contains 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.
Great test! #ByDesign