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

Select between ._fit() of .fit() depending on the sklearn version used #16

Merged
merged 5 commits into from
Jan 22, 2024
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
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### 0.3.2 - Fixed compliance with sklearn 1.3.0

* Fixed `AttributeError: 'super' object has no attribute 'fit' `.
PR [#16](https://github.com/smarie/python-m5p/pull/16) by [lccatala](https://github.com/lccatala)

### 0.3.1 - Fixed compliance with sklearn 1.1.0

* Fixed `TypeError: fit() got an unexpected keyword argument 'X_idx_sorted'`.
Expand Down
15 changes: 14 additions & 1 deletion src/m5py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
from sklearn.tree._tree import DOUBLE
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted
from sklearn import __version__ as sklearn_version

from m5py.linreg_utils import linreg_model_to_text, DeNormalizableMixIn, DeNormalizableLinearRegression

from packaging.version import Version

SKLEARN_VERSION = Version(sklearn_version)
SKLEARN13_OR_GREATER = SKLEARN_VERSION >= Version("1.3.0")


__all__ = ["M5Base", "M5Prime"]

Expand Down Expand Up @@ -210,8 +216,15 @@ def fit(self, X, y: np.ndarray, sample_weight=None, check_input=True, X_idx_sort
if self.use_smoothing not in [False, np.bool_(False), "installed", "on_prediction"]:
raise ValueError("use_smoothing: Unexpected value: %s, please report it as issue." % self.use_smoothing)


# (1) Build the initial tree as usual
super(M5Base, self).fit(X, y, sample_weight=sample_weight, check_input=check_input)

# Get the correct fit method name based on the sklearn version used
fit_method_name = "_fit" if SKLEARN13_OR_GREATER else "fit"

fit_method = getattr(super(M5Base, self), fit_method_name)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: readability: please move all new lines after the comment, or all lines before the comment.

fit_method(X, y, sample_weight=sample_weight, check_input=check_input)


if self.debug_prints:
logger.debug("(debug_prints) Initial tree:")
Expand Down
Loading