PHOTONAI Switch

The PipelineSwitch element acts like an OR-Operator and decides which element performs best. Currently, you can only optimize the PipelineSwitch using Grid Search, Random Grid Search and smac3

In this example, we add two different transformer elements and two different estimators, and PHOTONAI will evaluate the best choices including the respective hyperparameters.

           
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import KFold

from photonai.base import Hyperpipe, PipelineElement, Switch, OutputSettings

# GET DATA
X, y = load_breast_cancer(True)

# CREATE HYPERPIPE
my_pipe = Hyperpipe('basic_switch_pipe',
                    optimizer='random_grid_search',
                    optimizer_params={'n_configurations': 10},
                    metrics=['accuracy', 'precision', 'recall'],
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=5),
                    verbosity=1,
                    output_settings=OutputSettings(project_folder='./tmp/'))

# Transformer Switch : StandardScaler OR RobustScaler ??
my_pipe += Switch('TransformerSwitch',
                  [PipelineElement('StandardScaler'),
                   PipelineElement('RobustScaler')])

# Estimator Switch : SVC OR DecisionTree ??
svm = PipelineElement('SVC')
tree = PipelineElement('DecisionTreeClassifier')
my_pipe += Switch('EstimatorSwitch', [svm, tree])

my_pipe.fit(X, y)