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

automatic detection of multioutput datasets #1001

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
43 changes: 36 additions & 7 deletions tpot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ def _setup_toolbox(self):
self._toolbox.register('expr_mut', self._gen_grow_safe, min_=self._min, max_=self._max)
self._toolbox.register('mutate', self._random_mutation_operator)


def _fit_init(self):
def _fit_init(self, multi_output_target: bool = False):
# initialization for fit function
if not self.warm_start or not hasattr(self, '_pareto_front'):
self._pop = []
Expand All @@ -501,6 +500,35 @@ def _fit_init(self):
self._last_optimized_pareto_front_n_gens = 0
self._setup_config(self.config_dict)

if multi_output_target:
Copy link
Contributor

Choose a reason for hiding this comment

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

Modifying _config_dict may not work in the situation that use use a customized configurations instead of default one. So, I think a practical way is to modify the _compile_to_sklearn function (here). If multi_output_target is True, then
sklearn_pipeline=MultiOutputClassifier(estimator=sklearn_pipeline) or sklearn_pipeline=MultiOutputRegessor(estimator=sklearn_pipeline) . I think it maybe a more general solution for multioutput dataset.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems better, to be honest i didn't find a good place where to put my code and only settled on the _fit_init function therefore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay i looked into it, but the code would be a mess. Several functions would have to take multi_output_target as a new argument (most of them in export_utils.py), since they don't have access to the data or the TPOT Object.

Imo _fit_init seems to be the least intrusive point to include the checks

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for looking into. You are right. I think TPOT exported codes should also include MultiOutputRegessor/MultiOutputClassifier, which should change a lot of codes in TPOT. I will look into it when I get some time next week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any updates?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jhmenke I am sorry for overlooking this. I did not get a chance to look into this issue those days due to my busy schedule. I agree that TPOT need some major changes for including MultiOutputRegessor/MultiOutputClassifier. I may get some time in March to add those changes. You are welcome to push any changes meanwhile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we use my PR as a temporary fix until you have time to thoroughly refactor the code? I can prepare an update with the current development branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the delay. I think we can use it for a temporary solution with a minor release.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay i merged the current master/development into this. Should be good to go as an interim solution then.

single_output_classifiers = [
'sklearn.naive_bayes.MultinomialNB',
'sklearn.svm.LinearSVC',
'xgboost.XGBClassifier'
]
single_output_regressors = [
'sklearn.ensemble.AdaBoostRegressor',
'sklearn.linear_model.LassoLarsCV',
'sklearn.linear_model.ElasticNetCV',
'sklearn.svm.LinearSVR',
'xgboost.XGBRegressor',
'sklearn.linear_model.SGDRegressor'
]
for model in list(self._config_dict.keys()):
if model in single_output_classifiers:
if 'sklearn.multioutput.MultiOutputClassifier' not in self._config_dict.keys():
self._config_dict['sklearn.multioutput.MultiOutputClassifier'] = {"estimator": {}}
self._config_dict['sklearn.multioutput.MultiOutputClassifier']['estimator'][model] = self._config_dict[model]
Copy link
Contributor

Choose a reason for hiding this comment

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

There is only one sklearn.multioutput.MultiOutputClassifier in self._config_dict and estimator is keeping updated until the last model in single_output_classifiers so that the rest models should be removed.

self._config_dict.pop(model, None)
elif model in single_output_regressors:
if 'sklearn.multioutput.MultiOutputRegressor' not in self._config_dict.keys():
self._config_dict['sklearn.multioutput.MultiOutputRegressor'] = {"estimator": {}}
if model == 'sklearn.linear_model.ElasticNetCV':
self._config_dict['sklearn.linear_model.MultiTaskElasticNetCV'] = self._config_dict[model]
else:
self._config_dict['sklearn.multioutput.MultiOutputRegressor']['estimator'][model] = self._config_dict[model]
self._config_dict.pop(model, None)

self._setup_template(self.template)

self.operators = []
Expand Down Expand Up @@ -622,7 +650,7 @@ def fit(self, features, target, sample_weight=None, groups=None):
Returns a copy of the fitted TPOT object

"""
self._fit_init()
self._fit_init(multi_output_target=len(target.shape) > 1 and target.shape[1] > 1)
features, target = self._check_dataset(features, target, sample_weight)


Expand Down Expand Up @@ -792,10 +820,11 @@ def _update_top_pipeline(self):
if not self._optimized_pipeline:
raise RuntimeError('There was an error in the TPOT optimization '
'process. This could be because the data was '
'not formatted properly, or because data for '
'not formatted properly, because data for '
'a regression problem was provided to the '
'TPOTClassifier object. Please make sure you '
'passed the data to TPOT correctly.')
'TPOTClassifier object, or an error in a '
'custom scoring function. Please make sure '
'you passed the data to TPOT correctly.')
else:
pareto_front_wvalues = [pipeline_scores.wvalues[1] for pipeline_scores in self._pareto_front.keys]
if not self._last_optimized_pareto_front:
Expand Down Expand Up @@ -1157,7 +1186,7 @@ def _check_dataset(self, features, target, sample_weight=None):

try:
if target is not None:
X, y = check_X_y(features, target, accept_sparse=True, dtype=None)
X, y = check_X_y(features, target, accept_sparse=True, dtype=None, multi_output=len(target.shape) > 1 and target.shape[1] > 1)
if self._imputed:
return X, y
else:
Expand Down
2 changes: 1 addition & 1 deletion tpot/operator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def op_type(cls):
for dkey, dval in prange.items():
dep_import_str, dep_op_str, dep_op_obj = source_decode(dkey, verbose=verbose)
if dep_import_str in import_hash:
import_hash[import_str].append(dep_op_str)
import_hash[dep_import_str].append(dep_op_str)
else:
import_hash[dep_import_str] = [dep_op_str]
dep_op_list[pname] = dep_op_str
Expand Down