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, PipelineBranch
from photonai.optimization.Hyperparameters import FloatRange, IntegerRange, Categorical
from photonai.investigator.Investigator import Investigator
from photonai.configuration.Register import PhotonRegister
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='grid_search',
                    metrics=['accuracy', 'precision', 'recall'],
                    best_config_metric='f1_score',
                    outer_cv=KFold(n_splits=3),
                    inner_cv=KFold(n_splits=10),
                    verbosity=1)

# BRANCH WITH QUANTILTRANSFORMER AND DECISIONTREECLASSIFIER
tree_qua_branch = PipelineBranch('tree_branch')
tree_qua_branch += PipelineElement('QuantileTransformer')
tree_qua_branch += PipelineElement('DecisionTreeClassifier',{'min_samples_split': IntegerRange(2, 4)},criterion='gini')

# BRANCH WITH MinMaxScaler AND DecisionTreeClassifier
svm_mima_branch = PipelineBranch('svm_branch')
svm_mima_branch += PipelineElement('MinMaxScaler')
svm_mima_branch += PipelineElement('SVC', {'kernel': Categorical(['rbf', 'linear']),
                                      'C':2.0},gamma='auto')

# BRANCH WITH StandardScaler AND KNeighborsClassifier
knn_sta_branch = PipelineBranch('neighbour_branch')
knn_sta_branch += PipelineElement('StandardScaler')
knn_sta_branch += PipelineElement('KNeighborsClassifier')

# voting = True to mean the result of every branch
my_pipe_stack = PipelineStacking('final_stack', voting=True)
my_pipe_stack += tree_qua_branch
my_pipe_stack += svm_mima_branch
my_pipe_stack += knn_sta_branch

my_pipe += my_pipe_stack

my_pipe += PipelineElement('LogisticRegression',solver='lbfgs')

my_pipe.fit(X, y)