Stacking Element

Stacking is a way of combining multiple models, that introduces the concept of a meta learner, sometimes known as Ensemble Learning. It is like a vertical stack of pipeline elements or sub-pipelines.

What you need is a container class called PipelineStacking, that vertically stacks pipeline elements. It basically works like an AND-Operator. You can either stack PipeleineElements, or you use a class called PipelineBranch, to create subpipelines and and makes the data flow in parallel through all of them.

           
from photonai.base.PhotonBase import Hyperpipe, PipelineElement, PipelineStacking
from photonai.optimization.Hyperparameters import FloatRange, IntegerRange, Categorical
from sklearn.model_selection import KFold

from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(True)


my_pipe = Hyperpipe('basic_stacking',
                    optimizer='sk_opt',
                    metrics=['accuracy', 'precision', 'recall'],
                    best_config_metric='accuracy',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=10),
                    verbosity=1)

my_pipe += PipelineElement('StandardScaler')
my_pipe_stack = PipelineStacking('final_stack', voting=False)
my_pipe_stack += PipelineElement('DecisionTreeClassifier', hyperparameters={'criterion': ['gini'],
                                                                            'min_samples_split': IntegerRange(2, 4)})

my_pipe_stack += PipelineElement('LinearSVC', hyperparameters={'C': FloatRange(0.5, 25)})
my_pipe += my_pipe_stack

my_pipe += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear'])})
my_pipe.fit(X, y)