Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fix for sklearn 0.19 _BasePipeline class no longer exists #51

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xcessiv/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ def to_dict(self):
rv = dict(self.kwargs or ())
rv['message'] = self.message
return rv

def __repr__(self):
return self.message
8 changes: 6 additions & 2 deletions xcessiv/stacker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from sklearn.pipeline import _BasePipeline
import sklearn
if sklearn.__version__.startswith('0.18'):
from sklearn.pipeline import _BasePipeline as bp
else:
from sklearn.utils.metaestimators import _BaseComposition as bp
import numpy as np


class XcessivStackedEnsemble(_BasePipeline):
class XcessivStackedEnsemble(bp):
"""Contains the class for the Xcessiv stacked ensemble"""
def __init__(self, base_learners, meta_feature_generators,
secondary_learner, cv_function):
Expand Down
5 changes: 3 additions & 2 deletions xcessiv/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ def test_verify_estimator_class(self):
'min_weight_fraction_leaf': 0.0,
'criterion': 'gini',
'random_state': None,
'min_impurity_split': 1e-07,
'min_impurity_split': None,
'min_impurity_decrease': 0.0,
'max_features': 'auto',
'max_depth': None,
'class_weight': None
}

def test_non_serializable_parameters(self):
pipeline = Pipeline((('pca', PCA()), ('rf', RandomForestClassifier())))
pipeline = Pipeline([('pca', PCA()), ('rf', RandomForestClassifier())])
performance_dict, hyperparameters = functions.verify_estimator_class(
pipeline,
'predict_proba',
Expand Down
52 changes: 52 additions & 0 deletions xcessiv/tests/test_stacker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import absolute_import, print_function, division, unicode_literals
import unittest
import numpy as np
from xcessiv import stacker
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score


class TestStacker(unittest.TestCase):
def setUp(self):
bl1 = RandomForestClassifier(random_state=8)
bl2 = LogisticRegression()
bl3 = RandomForestClassifier(max_depth=10, random_state=10)

meta_est = LogisticRegression()

skf = StratifiedKFold(random_state=8).split

self.stacked_ensemble = stacker.XcessivStackedEnsemble(
[bl1, bl2, bl3],
['predict', 'predict_proba', 'predict_proba'],
meta_est,
skf
)

def test_fit_and_process_using_meta_feature_generator(self):
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=8)

self.stacked_ensemble.fit(X_train, y_train)

preds = self.stacked_ensemble._process_using_meta_feature_generator(X_test, 'predict')
assert np.round(accuracy_score(y_test, preds), 3) == 0.868

probas = self.stacked_ensemble._process_using_meta_feature_generator(X_test, 'predict_proba')
preds = np.argmax(probas, axis=1)
assert np.round(accuracy_score(y_test, preds), 3) == 0.868

def test_get_params(self):
self.stacked_ensemble.get_params()

def test_set_params(self):
self.stacked_ensemble.set_params(bl0__random_state=20)
assert self.stacked_ensemble.get_params()['bl0__random_state'] == 20
assert self.stacked_ensemble.get_params()['bl0'].get_params()['random_state'] == 20

self.stacked_ensemble.set_params(**{'secondary-learner__C': 1.5})
assert self.stacked_ensemble.get_params()['secondary-learner__C'] == 1.5
assert self.stacked_ensemble.get_params()['secondary-learner'].get_params()['C'] == 1.5