|
| 1 | +# Copyright 1999-2021 Alibaba Group Holding Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | +from sklearn.datasets import make_classification |
| 18 | +from sklearn.decomposition import PCA |
| 19 | +from sklearn.ensemble import GradientBoostingClassifier |
| 20 | +from sklearn.linear_model import LinearRegression, LogisticRegression |
| 21 | + |
| 22 | +from ... import tensor as mt |
| 23 | +from ..wrappers import ParallelPostFit |
| 24 | + |
| 25 | + |
| 26 | +raw_x, raw_y = make_classification(n_samples=1000) |
| 27 | +X, y = mt.tensor(raw_x, chunk_size=100), mt.tensor(raw_y, chunk_size=100) |
| 28 | + |
| 29 | + |
| 30 | +def test_parallel_post_fit_basic(setup): |
| 31 | + clf = ParallelPostFit(GradientBoostingClassifier()) |
| 32 | + clf.fit(X, y) |
| 33 | + |
| 34 | + assert isinstance(clf.predict(X), mt.Tensor) |
| 35 | + assert isinstance(clf.predict_proba(X), mt.Tensor) |
| 36 | + |
| 37 | + result = clf.score(X, y) |
| 38 | + expected = clf.estimator.score(X, y) |
| 39 | + assert result.fetch() == expected |
| 40 | + |
| 41 | + clf = ParallelPostFit(LinearRegression()) |
| 42 | + clf.fit(X, y) |
| 43 | + with pytest.raises(AttributeError, |
| 44 | + match="The wrapped estimator (.|\n)* 'predict_proba' method."): |
| 45 | + clf.predict_proba(X) |
| 46 | + |
| 47 | + |
| 48 | +def test_parallel_post_fit_predict(setup): |
| 49 | + base = LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs") |
| 50 | + wrap = ParallelPostFit(LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs")) |
| 51 | + |
| 52 | + base.fit(X, y) |
| 53 | + wrap.fit(X, y) |
| 54 | + |
| 55 | + result = wrap.predict(X) |
| 56 | + expected = base.predict(X) |
| 57 | + np.testing.assert_allclose(result, expected) |
| 58 | + |
| 59 | + result = wrap.predict_proba(X) |
| 60 | + expected = base.predict_proba(X) |
| 61 | + np.testing.assert_allclose(result, expected) |
| 62 | + |
| 63 | + result = wrap.predict_log_proba(X) |
| 64 | + expected = base.predict_log_proba(X) |
| 65 | + np.testing.assert_allclose(result, expected) |
| 66 | + |
| 67 | + |
| 68 | +def test_parallel_post_fit_transform(setup): |
| 69 | + base = PCA(random_state=0) |
| 70 | + wrap = ParallelPostFit(PCA(random_state=0)) |
| 71 | + |
| 72 | + base.fit(raw_x, raw_y) |
| 73 | + wrap.fit(X, y) |
| 74 | + |
| 75 | + result = base.transform(X) |
| 76 | + expected = wrap.transform(X) |
| 77 | + np.testing.assert_allclose(result, expected, atol=.1) |
| 78 | + |
| 79 | + |
| 80 | +def test_parallel_post_fit_multiclass(setup): |
| 81 | + raw_x, raw_y = make_classification(n_classes=3, n_informative=4) |
| 82 | + X, y = mt.tensor(raw_x, chunk_size=50), mt.tensor(raw_y, chunk_size=50) |
| 83 | + |
| 84 | + clf = ParallelPostFit( |
| 85 | + LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs", multi_class="auto") |
| 86 | + ) |
| 87 | + |
| 88 | + clf.fit(X, y) |
| 89 | + result = clf.predict(X) |
| 90 | + expected = clf.estimator.predict(X) |
| 91 | + |
| 92 | + np.testing.assert_allclose(result, expected) |
| 93 | + |
| 94 | + result = clf.predict_proba(X) |
| 95 | + expected = clf.estimator.predict_proba(X) |
| 96 | + |
| 97 | + np.testing.assert_allclose(result, expected) |
| 98 | + |
| 99 | + result = clf.predict_log_proba(X) |
| 100 | + expected = clf.estimator.predict_log_proba(X) |
| 101 | + |
| 102 | + np.testing.assert_allclose(result, expected) |
0 commit comments