Skip to content
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

Internal target check to ensure no class missing from train/val #1226

Merged
merged 9 commits into from Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/release_notes.rst
Expand Up @@ -7,6 +7,7 @@ Release Notes
* Added `get_feature_names` on `OneHotEncoder` :pr:`1193`
* Added LightGBM to AutoMLSearch :pr:`1199`
* Updates scikit-learn and scikit-optimize to use latest versions - 0.23.2 and 0.8.1 respectively :pr:`1141`
* Included internal target check for both training and validation data in AutoMLSearch :pr:`1226`
* Add `ProblemTypes.all_problem_types` helper to get list of supported problem types :pr:`1219`
* Fixes
* Updated GitHub URL after migration to Alteryx GitHub org :pr:`1207`
Expand Down
8 changes: 8 additions & 0 deletions evalml/automl/automl_search.py
Expand Up @@ -607,10 +607,18 @@ def _compute_cv_scores(self, pipeline, X, y):
start = time.time()
cv_data = []
logger.info("\tStarting cross validation")
warnings.filterwarnings("ignore", lineno=665)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add filter to catch and suppress SKLearn's warning for having too few cases of a target given n_splits=3

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm skeptical of lineno because it will be impossible to maintain as the size of this file changes. Maybe we don't worry about suppressing warnings?

FWIW, I'm still seeing the warning when I run this code anyways.

image

for i, (train, test) in enumerate(self.data_split.split(X, y)):
logger.debug(f"\t\tTraining and scoring on fold {i}")
X_train, X_test = X.iloc[train], X.iloc[test]
y_train, y_test = y.iloc[train], y.iloc[test]
if self.problem_type in [ProblemTypes.BINARY, ProblemTypes.MULTICLASS]:
freddyaboulton marked this conversation as resolved.
Show resolved Hide resolved
diff_train = set(y).difference(set(y_train))
bchen1116 marked this conversation as resolved.
Show resolved Hide resolved
diff_test = set(y).difference(set(y_test))
diff_string = f"Missing target values in the training set: {diff_train}. " if diff_train else ""
bchen1116 marked this conversation as resolved.
Show resolved Hide resolved
diff_string += f"Missing target values in the test set: {diff_test}." if diff_test else ""
if diff_string:
raise Exception(diff_string)
objectives_to_score = [self.objective] + self.additional_objectives
cv_pipeline = None
try:
Expand Down
51 changes: 51 additions & 0 deletions evalml/tests/automl_tests/test_automl.py
Expand Up @@ -1080,3 +1080,54 @@ def test_max_batches_must_be_non_negative(max_batches):

def test_can_print_out_automl_objective_names():
AutoMLSearch.print_objective_names_allowed_in_automl()


def test_data_split_binary(X_y_binary):
X, y = X_y_binary
y[:] = 0
y[0] = 1

automl = AutoMLSearch(problem_type='binary')
with pytest.raises(Exception, match="Missing target values in the"):
automl.search(X, y)
with pytest.raises(Exception, match="Missing target values in the"):
automl.search(X, y, data_checks="disabled")

y[1] = 1
with pytest.raises(Exception, match="Missing target values in the"):
automl.search(X, y)
with pytest.raises(Exception, match="Missing target values in the"):
automl.search(X, y, data_checks="disabled")

y[2] = 1
automl.search(X, y, data_checks="disabled")


def test_data_split_multi(X_y_multi):
X, y = X_y_multi
y[:] = 1
y[0] = 0

automl = AutoMLSearch(problem_type='multiclass')
with pytest.raises(Exception, match="Missing target values"):
automl.search(X, y)
with pytest.raises(Exception, match="Missing target values"):
automl.search(X, y, data_checks="disabled")

y[1] = 2
with pytest.raises(Exception, match=r"(\{\d?\}.+\{\d?\})|(\{.+\,.+\})"):
bchen1116 marked this conversation as resolved.
Show resolved Hide resolved
automl.search(X, y)
with pytest.raises(Exception, match=r"(\{\d?\}.+\{\d?\})|(\{.+\,.+\})"):
automl.search(X, y, data_checks="disabled")

y[1] = 0
y[2:4] = 2
with pytest.raises(Exception, match="Missing target values"):
automl.search(X, y, data_checks="disabled")

y[4] = 2
with pytest.raises(Exception, match="Missing target values"):
automl.search(X, y, data_checks="disabled")

y[5] = 0
automl.search(X, y, data_checks="disabled")