Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG] Standardize sample weights validation in BaseDecisionTree #15495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2019
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
14 changes: 2 additions & 12 deletions sklearn/tree/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from ..utils import Bunch
from ..utils import check_array
from ..utils import check_random_state
from ..utils.validation import _check_sample_weight
from ..utils import compute_sample_weight
from ..utils.multiclass import check_classification_targets
from ..utils.validation import check_is_fitted
Expand Down Expand Up @@ -266,18 +267,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
"or larger than 1").format(max_leaf_nodes))

if sample_weight is not None:
if (getattr(sample_weight, "dtype", None) != DOUBLE or
not sample_weight.flags.contiguous):
sample_weight = np.ascontiguousarray(
sample_weight, dtype=DOUBLE)
if len(sample_weight.shape) > 1:
raise ValueError("Sample weights array has more "
"than one dimension: %d" %
len(sample_weight.shape))
if len(sample_weight) != n_samples:
raise ValueError("Number of weights=%d does not match "
"number of samples=%d" %
(len(sample_weight), n_samples))
sample_weight = _check_sample_weight(sample_weight, X, DOUBLE)

if expanded_class_weight is not None:
if sample_weight is not None:
Expand Down
4 changes: 2 additions & 2 deletions sklearn/tree/tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import copy
import pickle
from functools import partial
from itertools import product
import struct

Expand Down Expand Up @@ -1121,7 +1120,8 @@ def test_sample_weight_invalid():
clf.fit(X, y, sample_weight=sample_weight)

sample_weight = np.array(0)
with pytest.raises(ValueError):
expected_err = r"Singleton.* cannot be considered a valid collection"
with pytest.raises(TypeError, match=expected_err):
clf.fit(X, y, sample_weight=sample_weight)

sample_weight = np.ones(101)
Expand Down