Skip to content

Commit 3876fbd

Browse files
TimotheeMathieurth
andauthored
Fix compatibility with latest version of scikit-learn (#131)
* change squared_loss to squared_error and fix check_is_fitted for fastfood * fix squared_error retrocompatibility * fix squared loss old version * fix test squared error * fix test squared error * fix example clustering bandwidth * Update sklearn_extra/robust/robust_weighted_estimator.py Co-authored-by: Roman Yurchak <[email protected]> * change support python and scikit-learn * change support python scikit for mac and windows * update requirements in README and doc * Apply suggestions from code review Co-authored-by: Roman Yurchak <[email protected]> Co-authored-by: TMat <[email protected]> Co-authored-by: Roman Yurchak <[email protected]>
1 parent 859ce5e commit 3876fbd

File tree

7 files changed

+48
-30
lines changed

7 files changed

+48
-30
lines changed

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Dependencies
3232

3333
scikit-learn-extra requires,
3434

35-
- Python (>=3.6)
36-
- scikit-learn (>=0.23), and its dependencies
35+
- Python (>=3.7)
36+
- scikit-learn (>=0.24), and its dependencies
3737

3838

3939
Installation

azure-pipelines.yml

+18-17
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ jobs:
55
vmImage: 'ubuntu-latest'
66
strategy:
77
matrix:
8-
Python36:
9-
python.version: '3.6'
10-
NUMPY_VERSION: "1.13.3"
11-
SCIPY_VERSION: "0.19.1"
12-
SKLEARN_VERSION: "0.23.0"
138
Python37:
149
python.version: '3.7'
1510
NUMPY_VERSION: "1.16.5"
1611
SCIPY_VERSION: "1.1.0"
17-
SKLEARN_VERSION: "0.23.2"
12+
SKLEARN_VERSION: "0.24.1"
1813
Python38:
1914
python.version: '3.8'
2015
NUMPY_VERSION: "1.19.4"
@@ -25,6 +20,12 @@ jobs:
2520
NUMPY_VERSION: "1.19.4"
2621
SCIPY_VERSION: "1.5.4"
2722
SKLEARN_VERSION: "nightly"
23+
Py39_sklearn1:
24+
python.version: '3.9'
25+
NUMPY_VERSION: "1.19.4"
26+
SCIPY_VERSION: "1.5.4"
27+
SKLEARN_VERSION: "1.0.0"
28+
2829
variables:
2930
OMP_NUM_THREADS: '2'
3031

@@ -70,11 +71,6 @@ jobs:
7071
vmImage: 'macOS-10.14'
7172
strategy:
7273
matrix:
73-
Python36:
74-
python.version: '3.6'
75-
NUMPY_VERSION: "1.13.3"
76-
SCIPY_VERSION: "0.19.1"
77-
SKLEARN_VERSION: "0.23.2"
7874
Python37:
7975
python.version: '3.7'
8076
NUMPY_VERSION: "1.16.5"
@@ -83,6 +79,11 @@ jobs:
8379
Python38:
8480
python.version: '3.8'
8581
SKLEARN_VERSION: "*"
82+
Py39_sklearn1:
83+
python.version: '3.9'
84+
NUMPY_VERSION: "1.19.4"
85+
SCIPY_VERSION: "1.5.4"
86+
SKLEARN_VERSION: "1.0.0"
8687
variables:
8788
OMP_NUM_THREADS: '2'
8889

@@ -126,18 +127,18 @@ jobs:
126127
vmImage: 'vs2017-win2016'
127128
strategy:
128129
matrix:
129-
Python36:
130-
python_ver: '36'
131-
python.version: '3.6'
132-
NUMPY_VERSION: "1.13.3"
133-
SCIPY_VERSION: "1.0.1"
134-
SKLEARN_VERSION: "0.23.2"
135130
Python38:
136131
python_ver: '38'
137132
python.version: '3.8'
138133
NUMPY_VERSION: "1.18.2"
139134
SCIPY_VERSION: "1.4.1"
140135
SKLEARN_VERSION: "0.24.1"
136+
Py39_sklearn1:
137+
python.version: '3.9'
138+
NUMPY_VERSION: "1.19.4"
139+
SCIPY_VERSION: "1.5.4"
140+
SKLEARN_VERSION: "1.0.0"
141+
141142
variables:
142143
OMP_NUM_THREADS: '2'
143144

doc/install.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Dependencies
55
^^^^^^^^^^^^
66

77
scikit-learn-extra requires,
8-
9-
- Python (>=3.6)
10-
- scikit-learn (>=0.23), and its dependencies
8+
9+
- Python (>=3.7)
10+
- scikit-learn (>=0.24), and its dependencies
1111

1212

1313
User installation
@@ -22,7 +22,7 @@ Latest release can be installed with conda,
2222
or from PyPi with,
2323

2424
.. code::
25-
25+
2626
pip install scikit-learn-extra
2727
2828
Latest development version can be installed with,

examples/cluster/plot_clustering.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
k=int(n_samples / 20),
7777
random_state=rng,
7878
)
79-
bandwidth = cluster.estimate_bandwidth(X, 0.2)
79+
bandwidth = cluster.estimate_bandwidth(X, quantile=0.2)
8080

