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 basic one-hot encoding #73

Merged
merged 19 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 0 additions & 5 deletions evalml/models/auto_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numpy as np
import pandas as pd
from colorama import Style
from pandas.api.types import is_numeric_dtype
from tqdm import tqdm

from evalml import preprocessing
Expand Down Expand Up @@ -97,10 +96,6 @@ def fit(self, X, y, feature_types=None, raise_errors=False):
if not isinstance(y, pd.Series):
y = pd.Series(y)

for col in X.columns:
if not is_numeric_dtype(X[col]):
raise ValueError("Input column '{}' contains non-numerical data".format(col))

self._log_title("Beginning pipeline search")
self._log("Optimizing for %s. " % self.objective.name, new_line=False)

Expand Down
8 changes: 6 additions & 2 deletions evalml/pipelines/classification/logistic_regression.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import category_encoders as ce
import numpy as np
import pandas as pd
from sklearn.impute import SimpleImputer
Expand All @@ -20,11 +21,13 @@ class LogisticRegressionPipeline(PipelineBase):
"penalty": ["l2"],
"C": Real(.01, 10),
"impute_strategy": ["mean", "median", "most_frequent"],
"drop_invariant": [True, False]
jeremyliweishih marked this conversation as resolved.
Show resolved Hide resolved
}

