SpaceComposer.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Sep 30 13:54:04 2020
  5. @author: tanya
  6. @description: a function that from a given list of pipeline steps
  7. composes a space to be passed in the HyperoptPipelineSelection class.
  8. A classic list of steps would be: [encoders, transformers, selectors, models]
  9. """
  10. from sklearn.pipeline import Pipeline
  11. from hyperopt import hp
  12. from itertools import product
  13. def space_composer(step_list: list) -> hp.choice:
  14. """
  15. :param step_list: list of pipeline steps
  16. of the form [encoders, transformers, selectors, models]
  17. each element of step_list is a list of dictionaries
  18. of the form {"name": NAME, "object": OBJECT, "params": PARAMS}
  19. :return: hp.choice object of pipelines to choose from
  20. when passed to the HyperoptPipelineSelection class
  21. """
  22. pipelines = []
  23. step_combinations = product(*[step for step in
  24. step_list if len(step) > 0])
  25. for step_combination in step_combinations:
  26. pipeline_dist = {}
  27. pipeline_dist["name"] = "_".join([step["name"]
  28. for step in step_combination])
  29. pipeline_dist["pipeline"] = Pipeline([(step["name"], step["object"])
  30. for step in step_combination]),
  31. pipeline_dist["params"] = {step["name"] + "__" + param_name: param_dist
  32. for step in step_combination
  33. for param_name, param_dist
  34. in step["params"].items()}
  35. pipelines.append(pipeline_dist)
  36. return hp.choice("pipelines", pipelines)