Skip to content

Commit d7b4a05

Browse files
committed
Fix regression algorithms to give correct output dimensions (#1335)
* Added ignored_warnings file * Use ignored_warnings file * Test regressors with 1d, 1d as 2d and 2d targets * Flake'd * Fix broken relative imports to ignore_warnings * Removed print and updated parameter type for tests * Type import fix
1 parent c86eb95 commit d7b4a05

File tree

5 files changed

+24
-111
lines changed

5 files changed

+24
-111
lines changed

autosklearn/pipeline/components/regression/gaussian_process.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def fit(self, X, y):
3737
normalize_y=True
3838
)
3939

40+
if y.ndim == 2 and y.shape[1] == 1:
41+
y = y.flatten()
42+
4043
self.estimator.fit(X, y)
4144

4245
return self

test/test_pipeline/components/regression/test_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, Container
1+
from typing import Type
22

33
import unittest
44

@@ -13,7 +13,7 @@
1313

1414
from autosklearn.pipeline.components.regression import _regressors, RegressorChoice
1515

16-
from test.test_pipeline.ignored_warnings import regressor_warnings, ignore_warnings
16+
from ...ignored_warnings import regressor_warnings, ignore_warnings
1717

1818

1919
class BaseRegressionComponentTest(unittest.TestCase):
@@ -331,7 +331,7 @@ def test_fit_and_predict_with_1d_targets_as_1d(
331331
regressor: Type[RegressorChoice],
332332
X: np.ndarray,
333333
y: np.ndarray
334-
) -> None:
334+
):
335335
"""Test that all pipelines work with 1d target types
336336
337337
Parameters
@@ -374,7 +374,7 @@ def test_fit_and_predict_with_1d_targets_as_2d(
374374
regressor: Type[RegressorChoice],
375375
X: np.ndarray,
376376
y: np.ndarray
377-
) -> None:
377+
):
378378
"""Test that all pipelines work with 1d target types when they are wrapped as 2d
379379
380380
Parameters
@@ -423,7 +423,7 @@ def test_fit_and_predict_with_2d_targets(
423423
regressor: Type[RegressorChoice],
424424
X: np.ndarray,
425425
y: np.ndarray
426-
) -> None:
426+
):
427427
"""Test that all pipelines work with 2d target types
428428
429429
Parameters

test/test_pipeline/ignored_warnings.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import List, Iterator, Tuple
33

44
import warnings
5-
65
from sklearn.exceptions import ConvergenceWarning
76

87

@@ -69,34 +68,14 @@
6968
r" optimization hasn't converged yet\."
7069
)
7170
),
72-
(
73-
ConvergenceWarning, ( # From FastICA
74-
r"FastICA did not converge\."
75-
r" Consider increasing tolerance or the maximum number of iterations\."
76-
)
77-
),
7871
(
7972
UserWarning, ( # From LDA (Linear Discriminant Analysis)
8073
r"Variables are collinear"
8174
)
8275
),
83-
(
84-
UserWarning, (
85-
r"Clustering metrics expects discrete values but received continuous values"
86-
r" for label, and multiclass values for target"
87-
)
88-
)
89-
]
90-
91-
feature_preprocessing_warnings = [
92-
(
93-
ConvergenceWarning, ( # From liblinear
94-
r"Liblinear failed to converge, increase the number of iterations."
95-
)
96-
)
9776
]
9877

99-
ignored_warnings = regressor_warnings + classifier_warnings + feature_preprocessing_warnings
78+
ignored_warnings = regressor_warnings + classifier_warnings
10079

10180

10281
@contextmanager

test/test_pipeline/test_classification.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import tempfile
88
import unittest
99
import unittest.mock
10-
import warnings
1110

1211
from joblib import Memory
1312
import numpy as np
@@ -19,7 +18,6 @@
1918
import sklearn.ensemble
2019
import sklearn.svm
2120
from sklearn.utils.validation import check_is_fitted
22-
from sklearn.exceptions import ConvergenceWarning
2321

2422
from ConfigSpace.configuration_space import ConfigurationSpace
2523
from ConfigSpace.hyperparameters import CategoricalHyperparameter
@@ -34,42 +32,7 @@
3432
from autosklearn.pipeline.constants import \
3533
DENSE, SPARSE, UNSIGNED_DATA, PREDICTIONS, SIGNED_DATA, INPUT
3634

37-
ignored_warnings = [
38-
(
39-
UserWarning, ( # From QuantileTransformer
40-
r"n_quantiles \(\d+\) is greater than the total number of samples \(\d+\)\."
41-
r" n_quantiles is set to n_samples\."
42-
)
43-
),
44-
(
45-
UserWarning, ( # From FastICA
46-
r"n_components is too large: it will be set to \d+"
47-
)
48-
49-
),
50-
(
51-
ConvergenceWarning, ( # From Liblinear
52-
r"Liblinear failed to converge, increase the number of iterations\."
53-
)
54-
),
55-
(
56-
ConvergenceWarning, ( # From SGD
57-
r"Maximum number of iteration reached before convergence\. Consider increasing"
58-
r" max_iter to improve the fit\."
59-
)
60-
),
61-
(
62-
ConvergenceWarning, ( # From MLP
63-
r"Stochastic Optimizer: Maximum iterations \(\d+\) reached and the"
64-
r" optimization hasn't converged yet\."
65-
)
66-
),
67-
(
68-
UserWarning, ( # From LDA (Linear Discriminant Analysis)
69-
r"Variables are collinear"
70-
)
71-
),
72-
]
35+
from .ignored_warnings import classifier_warnings, ignore_warnings
7336

7437

7538
class DummyClassifier(AutoSklearnClassificationAlgorithm):
@@ -535,10 +498,7 @@ def _test_configurations(
535498
check_is_fitted(step)
536499

537500
try:
538-
with warnings.catch_warnings():
539-
for category, message in ignored_warnings:
540-
warnings.filterwarnings('ignore', category=category, message=message)
541-
501+
with ignore_warnings(classifier_warnings):
542502
cls.fit(X_train, Y_train)
543503

544504
# After fit, all components should be tagged as fitted

test/test_pipeline/test_regression.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import tempfile
55
import unittest
66
import unittest.mock
7-
import warnings
87

98
from joblib import Memory
109
import numpy as np
@@ -14,7 +13,6 @@
1413
import sklearn.ensemble
1514
import sklearn.svm
1615
from sklearn.utils.validation import check_is_fitted
17-
from sklearn.exceptions import ConvergenceWarning
1816

1917
from ConfigSpace.configuration_space import ConfigurationSpace
2018
from ConfigSpace.hyperparameters import CategoricalHyperparameter
@@ -28,32 +26,7 @@
2826
from autosklearn.pipeline.util import get_dataset
2927
from autosklearn.pipeline.constants import SPARSE, DENSE, SIGNED_DATA, UNSIGNED_DATA, PREDICTIONS
3028

31-
ignored_warnings = [
32-
(
33-
UserWarning, ( # From QuantileTransformer
34-
r"n_quantiles \(\d+\) is greater than the total number of samples \(\d+\)\."
35-
r" n_quantiles is set to n_samples\."
36-
)
37-
),
38-
(
39-
ConvergenceWarning, ( # From GaussianProcesses
40-
r"The optimal value found for dimension \d+ of parameter \w+ is close"
41-
r" to the specified (upper|lower) bound .*(Increasing|Decreasing) the bound"
42-
r" and calling fit again may find a better value."
43-
)
44-
),
45-
(
46-
UserWarning, ( # From FastICA
47-
r"n_components is too large: it will be set to \d+"
48-
)
49-
),
50-
(
51-
ConvergenceWarning, ( # From SGD
52-
r"Maximum number of iteration reached before convergence\. Consider increasing"
53-
r" max_iter to improve the fit\."
54-
)
55-
),
56-
]
29+
from .ignored_warnings import regressor_warnings, ignore_warnings
5730

5831

5932
class SimpleRegressionPipelineTest(unittest.TestCase):
@@ -207,21 +180,19 @@ def _test_configurations(self, configurations_space, make_sparse=False,
207180
check_is_fitted(step)
208181

209182
try:
210-
with warnings.catch_warnings():
211-
for category, message in ignored_warnings:
212-
warnings.filterwarnings('ignore', category=category, message=message)
213-
183+
with ignore_warnings(regressor_warnings):
214184
cls.fit(X_train, Y_train)
215-
# After fit, all components should be tagged as fitted
216-
# by sklearn. Check is fitted raises an exception if that
217-
# is not the case
218-
try:
219-
for name, step in cls.named_steps.items():
220-
check_is_fitted(step)
221-
except sklearn.exceptions.NotFittedError:
222-
self.fail("config={} raised NotFittedError unexpectedly!".format(
223-
config
224-
))
185+
186+
# After fit, all components should be tagged as fitted
187+
# by sklearn. Check is fitted raises an exception if that
188+
# is not the case
189+
try:
190+
for name, step in cls.named_steps.items():
191+
check_is_fitted(step)
192+
except sklearn.exceptions.NotFittedError:
193+
self.fail("config={} raised NotFittedError unexpectedly!".format(
194+
config
195+
))
225196

226197
cls.predict(X_test)
227198
except MemoryError:

0 commit comments

Comments
 (0)