Classifier Ensemble

An ensemble is a combination of multiple base estimators. For a short introduction to ensemble methods, see Sklearn Ensemble Methods. In PHOTONAI, an estimator ensemble can be easily created by adding any number of estimators to a Stack. Afterwards, simply add a meta estimator that receives the predictions of your stack. This can be any estimator or simply a averaging or voting strategy. In this example, we used the PhotonVotingClassifier to create a final prediction.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import StratifiedKFold

from photonai.base import Hyperpipe, PipelineElement, Stack

my_pipe = Hyperpipe(name='ensemble_pipe',
                    optimizer='random_grid_search',
                    metrics=['balanced_accuracy'],
                    best_config_metric='balanced_accuracy',
                    outer_cv=StratifiedKFold(n_splits=2, shuffle=True, random_state=42),
                    inner_cv=StratifiedKFold(n_splits=2, shuffle=True, random_state=42),
                    project_folder='./tmp/')

my_pipe += PipelineElement('StandardScaler')

# setup estimator stack
est_stack = Stack(name='classifier_stack')
clf_list = ['RandomForestClassifier', 'LinearSVC', 'NuSVC', "SVC", "MLPClassifier",
            "KNeighborsClassifier", "Lasso", "PassiveAggressiveClassifier", "LogisticRegression",
            "Perceptron", "RidgeClassifier", "SGDClassifier", "GaussianProcessClassifier",
            "AdaBoostClassifier", "BaggingClassifier", "GradientBoostingClassifier"]

for clf in clf_list:
    est_stack += PipelineElement(clf)
my_pipe += est_stack

my_pipe += PipelineElement('PhotonVotingClassifier')

X, y = load_breast_cancer(return_X_y=True)
my_pipe.fit(X, y)