This repository was 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
Remove label column from features when no Y is specified and predictor supports labels. #439
Merged
Merged
Changes from all commits
Commits
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
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
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 |
---|---|---|
@@ -0,0 +1,231 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
# -------------------------------------------------------------------------------------------- | ||
|
||
import json | ||
import unittest | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from nimbusml import Pipeline, Role | ||
from nimbusml.cluster import KMeansPlusPlus | ||
from nimbusml.ensemble import FastTreesRegressor, FastForestRegressor | ||
from nimbusml.linear_model import FastLinearClassifier | ||
|
||
|
||
class TestVariableColumn(unittest.TestCase): | ||
|
||
def verify_regressor_nodes(self, graph, label_name, features, trainer_name): | ||
nodes = graph['nodes'] | ||
|
||
self.assertEqual(nodes[0]["Name"], "Transforms.OptionalColumnCreator") | ||
self.assertEqual(nodes[0]["Inputs"]["Column"], [label_name]) | ||
|
||
self.assertEqual(nodes[1]["Name"], "Transforms.LabelToFloatConverter") | ||
self.assertEqual(nodes[1]["Inputs"]["LabelColumn"], label_name) | ||
|
||
self.assertEqual(nodes[2]["Name"], "Transforms.FeatureCombiner") | ||
self.assertEqual(nodes[2]["Inputs"]["Features"], features) | ||
|
||
self.assertEqual(nodes[3]["Name"], trainer_name) | ||
self.assertEqual(nodes[3]["Inputs"]["FeatureColumnName"], "Features") | ||
self.assertEqual(nodes[3]["Inputs"]["LabelColumnName"], label_name) | ||
|
||
def verify_classifier_nodes(self, graph, label_name, features, trainer_name): | ||
nodes = graph['nodes'] | ||
|
||
self.assertEqual(nodes[0]["Name"], "Transforms.OptionalColumnCreator") | ||
self.assertEqual(nodes[0]["Inputs"]["Column"], [label_name]) | ||
|
||
self.assertEqual(nodes[1]["Name"], "Transforms.LabelColumnKeyBooleanConverter") | ||
self.assertEqual(nodes[1]["Inputs"]["LabelColumn"], label_name) | ||
|
||
self.assertEqual(nodes[2]["Name"], "Transforms.FeatureCombiner") | ||
self.assertEqual(nodes[2]["Inputs"]["Features"], features) | ||
|
||
self.assertEqual(nodes[3]["Name"], trainer_name) | ||
self.assertEqual(nodes[3]["Inputs"]["FeatureColumnName"], "Features") | ||
self.assertEqual(nodes[3]["Inputs"]["LabelColumnName"], label_name) | ||
|
||
def test_label_column_defaults_to_label_when_no_label_column_in_input_data(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastForestRegressor() | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "Label", | ||
["c1", "c2", "c3", "c4"], | ||
"Trainers.FastForestRegressor") | ||
|
||
def test_label_column_defaults_to_label_when_label_column_in_input_data(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'Label': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastTreesRegressor() | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "Label", | ||
["c1", "c2", "c3"], | ||
"Trainers.FastTreeRegressor") | ||
|
||
def test_label_column_specified_as_argument_without_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'd1': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastForestRegressor(label='d1') | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "d1", | ||
["c1", "c2", "c4"], | ||
"Trainers.FastForestRegressor") | ||
|
||
def test_label_column_specified_as_argument_with_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'd1': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastForestRegressor(label='d1', feature=['c1', 'c3', 'c4']) | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "d1", | ||
["c1", "c3", "c4"], | ||
"Trainers.FastForestRegressor") | ||
|
||
def test_label_column_specified_as_role_without_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'd1': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastForestRegressor() << {Role.Label: 'd1'} | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "d1", | ||
["c1", "c3", "c4"], | ||
"Trainers.FastForestRegressor") | ||
|
||
def test_label_column_specified_as_role_with_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'd1': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastForestRegressor() << { | ||
Role.Label: 'd1', | ||
Role.Feature: ['c1', 'c4'] | ||
} | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_regressor_nodes(result, "d1", | ||
["c1", "c4"], | ||
"Trainers.FastForestRegressor") | ||
|
||
def test_default_label_for_classifier_without_label_column(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier() | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "Label", | ||
['c1', 'c2', 'c3', 'c4'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_default_label_for_classifier_with_label_column(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'Label': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier() | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "Label", | ||
['c1', 'c2', 'c3'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_label_column_for_classifier_specified_as_argument(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'd1': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier(label='d1') | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "d1", | ||
['c1', 'c2', 'c3'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_label_column_for_classifier_specified_as_argument_with_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'd1': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier(label='d1', feature=['c1', 'c2']) | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "d1", | ||
['c1', 'c2'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_label_column_for_classifier_specified_as_role_without_features(self): | ||
train_data = {'d1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'c4': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier() << {Role.Label: 'd1'} | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "d1", | ||
['c2', 'c3', 'c4'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_label_column_for_classifier_specified_as_role_with_features(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'd1': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = FastLinearClassifier() << { | ||
Role.Label: 'd1', | ||
Role.Feature: ['c1', 'c4'] | ||
} | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
|
||
self.verify_classifier_nodes(result, "d1", | ||
['c1', 'c4'], | ||
"Trainers.StochasticDualCoordinateAscentClassifier") | ||
|
||
def test_non_label_based_predictor_does_not_have_label_column_automatically_removed(self): | ||
train_data = {'c1': [2, 3, 4, 5], 'c2': [3, 4, 5, 6], | ||
'c3': [4, 5, 6, 7], 'Label': [0, 1, 2, 1]} | ||
train_df = pd.DataFrame(train_data) | ||
|
||
predictor = KMeansPlusPlus(n_clusters=5) | ||
pipeline = Pipeline([predictor]) | ||
result = json.loads(pipeline.fit(train_df, dry_run=True)) | ||
nodes = result['nodes'] | ||
|
||
self.assertEqual(nodes[0]["Name"], "Transforms.FeatureCombiner") | ||
self.assertEqual(nodes[0]["Inputs"]["Features"], ['c1', 'c2', 'c3', 'Label']) | ||
|
||
self.assertEqual(nodes[1]["Name"], "Trainers.KMeansPlusPlusClusterer") | ||
self.assertEqual(nodes[1]["Inputs"]["FeatureColumnName"], "Features") | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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.
Awesome! Great suit of tests