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

Added support for splitting regression sets #112

Merged
merged 7 commits into from
Oct 9, 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
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import os
import sys

import evalml

path = os.path.join('..', '..')
sys.path.insert(0, os.path.abspath(path))

import evalml

# -- Project information -----------------------------------------------------

Expand Down
21 changes: 13 additions & 8 deletions evalml/preprocessing/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd
from dask import dataframe as dd
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import ShuffleSplit, StratifiedShuffleSplit


def load_data(path, index, label, drop=None, verbose=True, **kwargs):
Expand Down Expand Up @@ -43,24 +43,29 @@ def load_data(path, index, label, drop=None, verbose=True, **kwargs):
return X, y


def split_data(X, y, test_size=.2, random_state=None):
def split_data(X, y, regression=False, test_size=.2, random_state=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

After discussion with Chris, there didn't seem to be a solution to automatically catching the problem type (regression vs. classification) that would satisfy all edges cases. We proposed to set a flag so theres an all-in-one function for users to split data. We saw this as more convenient than creating two separate functions that could be more ambiguous especially when the function names are not indicative of its use case.

"""Splits data into train and test sets.

Args:
X (DataFrame) : features
y (Series) : labels
regression (bool): if true, do not use stratified split
test_size (float) : percent of train set to holdout for testing
random_state (int) : seed for the random number generator

Returns:
DataFrame, DataFrame, Series, Series : features and labels each split into train and test sets
"""
stratified = StratifiedShuffleSplit(
n_splits=1,
test_size=test_size,
random_state=random_state,
)
train, test = next(stratified.split(X, y))
if regression:
CV_method = ShuffleSplit(n_splits=1,
test_size=test_size,
random_state=0)
else:
CV_method = StratifiedShuffleSplit(
n_splits=1,
test_size=test_size,
random_state=random_state)
train, test = next(CV_method.split(X, y))
X_train = X.iloc[train]
X_test = X.iloc[test]
y_train = y.iloc[train]
Expand Down
7 changes: 7 additions & 0 deletions evalml/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ def X_y():
return X, y


@pytest.fixture
def X_y_reg():
X, y = datasets.make_regression(n_samples=100, n_features=20,
n_informative=3, random_state=0)
return X, y


@pytest.fixture
def X_y_multi():
X, y = datasets.make_classification(n_samples=100, n_features=20, n_classes=3,
Expand Down
31 changes: 31 additions & 0 deletions evalml/tests/preprocessing_tests/test_split_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pandas as pd

from evalml.preprocessing import split_data


def test_split_regression(X_y_reg):
X, y = X_y_reg
X = pd.DataFrame(X)
y = pd.Series(y)
test_pct = 0.25
X_train, X_test, y_train, y_test = split_data(X, y, test_size=test_pct, regression=True)
test_size = len(X) * test_pct
train_size = len(X) - test_size
assert len(X_train) == train_size
assert len(X_test) == test_size
assert len(y_train) == train_size
assert len(y_test) == test_size


def test_split_classification(X_y):
X, y = X_y
X = pd.DataFrame(X)
y = pd.Series(y)
test_pct = 0.25
X_train, X_test, y_train, y_test = split_data(X, y, test_size=test_pct)
test_size = len(X) * 0.25
train_size = len(X) - test_size
assert len(X_train) == train_size
assert len(X_test) == test_size
assert len(y_train) == train_size
assert len(y_test) == test_size