2015-04-21 07:27:30 +08:00
|
|
|
"""Testing for the boost module (sklearn.ensemble.boost)."""
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
from sklearn.utils.testing import assert_almost_equal
|
|
|
|
|
from sklearn.utils.testing import assert_equal
|
|
|
|
|
from sklearn.linear_model import LogisticRegression
|
|
|
|
|
from sklearn.naive_bayes import GaussianNB
|
|
|
|
|
from sklearn.ensemble import RandomForestClassifier
|
|
|
|
|
from sklearn.ensemble import VotingClassifier
|
|
|
|
|
from sklearn.grid_search import GridSearchCV
|
|
|
|
|
from sklearn import datasets
|
|
|
|
|
from sklearn import cross_validation
|
|
|
|
|
from sklearn.datasets import make_multilabel_classification
|
|
|
|
|
from sklearn.svm import SVC
|
|
|
|
|
from sklearn.multiclass import OneVsRestClassifier
|
|
|
|
|
|
|
|
|
|
# Load the iris dataset and randomly permute it
|
|
|
|
|
iris = datasets.load_iris()
|
|
|
|
|
X, y = iris.data[:, 1:3], iris.target
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_majority_label_iris():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Check classification by majority label on dataset iris."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=123)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=123)
|
|
|
|
|
clf3 = GaussianNB()
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='hard')
|
|
|
|
|
scores = cross_validation.cross_val_score(eclf,
|
|
|
|
|
X,
|
|
|
|
|
y,
|
|
|
|
|
cv=5,
|
|
|
|
|
scoring='accuracy')
|
|
|
|
|
assert_almost_equal(scores.mean(), 0.95, decimal=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_tie_situation():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Check voting classifier selects smaller class label in tie situation."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=123)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=123)
|
|
|
|
|
eclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2)],
|
2015-05-08 16:10:47 +08:00
|
|
|
voting='hard')
|
2015-04-21 07:27:30 +08:00
|
|
|
assert_equal(clf1.fit(X, y).predict(X)[73], 2)
|
|
|
|
|
assert_equal(clf2.fit(X, y).predict(X)[73], 1)
|
|
|
|
|
assert_equal(eclf.fit(X, y).predict(X)[73], 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_weights_iris():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Check classification by average probabilities on dataset iris."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=123)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=123)
|
|
|
|
|
clf3 = GaussianNB()
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='soft',
|
|
|
|
|
weights=[1, 2, 10])
|
|
|
|
|
scores = cross_validation.cross_val_score(eclf,
|
|
|
|
|
X,
|
|
|
|
|
y,
|
|
|
|
|
cv=5,
|
|
|
|
|
scoring='accuracy')
|
|
|
|
|
assert_almost_equal(scores.mean(), 0.93, decimal=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_predict_on_toy_problem():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Manually check predicted class labels for toy dataset."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=123)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=123)
|
|
|
|
|
clf3 = GaussianNB()
|
|
|
|
|
|
|
|
|
|
X = np.array([[-1.1, -1.5],
|
|
|
|
|
[-1.2, -1.4],
|
|
|
|
|
[-3.4, -2.2],
|
|
|
|
|
[1.1, 1.2],
|
|
|
|
|
[2.1, 1.4],
|
|
|
|
|
[3.1, 2.3]])
|
|
|
|
|
|
|
|
|
|
y = np.array([1, 1, 1, 2, 2, 2])
|
|
|
|
|
|
|
|
|
|
assert_equal(all(clf1.fit(X, y).predict(X)), all([1, 1, 1, 2, 2, 2]))
|
|
|
|
|
assert_equal(all(clf2.fit(X, y).predict(X)), all([1, 1, 1, 2, 2, 2]))
|
|
|
|
|
assert_equal(all(clf3.fit(X, y).predict(X)), all([1, 1, 1, 2, 2, 2]))
|
|
|
|
|
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='hard',
|
|
|
|
|
weights=[1, 1, 1])
|
|
|
|
|
assert_equal(all(eclf.fit(X, y).predict(X)), all([1, 1, 1, 2, 2, 2]))
|
|
|
|
|
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='soft',
|
|
|
|
|
weights=[1, 1, 1])
|
|
|
|
|
assert_equal(all(eclf.fit(X, y).predict(X)), all([1, 1, 1, 2, 2, 2]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_predict_proba_on_toy_problem():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Calculate predicted probabilities on toy dataset."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=123)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=123)
|
|
|
|
|
clf3 = GaussianNB()
|
|
|
|
|
X = np.array([[-1.1, -1.5], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
|
|
|
|
|
y = np.array([1, 1, 2, 2])
|
|
|
|
|
|
|
|
|
|
clf1_res = np.array([[0.59790391, 0.40209609],
|
|
|
|
|
[0.57622162, 0.42377838],
|
|
|
|
|
[0.50728456, 0.49271544],
|
|
|
|
|
[0.40241774, 0.59758226]])
|
|
|
|
|
|
|
|
|
|
clf2_res = np.array([[0.8, 0.2],
|
|
|
|
|
[0.8, 0.2],
|
|
|
|
|
[0.2, 0.8],
|
|
|
|
|
[0.3, 0.7]])
|
|
|
|
|
|
|
|
|
|
clf3_res = np.array([[0.9985082, 0.0014918],
|
|
|
|
|
[0.99845843, 0.00154157],
|
|
|
|
|
[0., 1.],
|
|
|
|
|
[0., 1.]])
|
|
|
|
|
|
|
|
|
|
t00 = (2*clf1_res[0][0] + clf2_res[0][0] + clf3_res[0][0]) / 4
|
|
|
|
|
t11 = (2*clf1_res[1][1] + clf2_res[1][1] + clf3_res[1][1]) / 4
|
|
|
|
|
t21 = (2*clf1_res[2][1] + clf2_res[2][1] + clf3_res[2][1]) / 4
|
|
|
|
|
t31 = (2*clf1_res[3][1] + clf2_res[3][1] + clf3_res[3][1]) / 4
|
|
|
|
|
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='soft',
|
|
|
|
|
weights=[2, 1, 1])
|
|
|
|
|
eclf_res = eclf.fit(X, y).predict_proba(X)
|
|
|
|
|
|
|
|
|
|
assert_almost_equal(t00, eclf_res[0][0], decimal=1)
|
|
|
|
|
assert_almost_equal(t11, eclf_res[1][1], decimal=1)
|
|
|
|
|
assert_almost_equal(t21, eclf_res[2][1], decimal=1)
|
|
|
|
|
assert_almost_equal(t31, eclf_res[3][1], decimal=1)
|
2015-05-08 16:10:47 +08:00
|
|
|
|
2015-04-21 07:27:30 +08:00
|
|
|
try:
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='hard')
|
|
|
|
|
eclf.fit(X, y).predict_proba(X)
|
2015-04-22 07:01:39 +08:00
|
|
|
|
|
|
|
|
except AttributeError:
|
2015-04-21 07:27:30 +08:00
|
|
|
pass
|
|
|
|
|
else:
|
2015-04-22 07:01:39 +08:00
|
|
|
raise AssertionError('AttributeError for voting == "hard"'
|
|
|
|
|
' and with predict_proba not raised')
|
2015-04-21 07:27:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_multilabel():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Check if error is raised for multilabel classification."""
|
2015-04-21 07:27:30 +08:00
|
|
|
X, y = make_multilabel_classification(n_classes=2, n_labels=1,
|
|
|
|
|
allow_unlabeled=False,
|
|
|
|
|
random_state=123)
|
|
|
|
|
clf = OneVsRestClassifier(SVC(kernel='linear'))
|
|
|
|
|
|
|
|
|
|
eclf = VotingClassifier(estimators=[('ovr', clf)], voting='hard')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
eclf.fit(X, y)
|
|
|
|
|
except NotImplementedError:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_gridsearch():
|
2015-05-08 16:10:47 +08:00
|
|
|
"""Check GridSearch support."""
|
2015-04-21 07:27:30 +08:00
|
|
|
clf1 = LogisticRegression(random_state=1)
|
|
|
|
|
clf2 = RandomForestClassifier(random_state=1)
|
|
|
|
|
clf3 = GaussianNB()
|
|
|
|
|
eclf = VotingClassifier(estimators=[
|
|
|
|
|
('lr', clf1), ('rf', clf2), ('gnb', clf3)],
|
|
|
|
|
voting='soft')
|
|
|
|
|
|
|
|
|
|
params = {'lr__C': [1.0, 100.0],
|
2015-07-23 14:34:26 +08:00
|
|
|
'voting': ['soft', 'hard'],
|
|
|
|
|
'weights': [[0.5, 0.5, 0.5], [1.0, 0.5, 0.5]]}
|
2015-04-21 07:27:30 +08:00
|
|
|
|
|
|
|
|
grid = GridSearchCV(estimator=eclf, param_grid=params, cv=5)
|
|
|
|
|
grid.fit(iris.data, iris.target)
|