Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Fix a bug with the classes_ attribute when no y input is specified during fitting. #218

Merged
merged 2 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions src/python/nimbusml/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,9 +1913,10 @@ def _extract_classes(self, y):
self._add_classes(unique_classes)

def _extract_classes_from_headers(self, headers):
classes = [x.replace('Score.', '') for x in headers]
classes = np.array(classes).astype(self.last_node.classes_.dtype)
self._add_classes(classes)
if hasattr(self.last_node, 'classes_'):
classes = [x.replace('Score.', '') for x in headers]
classes = np.array(classes).astype(self.last_node.classes_.dtype)
self._add_classes(classes)

def _add_classes(self, classes):
# Create classes_ attribute similar to scikit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_pass_predict_proba_multiclass_3class(self):
s,
38.0,
decimal=4,
err_msg=invalid_decision_function_output)
err_msg=invalid_predict_proba_output)
assert_equal(set(clf.classes_), {'Blue', 'Green', 'Red'})

def test_pass_predict_proba_multiclass_with_pipeline_adds_classes(self):
Expand All @@ -137,7 +137,7 @@ def test_pass_predict_proba_multiclass_with_pipeline_adds_classes(self):
s,
38.0,
decimal=4,
err_msg=invalid_decision_function_output)
err_msg=invalid_predict_proba_output)

assert_equal(set(clf.classes_), expected_classes)
assert_equal(set(pipeline.classes_), expected_classes)
Expand All @@ -150,9 +150,34 @@ def test_pass_predict_proba_multiclass_3class_retains_classes_type(self):
s,
38.0,
decimal=4,
err_msg=invalid_decision_function_output)
err_msg=invalid_predict_proba_output)
assert_equal(set(clf.classes_), {0, 1, 2})

def test_predict_proba_multiclass_3class_no_y_input_implies_no_classes_attribute(self):
X_train = X_train_3class_int.join(y_train_3class_int)
X_test = X_test_3class_int.join(y_test_3class_int)

clf = FastLinearClassifier(number_of_threads=1, label='Label')
clf.fit(X_train)

if hasattr(clf, 'classes_'):
# The classes_ attribute is currently not supported
# when fitting when there is no y input specified.
self.fail("classes_ attribute not expected.")

s = clf.predict_proba(X_test).sum()
assert_almost_equal(
s,
38.0,
decimal=4,
err_msg=invalid_predict_proba_output)

if hasattr(clf, 'classes_'):
# The classes_ attribute is currently not supported
# when predicting when there was no y input specified
# during fitting.
self.fail("classes_ attribute not expected.")

def test_fail_predict_proba_multiclass_with_pipeline(self):
check_unsupported_predict_proba(self, Pipeline(
[NaiveBayesClassifier()]), X_train, y_train, X_test)
Expand Down Expand Up @@ -242,6 +267,31 @@ def test_pass_decision_function_multiclass_3class_retains_classes_type(self):
err_msg=invalid_decision_function_output)
assert_equal(set(clf.classes_), {0, 1, 2})

def test_decision_function_multiclass_3class_no_y_input_implies_no_classes_attribute(self):
X_train = X_train_3class_int.join(y_train_3class_int)
X_test = X_test_3class_int.join(y_test_3class_int)

clf = FastLinearClassifier(number_of_threads=1, label='Label')
clf.fit(X_train)

if hasattr(clf, 'classes_'):
# The classes_ attribute is currently not supported
# when fitting when there is no y input specified.
self.fail("classes_ attribute not expected.")

s = clf.decision_function(X_test).sum()
assert_almost_equal(
s,
38.0,
decimal=4,
err_msg=invalid_decision_function_output)

if hasattr(clf, 'classes_'):
# The classes_ attribute is currently not supported
# when predicting when there was no y input specified
# during fitting.
self.fail("classes_ attribute not expected.")

def test_fail_decision_function_multiclass(self):
check_unsupported_decision_function(
self, LogisticRegressionClassifier(), X_train, y_train, X_test)
Expand Down