Switch Element

The PipelineSwitch element acts like an OR-Operator and decides which element performs best. Currently, you can only optimize the PipelineSwitch using GridSearch (we are working on it..)

In this example, we add three different estimators to the SwitchElement and PHOTON evaluates the best estimator in [SVC,RFC,KNN] and which hyperparameter configuration to choose.

           
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineSwitch
from photonai.optimization.Hyperparameters import Categorical
from photonai.configuration.Register import PhotonRegister
from sklearn.model_selection import KFold
from imblearn.datasets import fetch_datasets


# WE USE THE wine_quality DATA SET FROM IMBLEARN WITH 26:1 RATIO
wine_quality = fetch_datasets()['wine_quality']
X, y = wine_quality.data, wine_quality.target # ratio class 0: 0.04%, class 1: 0.96%


my_pipe = Hyperpipe('basic_switch_pipe',
                    optimizer='grid_search',
                    metrics=['accuracy', 'f1_score'],
                    best_config_metric='f1_score',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=10),
                    verbosity=1)

# NOW FIND OUT MORE ABOUT A SPECIFIC ELEMENT
PhotonRegister.info('KNeighborsClassifier')

# ADD ELEMENTS TO YOUR PIPELINE
my_pipe += PipelineElement('StandardScaler')

svm = PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear'])},C=1)
tree = PipelineElement('RandomForestClassifier',n_estimators=10,criterion="gini")
knn = PipelineElement('KNeighborsClassifier',hyperparameters={'n_neighbors':Categorical([5,10]),'p':Categorical([2,5])})

# Add the svm, knn and tree element to the estimator_switch
switch = PipelineSwitch('estimator_switch')
switch += svm
switch += tree
switch += knn

# add the switch element to the pipe
my_pipe += switch

# NOW TRAIN YOUR PIPELINE
my_pipe.fit(X, y)