-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
classifier_stacking_example.py
62 lines (48 loc) · 2.12 KB
/
classifier_stacking_example.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Example of Stacking (meta ensembling)
"""
# Author: Yue Zhao <[email protected]>
# License: BSD 2 clause
import os
import sys
# temporary solution for relative imports in case combo is not installed
# if combo is installed, no need to use the following line
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
from combo.models.classifier_stacking import Stacking
from combo.utils.data import evaluate_print
import warnings
warnings.filterwarnings("ignore")
if __name__ == "__main__":
# Define data file and read X and y
random_state = 42
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
random_state=random_state)
# initialize a group of clfs
classifiers = [DecisionTreeClassifier(random_state=random_state),
LogisticRegression(random_state=random_state),
KNeighborsClassifier(),
RandomForestClassifier(random_state=random_state),
GradientBoostingClassifier(random_state=random_state)]
clf_names = ['DT', 'LR', 'KNN', 'RF', 'GBDT']
# evaluate individual classifiers
for i, clf in enumerate(classifiers):
clf.fit(X_train, y_train)
y_test_predict = clf.predict(X_test)
evaluate_print(clf_names[i] + ' | ', y_test, y_test_predict)
print()
# build a Stacking model and evaluate
clf = Stacking(classifiers, n_folds=4, shuffle_data=False,
keep_original=True, use_proba=False,
random_state=random_state)
clf.fit(X_train, y_train)
y_test_predict = clf.predict(X_test)
evaluate_print('Stacking | ', y_test, y_test_predict)