Skip to content

BUG Fixes sample weights when there are missing values in DecisionTrees #26376

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

Conversation

thomasjpfan
Copy link
Member

Reference Issues/PRs

Follow up to #23595

What does this implement/fix? Explain your changes.

On main, the weighted_n_missing was incorrectly computed. This PR fixes it. For reference the computation is exactly the same as sum_total:

for k in range(self.n_outputs):
y_ik = self.y[i, k]
w_y_ik = w * y_ik
self.sum_total[k] += w_y_ik
self.sq_sum_total += w_y_ik * y_ik
self.weighted_n_node_samples += w

@thomasjpfan thomasjpfan added this to the 1.3 milestone May 15, 2023
@thomasjpfan thomasjpfan changed the title BUG Fixes sample weights when there are missing values in Regression Trees BUG Fixes sample weights when there are missing values in DecisionTrees May 15, 2023
@@ -2549,7 +2549,8 @@ def test_missing_values_poisson():
(datasets.make_classification, DecisionTreeClassifier),
],
)
def test_missing_values_is_resilience(make_data, Tree):
@pytest.mark.parametrize("sample_weight_train", [None, "ones"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test the behavior when using non-uniform weights?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, an easier test than non-uniform weight is to assign 0-weight to some specific samples.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth having a separated test for checking an equivalence.

Copy link
Member

@jjerphan jjerphan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you, @thomasjpfan.

Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

X, y = make_data(n_samples=n_samples, n_features=n_features, random_state=rng)

# Create dataset with missing values
X[rng.choice([False, True], size=X.shape, p=[0.9, 0.1])] = np.nan
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat idiom :)

@ogrisel ogrisel merged commit 43cf7d4 into scikit-learn:main May 16, 2023
REDVM pushed a commit to REDVM/scikit-learn that referenced this pull request Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants