-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54_stacking_ensemble.py
25 lines (18 loc) · 987 Bytes
/
54_stacking_ensemble.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
from numpy import mean
from numpy import std
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import StackingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
X, Y = make_classification(random_state=1)
models = [('knn', KNeighborsClassifier()), ('tree', DecisionTreeClassifier())]
# cv: Determines the cross-validation splitting strategy used in
# cross_val_predict to train final_estimator. Possible inputs for cv are:
# Defaults to 5
model = StackingClassifier(models, final_estimator=LogisticRegression(), cv=3)
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
n_scores = cross_val_score(model, X, Y, scoring='accuracy', cv=cv, n_jobs=-1)
print('Mean Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))