def __init__(self, objective, penalty, C, impute_strategy,
def __init__(self, objective, penalty, C, impute_strategy, drop_invariant,
number_features, n_jobs=1, random_state=0):
imputer = SimpleImputer(strategy=impute_strategy)
enc = ce.OneHotEncoder(drop_invariant=drop_invariant, return_df=True)
jeremyliweishih marked this conversation as resolved.
Show resolved Hide resolved

estimator = LogisticRegression(random_state=random_state,
penalty=penalty,
Expand All @@ -34,7 +37,8 @@ def __init__(self, objective, penalty, C, impute_strategy,
n_jobs=-1)

self.pipeline = Pipeline(
[("imputer", imputer),
[("encoder", enc),
("imputer", imputer),
("scaler", StandardScaler()),
("estimator", estimator)]
)
Expand Down
12 changes: 8 additions & 4 deletions evalml/pipelines/classification/random_forest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import category_encoders as ce
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
Expand All @@ -20,12 +21,14 @@ class RFClassificationPipeline(PipelineBase):
"n_estimators": Integer(10, 1000),
"max_depth": Integer(1, 32),
"impute_strategy": ["mean", "median", "most_frequent"],
"percent_features": Real(.01, 1)
"percent_features": Real(.01, 1),
"drop_invariant": [True, False]
}

def __init__(self, objective, n_estimators, max_depth, impute_strategy, percent_features,
number_features, n_jobs=1, random_state=0):
def __init__(self, objective, n_estimators, max_depth, impute_strategy, drop_invariant,
percent_features, number_features, n_jobs=1, random_state=0):
imputer = SimpleImputer(strategy=impute_strategy)
enc = ce.OneHotEncoder(drop_invariant=drop_invariant, return_df=True)

estimator = RandomForestClassifier(random_state=random_state,
n_estimators=n_estimators,
Expand All @@ -39,7 +42,8 @@ def __init__(self, objective, n_estimators, max_depth, impute_strategy, percent_
)

self.pipeline = Pipeline(
[("imputer", imputer),
[("encoder", enc),
("imputer", imputer),
("feature_selection", feature_selection),
("estimator", estimator)]
)
Expand Down
10 changes: 7 additions & 3 deletions evalml/pipelines/classification/xgboost.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import category_encoders as ce
import numpy as np
import pandas as pd
from sklearn.feature_selection import SelectFromModel
Expand All @@ -21,12 +22,14 @@ class XGBoostPipeline(PipelineBase):
"min_child_weight": Real(1, 10),
"max_depth": Integer(1, 20),
"impute_strategy": ["mean", "median", "most_frequent"],
"drop_invariant": [True, False],
"percent_features": Real(.01, 1)
}

def __init__(self, objective, eta, min_child_weight, max_depth, impute_strategy, percent_features,
number_features, n_jobs=1, random_state=0):
def __init__(self, objective, eta, min_child_weight, max_depth, impute_strategy, drop_invariant,
percent_features, number_features, n_jobs=1, random_state=0):
imputer = SimpleImputer(strategy=impute_strategy)
enc = ce.OneHotEncoder(drop_invariant=drop_invariant, return_df=True)

estimator = XGBClassifier(
random_state=random_state,
Expand All @@ -42,7 +45,8 @@ def __init__(self, objective, eta, min_child_weight, max_depth, impute_strategy,
)

self.pipeline = Pipeline(
[("imputer", imputer),
[("encoder", enc),
("imputer", imputer),
("feature_selection", feature_selection),
("estimator", estimator)]
)
Expand Down
10 changes: 7 additions & 3 deletions evalml/pipelines/regression/random_forest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import category_encoders as ce
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
Expand All @@ -20,13 +21,15 @@ class RFRegressionPipeline(PipelineBase):
"n_estimators": Integer(10, 1000),
"max_depth": Integer(1, 32),
"impute_strategy": ["mean", "median", "most_frequent"],
"drop_invariant": [True, False],
"percent_features": Real(.01, 1)
}

def __init__(self, objective, n_estimators, max_depth, impute_strategy, percent_features,
def __init__(self, objective, n_estimators, max_depth, impute_strategy, drop_invariant, percent_features,
number_features, n_jobs=1, random_state=0):

imputer = SimpleImputer(strategy=impute_strategy)
enc = ce.OneHotEncoder(drop_invariant=drop_invariant, return_df=True)

estimator = RandomForestRegressor(random_state=random_state,
n_estimators=n_estimators,
Expand All @@ -40,9 +43,10 @@ def __init__(self, objective, n_estimators, max_depth, impute_strategy, percent_
)

self.pipeline = Pipeline(
[("imputer", imputer),
[("encoder", enc),
("imputer", imputer),
("feature_selection", feature_selection),
("estimator", estimator)]
("estimator", estimator)],
)

super().__init__(objective=objective, random_state=random_state)
Expand Down
8 changes: 8 additions & 0 deletions evalml/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def X_y_categorical_regression():
return X, y


@pytest.fixture
def X_y_categorical_classification():
titanic = pd.read_csv('https://featuretools-static.s3.amazonaws.com/evalml/Titanic/train.csv')
y = titanic['Survived']
X = titanic.drop('Survived', axis=1)
return X, y


@pytest.fixture
def trained_model(X_y):
X, y = X_y
Expand Down
7 changes: 7 additions & 0 deletions evalml/tests/test_autoclassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def test_multi_auto(X_y_multi):
assert clf.default_objectives == get_objectives('multiclass')


def test_categorical_auto(X_y_categorical_classification):
X, y = X_y_categorical_classification
clf = AutoClassifier(objective="recall", max_pipelines=5, multiclass=False)
clf.fit(X.values, y)
assert not clf.rankings['score'].isnull().all()


def test_random_state(X_y):
X, y = X_y

Expand Down
5 changes: 2 additions & 3 deletions evalml/tests/test_autoregressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def test_random_state(X_y):
def test_categorical(X_y_categorical_regression):
X, y = X_y_categorical_regression
clf = AutoRegressor(objective="R2", max_pipelines=5, random_state=0)
error_msg = 'contains non-numerical data'
with pytest.raises(ValueError, match=error_msg):
clf.fit(X, y, raise_errors=True)
clf.fit(X.values, y)
assert not clf.rankings['score'].isnull().all()


def test_callback(X_y):
Expand Down
2 changes: 1 addition & 1 deletion evalml/tests/test_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def test_lr_multi(X_y_multi):
X, y = X_y_multi
objective = PrecisionMicro()
clf = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', number_features=len(X[0]))
clf = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', drop_invariant=False, number_features=len(X[0]))
clf.fit(X, y)
clf.score(X, y)
y_pred = clf.predict(X)
Expand Down
2 changes: 1 addition & 1 deletion evalml/tests/test_objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_binary_average(X_y):
X = pd.DataFrame(X)
y = pd.Series(y)

pipeline = LogisticRegressionPipeline(objective=Precision(), penalty='l2', C=1.0, impute_strategy='mean', number_features=0)
pipeline = LogisticRegressionPipeline(objective=Precision(), penalty='l2', C=1.0, impute_strategy='mean', drop_invariant=False, number_features=0)
pipeline.fit(X, y)
y_pred = pipeline.predict(X)

Expand Down
6 changes: 3 additions & 3 deletions evalml/tests/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_serialization(X_y, trained_model, path_management):
path = os.path.join(path_management, 'pipe.pkl')
objective = Precision()

pipeline = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', number_features=len(X[0]))
pipeline = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', drop_invariant=False, number_features=len(X[0]))
pipeline.fit(X, y)
save_pipeline(pipeline, path)
assert pipeline.score(X, y) == load_pipeline(path).score(X, y)
Expand All @@ -60,10 +60,10 @@ def test_reproducibility(X_y):
amount_col=10
)

clf = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', number_features=len(X[0]), random_state=0)
clf = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', drop_invariant=False, number_features=len(X[0]), random_state=0)
clf.fit(X, y)

clf_1 = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', number_features=len(X[0]), random_state=0)
clf_1 = LogisticRegressionPipeline(objective=objective, penalty='l2', C=1.0, impute_strategy='mean', drop_invariant=False, number_features=len(X[0]), random_state=0)
clf_1.fit(X, y)

assert clf_1.score(X, y) == clf.score(X, y)
2 changes: 1 addition & 1 deletion evalml/tests/test_rf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def test_rf_multi(X_y_multi):
X, y = X_y_multi
objective = PrecisionMicro()
clf = RFClassificationPipeline(objective=objective, n_estimators=10, max_depth=3, impute_strategy='mean', percent_features=1.0, number_features=len(X[0]))
clf = RFClassificationPipeline(objective=objective, n_estimators=10, max_depth=3, impute_strategy='mean', drop_invariant=False, percent_features=1.0, number_features=len(X[0]))
clf.fit(X, y)
clf.score(X, y)
y_pred = clf.predict(X)
Expand Down
2 changes: 1 addition & 1 deletion evalml/tests/test_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
def test_xg_multi(X_y_multi):
X, y = X_y_multi
objective = PrecisionMicro()
clf = XGBoostPipeline(objective=objective, eta=0.1, min_child_weight=1, max_depth=3, impute_strategy='mean', percent_features=1.0, number_features=len(X[0]))
clf = XGBoostPipeline(objective=objective, eta=0.1, min_child_weight=1, max_depth=3, impute_strategy='mean', drop_invariant=False, percent_features=1.0, number_features=len(X[0]))
clf.fit(X, y)
clf.score(X, y)
y_pred = clf.predict(X)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ scikit-optimize[plots]
colorama
s3fs==0.2.2
joblib>=0.10.3
category_encoders
jeremyliweishih marked this conversation as resolved.
Show resolved Hide resolved