-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
556 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 1999-2021 Alibaba Group Holding Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Callable, Union | ||
|
||
from sklearn.metrics import make_scorer | ||
|
||
from . import accuracy_score, log_loss, r2_score | ||
|
||
|
||
accuracy_score = make_scorer(accuracy_score) | ||
r2_score = make_scorer(r2_score) | ||
neg_log_loss_scorer = make_scorer(log_loss, greater_is_better=False, | ||
needs_proba=True) | ||
|
||
|
||
SCORERS = dict( | ||
r2=r2_score, | ||
accuracy=accuracy_score, | ||
neg_log_loss=neg_log_loss_scorer, | ||
) | ||
|
||
|
||
def get_scorer(score_func: Union[str, Callable], **kwargs) -> Callable: | ||
""" | ||
Get a scorer from string | ||
Parameters | ||
---------- | ||
score_func : str | callable | ||
scoring method as string. If callable it is returned as is. | ||
Returns | ||
------- | ||
scorer : callable | ||
The scorer. | ||
""" | ||
if isinstance(score_func, str): | ||
try: | ||
scorer = SCORERS[score_func] | ||
except KeyError: | ||
raise ValueError( | ||
"{} is not a valid scoring value. " | ||
"Valid options are {}".format(score_func, sorted(SCORERS)) | ||
) | ||
return scorer | ||
else: | ||
return make_scorer(score_func, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 1999-2020 Alibaba Group Holding Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
from sklearn.metrics import r2_score | ||
|
||
from .. import get_scorer | ||
|
||
|
||
def test_get_scorer(): | ||
with pytest.raises(ValueError): | ||
get_scorer('unknown') | ||
|
||
assert get_scorer('r2') is not None | ||
assert get_scorer(r2_score) is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 1999-2021 Alibaba Group Holding Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 1999-2021 Alibaba Group Holding Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import numpy as np | ||
import pytest | ||
from sklearn.datasets import make_classification | ||
from sklearn.decomposition import PCA | ||
from sklearn.ensemble import GradientBoostingClassifier | ||
from sklearn.linear_model import LinearRegression, LogisticRegression | ||
|
||
from ... import tensor as mt | ||
from ..wrappers import ParallelPostFit | ||
|
||
|
||
raw_x, raw_y = make_classification(n_samples=1000) | ||
X, y = mt.tensor(raw_x, chunk_size=100), mt.tensor(raw_y, chunk_size=100) | ||
|
||
|
||
def test_parallel_post_fit_basic(setup): | ||
clf = ParallelPostFit(GradientBoostingClassifier()) | ||
clf.fit(X, y) | ||
|
||
assert isinstance(clf.predict(X), mt.Tensor) | ||
assert isinstance(clf.predict_proba(X), mt.Tensor) | ||
|
||
result = clf.score(X, y) | ||
expected = clf.estimator.score(X, y) | ||
assert result.fetch() == expected | ||
|
||
clf = ParallelPostFit(LinearRegression()) | ||
clf.fit(X, y) | ||
with pytest.raises(AttributeError, | ||
match="The wrapped estimator (.|\n)* 'predict_proba' method."): | ||
clf.predict_proba(X) | ||
|
||
|
||
def test_parallel_post_fit_predict(setup): | ||
base = LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs") | ||
wrap = ParallelPostFit(LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs")) | ||
|
||
base.fit(X, y) | ||
wrap.fit(X, y) | ||
|
||
result = wrap.predict(X) | ||
expected = base.predict(X) | ||
np.testing.assert_allclose(result, expected) | ||
|
||
result = wrap.predict_proba(X) | ||
expected = base.predict_proba(X) | ||
np.testing.assert_allclose(result, expected) | ||
|
||
result = wrap.predict_log_proba(X) | ||
expected = base.predict_log_proba(X) | ||
np.testing.assert_allclose(result, expected) | ||
|
||
|
||
def test_parallel_post_fit_transform(setup): | ||
base = PCA(random_state=0) | ||
wrap = ParallelPostFit(PCA(random_state=0)) | ||
|
||
base.fit(raw_x, raw_y) | ||
wrap.fit(X, y) | ||
|
||
result = base.transform(X) | ||
expected = wrap.transform(X) | ||
np.testing.assert_allclose(result, expected, atol=.1) | ||
|
||
|
||
def test_parallel_post_fit_multiclass(setup): | ||
raw_x, raw_y = make_classification(n_classes=3, n_informative=4) | ||
X, y = mt.tensor(raw_x, chunk_size=50), mt.tensor(raw_y, chunk_size=50) | ||
|
||
clf = ParallelPostFit( | ||
LogisticRegression(random_state=0, n_jobs=1, solver="lbfgs", multi_class="auto") | ||
) | ||
|
||
clf.fit(X, y) | ||
result = clf.predict(X) | ||
expected = clf.estimator.predict(X) | ||
|
||
np.testing.assert_allclose(result, expected) | ||
|
||
result = clf.predict_proba(X) | ||
expected = clf.estimator.predict_proba(X) | ||
|
||
np.testing.assert_allclose(result, expected) | ||
|
||
result = clf.predict_log_proba(X) | ||
expected = clf.estimator.predict_log_proba(X) | ||
|
||
np.testing.assert_allclose(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.