123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Wed Sep 30 13:54:04 2020
- @author: tanya
- @description: a function that from a given list of pipeline steps
- composes a space to be passed in the HyperoptPipelineSelection class.
- A classic list of steps would be: [encoders, transformers, selectors, models]
- """
- from sklearn.pipeline import Pipeline
- from hyperopt import hp
- from itertools import product
- def space_composer(step_list: list) -> hp.choice:
- """
- :param step_list: list of pipeline steps
- of the form [encoders, transformers, selectors, models]
- each element of step_list is a list of dictionaries
- of the form {"name": NAME, "object": OBJECT, "params": PARAMS}
- :return: hp.choice object of pipelines to choose from
- when passed to the HyperoptPipelineSelection class
- """
- pipelines = []
- step_combinations = product(*[step for step in
- step_list if len(step) > 0])
- for step_combination in step_combinations:
- pipeline_dist = {}
- pipeline_dist["name"] = "_".join([step["name"]
- for step in step_combination])
- pipeline_dist["pipeline"] = Pipeline([(step["name"], step["object"])
- for step in step_combination]),
- pipeline_dist["params"] = {step["name"] + "__" + param_name: param_dist
- for step in step_combination
- for param_name, param_dist
- in step["params"].items()}
- pipelines.append(pipeline_dist)
- return hp.choice("pipelines", pipelines)
|