Skip to content

Commit

Permalink
Merge branch 'master' into issue_1085
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Mar 30, 2024
2 parents 7cfd45b + e82c9c5 commit f018a8d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
isort -p mlxtend --check --diff --line-length 88 --multi-line 3 --py 39 --profile black mlxtend/*
black --check --diff mlxtend/*
# exit-zero treats all errors as warnings.
flake8 . --config=.flake8 --count --exit-zero --statistics
flake8 . --config=.flake8 --count --exit-zero --statistics
14 changes: 14 additions & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ The CHANGELOG for the current development version is available at



### Version 0.23.2 (TBD)

##### Downloads

- [Source code (zip)](https://github.com/rasbt/mlxtend/archive/v0.23.2.zip)

- [Source code (tar.gz)](https://github.com/rasbt/mlxtend/archive/v0.23.2.tar.gz)

##### Changes

- Add `n_classes_` attribute to stacking classifiers for compatibility with scikit-learn 1.3 ([#1091](https://github.com/rasbt/mlxtend/issues/1091)



### Version 0.23.1 (5 Jan 2024)

##### Downloads
Expand Down
2 changes: 1 addition & 1 deletion mlxtend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#
# License: BSD 3 clause

__version__ = "0.23.1"
__version__ = "0.23.2dev"
11 changes: 11 additions & 0 deletions mlxtend/classifier/stacking_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
from scipy import sparse
from sklearn.base import TransformerMixin, clone
from sklearn.preprocessing import LabelEncoder

from ..externals.estimator_checks import check_is_fitted
from ..externals.name_estimators import _name_estimators
Expand Down Expand Up @@ -95,6 +96,9 @@ class StackingClassifier(_BaseXComposition, _BaseStackingClassifier, Transformer
Fitted classifiers (clones of the original classifiers)
meta_clf_ : estimator
Fitted meta-classifier (clone of the original meta-estimator)
classes_ : ndarray of shape (n_classes,) or list of ndarray if `y` \
is of type `"multilabel-indicator"`.
Class labels.
train_meta_features : numpy array, shape = [n_samples, n_classifiers]
meta-features for training data, where n_samples is the
number of samples
Expand Down Expand Up @@ -175,6 +179,13 @@ def fit(self, X, y, sample_weight=None):
self.clfs_ = self.classifiers
self.meta_clf_ = self.meta_classifier

if y.ndim > 1:
self._label_encoder = [LabelEncoder().fit(yk) for yk in y.T]
self.classes_ = [le.classes_ for le in self._label_encoder]
else:
self._label_encoder = LabelEncoder().fit(y)
self.classes_ = self._label_encoder.classes_

if self.fit_base_estimators:
if self.verbose > 0:
print("Fitting %d classifiers..." % (len(self.classifiers)))
Expand Down
11 changes: 11 additions & 0 deletions mlxtend/classifier/stacking_cv_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sklearn.base import TransformerMixin, clone
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection._split import check_cv
from sklearn.preprocessing import LabelEncoder

from ..externals.estimator_checks import check_is_fitted
from ..externals.name_estimators import _name_estimators
Expand Down Expand Up @@ -129,6 +130,9 @@ class StackingCVClassifier(
Fitted classifiers (clones of the original classifiers)
meta_clf_ : estimator
Fitted meta-classifier (clone of the original meta-estimator)
classes_ : ndarray of shape (n_classes,) or list of ndarray if `y` \
is of type `"multilabel-indicator"`.
Class labels.
train_meta_features : numpy array, shape = [n_samples, n_classifiers]
meta-features for training data, where n_samples is the
number of samples
Expand Down Expand Up @@ -220,6 +224,13 @@ def fit(self, X, y, groups=None, sample_weight=None):
if self.verbose > 0:
print("Fitting %d classifiers..." % (len(self.classifiers)))

if y.ndim > 1:
self._label_encoder = [LabelEncoder().fit(yk) for yk in y.T]
self.classes_ = [le.classes_ for le in self._label_encoder]
else:
self._label_encoder = LabelEncoder().fit(y)
self.classes_ = self._label_encoder.classes_

final_cv = check_cv(self.cv, y, classifier=self.stratify)
if isinstance(self.cv, int):
# Override shuffle parameter in case of self generated
Expand Down
15 changes: 12 additions & 3 deletions mlxtend/classifier/tests/test_stacking_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#
# License: BSD 3 clause

import platform
import random

import numpy as np
Expand Down Expand Up @@ -549,8 +550,12 @@ def test_decision_function():

if Version(sklearn_version) < Version("0.21"):
assert scores_mean == 0.96, scores_mean
else:
assert scores_mean == 0.93, scores_mean

min_allowed_score = 0.92
max_allowed_score = 0.95
assert (
min_allowed_score <= scores_mean <= max_allowed_score
), "Score is out of the allowed range."

# another test
meta = SVC(decision_function_shape="ovo")
Expand All @@ -565,7 +570,11 @@ def test_decision_function():
if Version(sklearn_version) < Version("0.22"):
assert scores_mean == 0.95, scores_mean
else:
assert scores_mean == 0.94, scores_mean
min_allowed_score = 0.92
max_allowed_score = 0.95
assert (
min_allowed_score <= scores_mean <= max_allowed_score
), "Score is out of the allowed range."


def test_drop_col_unsupported():
Expand Down
1 change: 1 addition & 0 deletions mlxtend/regressor/tests/test_stacking_cv_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ def test_weight_unsupported_with_no_weight():
stack.fit(X1, y).predict(X1)


@pytest.mark.skip(reason="scikit-learn implemented a StackingRegressor in 0.22.")
def test_gridsearch_replace_mix():
svr_lin = SVR(kernel="linear", gamma="auto")
ridge = Ridge(random_state=1)
Expand Down

0 comments on commit f018a8d

Please sign in to comment.