SpaceComposer.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 class that from a given list of pipeline steps
  7. composes a space to be passed in the GridsearchPipelineSelector
  8. or HyperoptPipelineSelector classes.
  9. A classic list of steps would be: [encoders, transformers, selectors, models]
  10. """
  11. from sklearn.pipeline import Pipeline
  12. from hyperopt import hp
  13. from itertools import product
  14. class SpaceComposer:
  15. """
  16. A class that from a given list of pipeline steps
  17. composes a space to be passed to GridsearchPipelineSelector
  18. or HyperoptPipelineSelector.
  19. """
  20. def compose_gridsearch_space(self, step_list: list) -> list:
  21. """
  22. Composes a hyperparameter space for input to the
  23. GridsearchPipelineSelector class.
  24. :param step_list: a classic list of steps would be
  25. [encoders, transformers, selectors, models],
  26. where, for example, selectors is a list
  27. of sklearn feature selectors, each selector given as a dict:
  28. for example {"name": "kbest",
  29. "object": SelectPercentile(),
  30. "params": {
  31. "percentile":
  32. [5, 10, 20],
  33. "score_func":
  34. [f_classif, chi2, mutual_info_classif]}}
  35. :return: a list of dictionaries of form
  36. {"name": NAME, "pipeline": PIPELINE, "params": PARAMS}
  37. """
  38. space = []
  39. step_combinations = product(*[step for step in
  40. step_list if len(step) > 0])
  41. for step_combination in step_combinations:
  42. space_element = {}
  43. space_element["name"] = "_".join([step["name"]
  44. for step in step_combination])
  45. space_element["pipeline"] = Pipeline(
  46. [(step["name"], step["object"])
  47. for step in step_combination])
  48. space_element["params"] =\
  49. {step["name"] + "__" + param_name: param_dist
  50. for step in step_combination
  51. for param_name, param_dist
  52. in step["params"].items()}
  53. space.append(space_element)
  54. return space
  55. def compose_hyperopt_space(self, step_list: list) -> hp.choice:
  56. """
  57. Composes a hyperopt space from a list of steps.
  58. A classic list of steps would be
  59. [encoders, transformers, selectors, models],
  60. where, for example, selectors is a list
  61. of sklearn feature selectors, each selector given as a dict:
  62. for example {"name": "kbest",
  63. "object": SelectPercentile(),
  64. "params": {
  65. "percentile":
  66. 3 + hp.randint("kbest__percentile", 200),
  67. "score_func":
  68. hp.choice("kbest__score_func",
  69. [f_classif, chi2, mutual_info_classif])}}
  70. """
  71. return hp.choice("pipelines", self.compose_gridsearch_space(step_list))