-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
################## load packages ##################### | ||
from sklearn import datasets | ||
from sklearn import model_selection | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from sklearn.naive_bayes import GaussianNB | ||
from sklearn.ensemble import RandomForestClassifier | ||
from mlxtend.classifier import StackingClassifier | ||
|
||
|
||
################## load data ##################### | ||
iris = datasets.load_iris() | ||
x, y = iris.data[:, 1:3], iris.target | ||
|
||
|
||
################## define classifier ##################### | ||
clf1 = KNeighborsClassifier(n_neighbors = 1) | ||
|
||
clf2 = RandomForestClassifier(random_state = 1) | ||
|
||
clf3 = GaussianNB() | ||
|
||
lr = LogisticRegression() | ||
|
||
sclf = StackingClassifier(classifiers = [clf1, clf2, clf3], meta_classifier = lr) | ||
|
||
|
||
################## class result ##################### | ||
for clf, label in zip([clf1, clf2, clf3, sclf], | ||
['KNN', | ||
'Random Forest', | ||
'Naive Bayes', | ||
'StackingClassifier']): | ||
|
||
scores = model_selection.cross_val_score(clf, x, y, cv = 3, scoring='accuracy') | ||
|
||
print("Accuracy: %0.2f (+/- %0.2f) [%s]" | ||
% (scores.mean(), scores.std(), label)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
################## load packages ##################### | ||
from sklearn import datasets | ||
from sklearn import model_selection | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from sklearn.naive_bayes import GaussianNB | ||
from sklearn.ensemble import RandomForestClassifier | ||
from mlxtend.classifier import StackingClassifier | ||
|
||
|
||
################## load data ##################### | ||
iris = datasets.load_iris() | ||
x, y = iris.data[:, 1:3], iris.target | ||
|
||
|
||
################## define classifier ##################### | ||
clf1 = KNeighborsClassifier(n_neighbors = 1) | ||
|
||
clf2 = RandomForestClassifier(random_state = 1) | ||
|
||
clf3 = GaussianNB() | ||
|
||
lr = LogisticRegression() | ||
|
||
sclf = StackingClassifier(classifiers = [clf1, clf2, clf3], | ||
use_probas=True, | ||
average_probas=False, | ||
meta_classifier = lr) | ||
|
||
|
||
################## class result ##################### | ||
for clf, label in zip([clf1, clf2, clf3, sclf], | ||
['KNN', | ||
'Random Forest', | ||
'Naive Bayes', | ||
'StackingClassifier']): | ||
|
||
scores = model_selection.cross_val_score(clf, x, y, cv = 3, scoring='accuracy') | ||
|
||
print("Accuracy: %0.2f (+/- %0.2f) [%s]" | ||
% (scores.mean(), scores.std(), label)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
################## load packages ##################### | ||
from sklearn import datasets | ||
from sklearn.linear_model import LogisticRegression | ||
from mlxtend.classifier import StackingClassifier | ||
from mlxtend.feature_selection import ColumnSelector | ||
from sklearn.pipeline import make_pipeline | ||
|
||
|
||
################## load data ##################### | ||
iris = datasets.load_iris() | ||
x, y = iris.data, iris.target | ||
|
||
|
||
################## define classifier ##################### | ||
|
||
pipe1 = make_pipeline(ColumnSelector(cols=(0, 1)), | ||
LogisticRegression()) | ||
pipe2 = make_pipeline(ColumnSelector(cols=(2, 3)), | ||
LogisticRegression()) | ||
|
||
sclf = StackingClassifier(classifiers=[pipe1, pipe2], | ||
meta_classifier=LogisticRegression()) | ||
|
||
################## fit and predict ##################### | ||
sclf.fit(x, y) | ||
|
||
print(sclf.predict(x)) | ||
|
||
########### predict class probability ########### | ||
print(sclf.predict_proba(x)) |