-
Notifications
You must be signed in to change notification settings - Fork 858
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
218 additions
and
1 deletion.
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
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,114 @@ | ||
# Sebastian Raschka 2014-2018 | ||
# mlxtend Machine Learning Library Extensions | ||
# Author: Sebastian Raschka <sebastianraschka.com> | ||
# | ||
# License: BSD 3 clause | ||
|
||
import sys | ||
from mlxtend.evaluate import paired_ttest_kfold_cv | ||
from mlxtend.utils import assert_raises | ||
from mlxtend.data import iris_data | ||
from mlxtend.data import boston_housing_data | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.linear_model import Lasso | ||
from sklearn.linear_model import Ridge | ||
from sklearn.tree import DecisionTreeClassifier | ||
from sklearn.model_selection import train_test_split | ||
|
||
|
||
def test_classifier_defaults(): | ||
X, y = iris_data() | ||
clf1 = LogisticRegression(random_state=1) | ||
clf2 = DecisionTreeClassifier(random_state=1) | ||
|
||
X_train, X_test, y_train, y_test = \ | ||
train_test_split(X, y, test_size=0.25, | ||
random_state=123) | ||
|
||
score1 = clf1.fit(X_train, y_train).score(X_test, y_test) | ||
score2 = clf2.fit(X_train, y_train).score(X_test, y_test) | ||
|
||
assert round(score1, 2) == 0.97 | ||
assert round(score2, 2) == 0.95 | ||
|
||
t, p = paired_ttest_kfold_cv(estimator1=clf1, | ||
estimator2=clf2, | ||
X=X, y=y, | ||
random_seed=1) | ||
|
||
assert round(t, 3) == -1.861, t | ||
assert round(p, 3) == 0.096, p | ||
|
||
# change maxdepth of decision tree classifier | ||
|
||
clf2 = DecisionTreeClassifier(max_depth=1, random_state=1) | ||
|
||
score3 = clf2.fit(X_train, y_train).score(X_test, y_test) | ||
|
||
assert round(score3, 2) == 0.63 | ||
|
||
t, p = paired_ttest_kfold_cv(estimator1=clf1, | ||
estimator2=clf2, | ||
X=X, y=y, | ||
random_seed=1) | ||
|
||
assert round(t, 3) == 13.491, t | ||
assert round(p, 3) == 0.000, p | ||
|
||
|
||
def test_scoring(): | ||
X, y = iris_data() | ||
clf1 = LogisticRegression(random_state=1) | ||
clf2 = DecisionTreeClassifier(random_state=1) | ||
|
||
X_train, X_test, y_train, y_test = \ | ||
train_test_split(X, y, test_size=0.25, | ||
random_state=123) | ||
|
||
score1 = clf1.fit(X_train, y_train).score(X_test, y_test) | ||
score2 = clf2.fit(X_train, y_train).score(X_test, y_test) | ||
|
||
assert round(score1, 2) == 0.97 | ||
assert round(score2, 2) == 0.95 | ||
|
||
t, p = paired_ttest_kfold_cv(estimator1=clf1, | ||
estimator2=clf2, | ||
X=X, y=y, | ||
scoring='accuracy', | ||
random_seed=1) | ||
|
||
assert round(t, 3) == -1.861, t | ||
assert round(p, 3) == 0.096, p | ||
|
||
t, p = paired_ttest_kfold_cv(estimator1=clf1, | ||
estimator2=clf2, | ||
X=X, y=y, | ||
scoring='f1_macro', | ||
random_seed=1) | ||
|
||
assert round(t, 3) == -1.872, t | ||
assert round(p, 3) == 0.094, p | ||
|
||
|
||
def test_regressor(): | ||
X, y = boston_housing_data() | ||
reg1 = Lasso(random_state=1) | ||
reg2 = Ridge(random_state=1) | ||
|
||
X_train, X_test, y_train, y_test = \ | ||
train_test_split(X, y, test_size=0.25, | ||
random_state=123) | ||
|
||
score1 = reg1.fit(X_train, y_train).score(X_test, y_test) | ||
score2 = reg2.fit(X_train, y_train).score(X_test, y_test) | ||
|
||
assert round(score1, 2) == 0.66, score1 | ||
assert round(score2, 2) == 0.68, score2 | ||
|
||
t, p = paired_ttest_kfold_cv(estimator1=reg1, | ||
estimator2=reg2, | ||
X=X, y=y, | ||
random_seed=1) | ||
|
||
assert round(t, 3) == -0.549, t | ||
assert round(p, 3) == 0.596, p |
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