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

support ray tune imputer #2823

Merged
merged 2 commits into from
Sep 23, 2020
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
1 change: 1 addition & 0 deletions pyzoo/zoo/automl/config/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ def search_space(self, all_available_features):
# -------- feature related parameters
"model": "XGBRegressor",

"imputation": tune.choice(["LastFillImpute", "FillZeroImpute"]),
"n_estimators": self.n_estimators,
"max_depth": self.max_depth,
"min_child_weight": self.min_child_weight,
Expand Down
15 changes: 15 additions & 0 deletions pyzoo/zoo/automl/impute/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright 2018 Analytics Zoo Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
53 changes: 53 additions & 0 deletions pyzoo/zoo/automl/impute/abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright 2018 Analytics Zoo Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from abc import ABC, abstractmethod


class BaseImputation(ABC):
"""
Abstract Base class for imputation.
"""

@abstractmethod
def impute(self, input_df):
"""
fill na value
:param input_df: input dataframe
:return:
"""
pass

def save(self, file_path):
"""
save the feature tools internal variables.
Some of the variables are derived after fit_transform, so only saving config is not enough.
:param: file_path : the file to be saved
:param: config: the trial config
:return:
"""
pass

def restore(self, **config):
"""
Restore variables from file
:param file_path: file contain saved parameters.
i.e. some parameters are obtained during training,
not in trial config, e.g. scaler fit params)
:param config: the trial config
:return:
"""
pass
44 changes: 44 additions & 0 deletions pyzoo/zoo/automl/impute/impute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2018 Analytics Zoo Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from zoo.automl.common.util import save_config
from zoo.automl.impute.abstract import BaseImputation
from zoo.zouwu.preprocessing.impute.LastFill import LastFill


class LastFillImpute(BaseImputation):
"""
LastFill imputation
"""
def __init__(self):
self.imputer = LastFill()

def impute(self, input_df):
assert self.imputer is not None
df = self.imputer.impute(input_df)
return df

def restore(self, **config):
self.imputer = LastFill()


class FillZeroImpute(BaseImputation):
"""
FillZero imputation
"""
def impute(self, input_df):
input_df.fillna(0)
return input_df
11 changes: 6 additions & 5 deletions pyzoo/zoo/automl/regression/xgbregressor_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def fit(self,
mc=False,
resources_per_trial={"cpu": 2},
distributed=False,
hdfs_url=None
hdfs_url=None,
):
"""
Trains the model for time sequence prediction.
Expand Down Expand Up @@ -121,7 +121,8 @@ def fit(self,
recipe=recipe,
mc=mc,
resources_per_trial=resources_per_trial,
remote_dir=remote_dir)
remote_dir=remote_dir,
)
return self.pipeline

def evaluate(self,
Expand Down Expand Up @@ -220,10 +221,10 @@ def _hp_search(self,
recipe,
mc,
resources_per_trial,
remote_dir):
remote_dir,):
def model_create_func():
model = XGBoostRegressor(config)
model.set_params(n_jobs=resources_per_trial)
model.set_params(n_jobs=resources_per_trial.get("cpu", 1))
return model
model = model_create_func()
ft = IdentityTransformer(self.feature_cols, self.target_col)
Expand Down Expand Up @@ -257,7 +258,7 @@ def model_create_func():
metric=metric,
metric_mode=metric_mode,
mc=mc,
num_samples=num_samples)
num_samples=num_samples,)
# searcher.test_run()
analysis = searcher.run()

Expand Down
15 changes: 13 additions & 2 deletions pyzoo/zoo/automl/search/RayTuneSearchEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from zoo.automl.search.abstract import *
from zoo.automl.common.util import *
from zoo.automl.impute.impute import *
from ray.tune import Trainable
import ray.tune.track
from ray.tune.suggest.bayesopt import BayesOptSearch
Expand Down Expand Up @@ -102,7 +103,8 @@ def compile(self,
metric=metric,
metric_mode=metric_mode,
mc=mc,
remote_dir=self.remote_dir)
remote_dir=self.remote_dir
)
# self.trainable_class = self._prepare_trainable_class(input_df,
# feature_transformers,
# # model,
Expand Down Expand Up @@ -205,7 +207,7 @@ def _prepare_train_func(input_df,
metric_mode,
validation_df=None,
mc=False,
remote_dir=None
remote_dir=None,
):
"""
Prepare the train function for ray tune
Expand Down Expand Up @@ -244,9 +246,18 @@ def train_func(config):
#
trial_model = model_create_func

imputer = None
if "imputation" in config:
if config["imputation"] == "LastFillImpute":
imputer = LastFillImpute()
elif config["imputation"] == "FillZeroImpute":
imputer = FillZeroImpute()

# handling input
global_input_df = ray.get(input_df_id)
trial_input_df = deepcopy(global_input_df)
if imputer:
trial_input_df = imputer.impute(trial_input_df)
config = convert_bayes_configs(config).copy()
# print("config is ", config)
(x_train, y_train) = trial_ft.fit_transform(trial_input_df, **config)
Expand Down