8181
ms = cluster.MeanShift(bandwidth=bandwidth, bin_seeding=True)
8282

sklearn_extra/kernel_approximation/_fastfood.py

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def fit(self, X, y=None):
201201
)
202202

203203
self._U = self._uniform_vector(rng)
204+
self.is_fitted_ = True
204205

205206
return self
206207

sklearn_extra/robust/robust_weighted_estimator.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,21 @@
4949
LOSS_FUNCTIONS = {
5050
"hinge": (Hinge,),
5151
"log": (Log,),
52+
"squared_error": (SquaredLoss,),
5253
"squared_loss": (SquaredLoss,),
5354
"squared_hinge": (SquaredHinge,),
5455
"modified_huber": (ModifiedHuber,),
5556
"huber": (Huber, 1.35), # 1.35 is default value. TODO : set as parameter
5657
}
5758

59+
# Test version of sklearn, in version older than v1.0 squared_loss must be used
60+
import sklearn
61+
62+
if sklearn.__version__[0] == "0":
63+
SQ_LOSS = "squared_loss"
64+
else:
65+
SQ_LOSS = "squared_error"
66+
5867

5968
def _huber_psisx(x, c):
6069
"""Huber-loss weight for RobustWeightedEstimator algorithm"""
@@ -107,7 +116,7 @@ class _RobustWeightedEstimator(BaseEstimator):
107116
base_estimator.
108117
Classification losses supported : 'log', 'hinge', 'squared_hinge',
109118
'modified_huber'. If 'log', then the base_estimator must support
110-
predict_proba. Regression losses supported : 'squared_loss', 'huber'.
119+
predict_proba. Regression losses supported : 'squared_error', 'huber'.
111120
If callable, the function is used as loss function ro construct
112121
the weights.
113122
@@ -270,7 +279,7 @@ def fit(self, X, y=None):
270279
if "warm_start" in parameters:
271280
base_estimator.set_params(warm_start=True)
272281

273-
if "loss" in parameters:
282+
if ("loss" in parameters) and (loss_param != "squared_error"):
274283
base_estimator.set_params(loss=loss_param)
275284

276285
if "eta0" in parameters:
@@ -971,8 +980,8 @@ class RobustWeightedRegressor(BaseEstimator, RegressorMixin):
971980
(using the inter-quartile range), this tends to be conservative
972981
(robust).
973982
974-
loss : string, None or callable, default="squared_loss"
975-
For now, only "squared_loss" and "huber" are implemented.
983+
loss : string, None or callable, default="squared_error"
984+
For now, only "squared_error" and "huber" are implemented.
976985
977986
sgd_args : dict, default={}
978987
arguments of the SGDClassifier base estimator.
@@ -1057,7 +1066,7 @@ def __init__(
10571066
eta0=0.01,
10581067
c=None,
10591068
k=0,
1060-
loss="squared_loss",
1069+
loss=SQ_LOSS,
10611070
sgd_args=None,
10621071
tol=1e-3,
10631072
n_iter_no_change=10,

sklearn_extra/robust/tests/test_robust_weighted_estimator.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
assert_almost_equal,
1717
)
1818

19+
# Test version of sklearn, in version older than v1.0 squared_loss must be used
20+
import sklearn
21+
22+
if sklearn.__version__[0] == "0":
23+
SQ_LOSS = "squared_loss"
24+
else:
25+
SQ_LOSS = "squared_error"
1926

2027
k_values = [None, 10] # values of k for test robust
2128
c_values = [None, 1e-3] # values of c for test robust
@@ -239,7 +246,7 @@ def test_robust_no_proba():
239246
X_rc = X_rc.reshape(-1, 1)
240247
y_rc[0] = -1
241248

242-
regression_losses = ["squared_loss", "huber"]
249+
regression_losses = [SQ_LOSS, "huber"]
243250

244251

245252
@pytest.mark.parametrize("loss", regression_losses)

0 commit comments

Comments
 (0)