Skip to content
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ classifiers = [
requires-python = ">=3.8,<3.14"
dependencies = [
"scikit-learn>=1.2.2",
"typing-extensions>=4.1.0; python_full_version < '3.11'"
]

[dependency-groups]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
scikit-learn>=1.2.2
typing-extensions>=4.1.0; python_version < "3.11"
46 changes: 32 additions & 14 deletions src/linearboost/linear_boost.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from __future__ import annotations

import sys
import warnings
from numbers import Integral, Real

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self

import numpy as np
from sklearn.base import clone
from sklearn.ensemble import AdaBoostClassifier
Expand Down Expand Up @@ -65,13 +72,18 @@ class LinearBoostClassifier(AdaBoostClassifier):

algorithm : {'SAMME', 'SAMME.R'}, default='SAMME'
If 'SAMME' then use the SAMME discrete boosting algorithm.
If 'SAMME.R' then use the SAMME.R real boosting algorithm.
If 'SAMME.R' then use the SAMME.R real boosting algorithm
(only available in scikit-learn < 1.6).
The SAMME.R algorithm typically converges faster than SAMME,
achieving a lower test error with fewer boosting iterations.

.. deprecated:: sklearn 1.6
`algorithm` is deprecated and will be removed in sklearn 1.8. This
estimator only implements the 'SAMME' algorithm.
.. deprecated:: scikit-learn 1.4
`"SAMME.R"` is deprecated and will be removed in scikit-learn 1.6.
'"SAMME"' will become the default.

.. deprecated:: scikit-learn 1.6
`algorithm` is deprecated and will be removed in scikit-learn 1.8.
This estimator only implements the 'SAMME' algorithm in scikit-learn >= 1.6.

scaler : str, default='minmax'
Specifies the scaler to apply to the data. Options include:
Expand Down Expand Up @@ -111,21 +123,21 @@ class LinearBoostClassifier(AdaBoostClassifier):
where:
- y_true: Ground truth (correct) target values.
- y_pred: Estimated target values.
- sample_weight: Sample weights.
- sample_weight: Sample weights (optional).

Attributes
----------
estimator_ : estimator
The base estimator (SEFR) from which the ensemble is grown.

.. versionadded:: sklearn 1.2
.. versionadded:: scikit-learn 1.2
`base_estimator_` was renamed to `estimator_`.

base_estimator_ : estimator
The base estimator from which the ensemble is grown.

.. deprecated:: sklearn 1.2
`base_estimator_` is deprecated and will be removed in sklearn 1.4.
.. deprecated:: scikit-learn 1.2
`base_estimator_` is deprecated and will be removed in scikit-learn 1.4.
Use `estimator_` instead.

estimators_ : list of classifiers
Expand Down Expand Up @@ -176,10 +188,9 @@ class LinearBoostClassifier(AdaBoostClassifier):
_parameter_constraints: dict = {
"n_estimators": [Interval(Integral, 1, None, closed="left")],
"learning_rate": [Interval(Real, 0, None, closed="neither")],
"algorithm": [
StrOptions({"SAMME", "SAMME.R"}),
Hidden(StrOptions({"deprecated"})),
],
"algorithm": [StrOptions({"SAMME"}), Hidden(StrOptions({"deprecated"}))]
if SKLEARN_V1_6_OR_LATER
else [StrOptions({"SAMME", "SAMME.R"})],
"scaler": [StrOptions({s for s in _scalers})],
"class_weight": [
StrOptions({"balanced_subsample", "balanced"}),
Expand Down Expand Up @@ -257,7 +268,7 @@ def _check_X_y(self, X, y) -> tuple[np.ndarray, np.ndarray]:

return X, y

def fit(self, X, y, sample_weight=None) -> "LinearBoostClassifier":
def fit(self, X, y, sample_weight=None) -> Self:
X, y = self._check_X_y(X, y)
self.classes_ = np.unique(y)
self.n_classes_ = self.classes_.shape[0]
Expand Down Expand Up @@ -291,7 +302,14 @@ def fit(self, X, y, sample_weight=None) -> "LinearBoostClassifier":
else:
sample_weight = expanded_class_weight

return super().fit(X_transformed, y, sample_weight)
with warnings.catch_warnings():
if SKLEARN_V1_6_OR_LATER:
warnings.filterwarnings(
"ignore",
category=FutureWarning,
message=".*parameter 'algorithm' is deprecated.*",
)
return super().fit(X_transformed, y, sample_weight)

def _boost(self, iboost, X, y, sample_weight, random_state):
estimator = self._make_estimator(random_state=random_state)
Expand Down
9 changes: 8 additions & 1 deletion src/linearboost/sefr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from __future__ import annotations

import sys

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self

import numpy as np
from sklearn.base import BaseEstimator
from sklearn.linear_model._base import LinearClassifierMixin
Expand Down Expand Up @@ -139,7 +146,7 @@ def _check_X_y(self, X, y) -> tuple[np.ndarray, np.ndarray]:
return X, y

@_fit_context(prefer_skip_nested_validation=True)
def fit(self, X, y, sample_weight=None) -> "SEFR":
def fit(self, X, y, sample_weight=None) -> Self:
"""
Fit the model according to the given training data.

Expand Down
15 changes: 14 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.