From 6f4e1c5fce3c03ec2cbdb56b1baf892d211af075 Mon Sep 17 00:00:00 2001 From: Yu Sun Date: Sat, 2 Nov 2019 14:38:55 -0700 Subject: [PATCH 1/3] added validation for sample weight in DummyRegressor --- sklearn/dummy.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sklearn/dummy.py b/sklearn/dummy.py index b12b314c4a91f..9cc14a9d1e172 100644 --- a/sklearn/dummy.py +++ b/sklearn/dummy.py @@ -13,7 +13,7 @@ from .utils.validation import _num_samples from .utils.validation import check_array from .utils.validation import check_consistent_length -from .utils.validation import check_is_fitted +from .utils.validation import check_is_fitted, _check_sample_weight from .utils.random import _random_choice_csc from .utils.stats import _weighted_percentile from .utils.multiclass import class_distribution @@ -140,8 +140,9 @@ def fit(self, X, y, sample_weight=None): y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] + if sample_weight is not None: + sample_weight = _check_sample_weight(sample_weight, X) - check_consistent_length(X, y, sample_weight) if self.strategy == "constant": if self.constant is None: @@ -470,9 +471,8 @@ def fit(self, X, y, sample_weight=None): y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] - check_consistent_length(X, y, sample_weight) if sample_weight is not None: - sample_weight = np.asarray(sample_weight) + sample_weight = _check_sample_weight(sample_weight, X) if self.strategy == "mean": self.constant_ = np.average(y, axis=0, weights=sample_weight) From 82dbfc6fd80ed03b22c6bd0db95433b03f134b59 Mon Sep 17 00:00:00 2001 From: theoptips Date: Sat, 2 Nov 2019 15:30:51 -0700 Subject: [PATCH 2/3] Fix issue with earlier commit dummy.py add check_consistent_length(X, y, sample_weight) to check for X y input --- sklearn/dummy.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sklearn/dummy.py b/sklearn/dummy.py index 9cc14a9d1e172..1a6f9b8bcdc63 100644 --- a/sklearn/dummy.py +++ b/sklearn/dummy.py @@ -140,6 +140,9 @@ def fit(self, X, y, sample_weight=None): y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] + + check_consistent_length(X, y, sample_weight) + if sample_weight is not None: sample_weight = _check_sample_weight(sample_weight, X) @@ -470,7 +473,9 @@ def fit(self, X, y, sample_weight=None): if y.ndim == 1: y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] - + + check_consistent_length(X, y, sample_weight) + if sample_weight is not None: sample_weight = _check_sample_weight(sample_weight, X) From 25cdf917936474d4e2d83bec4fc1bc11a71cf812 Mon Sep 17 00:00:00 2001 From: Yu Sun Date: Sat, 2 Nov 2019 15:50:56 -0700 Subject: [PATCH 3/3] fix flake8 flag for white space indent --- sklearn/dummy.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sklearn/dummy.py b/sklearn/dummy.py index 1a6f9b8bcdc63..e20c5d2c678ef 100644 --- a/sklearn/dummy.py +++ b/sklearn/dummy.py @@ -140,13 +140,12 @@ def fit(self, X, y, sample_weight=None): y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] - + check_consistent_length(X, y, sample_weight) - + if sample_weight is not None: sample_weight = _check_sample_weight(sample_weight, X) - if self.strategy == "constant": if self.constant is None: raise ValueError("Constant target value has to be specified " @@ -473,9 +472,9 @@ def fit(self, X, y, sample_weight=None): if y.ndim == 1: y = np.reshape(y, (-1, 1)) self.n_outputs_ = y.shape[1] - + check_consistent_length(X, y, sample_weight) - + if sample_weight is not None: sample_weight = _check_sample_weight(sample_weight, X)