-
Notifications
You must be signed in to change notification settings - Fork 655
Support unlimited_depth for the random forest estimators #7895
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
Changes from all commits
d5bdcef
625526a
c5fc11a
977107a
d29771d
19d4268
d1d6b7d
42b1174
95045ec
3237c3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION. | ||
| # SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import json | ||
|
|
@@ -357,6 +357,36 @@ def check_count(node, nodes): | |
| check_count(node, nodes) | ||
|
|
||
|
|
||
| def test_unlimited_max_depth_classifier(client): | ||
| n_workers = len(client.scheduler_info(n_workers=-1)["workers"]) | ||
| X, y = make_classification( | ||
| n_samples=n_workers * 200, n_features=10, random_state=42 | ||
| ) | ||
| X = X.astype(np.float32) | ||
| y = y.astype(np.int32) | ||
|
|
||
| X_dask, y_dask = _prep_training_data(client, X, y, partitions_per_worker=1) | ||
| clf = cuRFC_mg(n_estimators=n_workers * 5, max_depth=None) | ||
| clf.fit(X_dask, y_dask) | ||
| preds = cp.asnumpy(cp.array(clf.predict(X_dask).compute())) | ||
| assert len(preds) == len(y) | ||
|
|
||
|
|
||
| def test_unlimited_max_depth_regressor(client): | ||
| n_workers = len(client.scheduler_info(n_workers=-1)["workers"]) | ||
| X, y = make_regression( | ||
| n_samples=n_workers * 200, n_features=10, random_state=42 | ||
| ) | ||
| X = X.astype(np.float32) | ||
| y = y.astype(np.float32) | ||
|
|
||
| X_dask, y_dask = _prep_training_data(client, X, y, partitions_per_worker=1) | ||
| reg = cuRFR_mg(n_estimators=n_workers * 5, max_depth=None) | ||
| reg.fit(X_dask, y_dask) | ||
| preds = cp.asnumpy(cp.array(reg.predict(X_dask).compute())) | ||
| assert len(preds) == len(y) | ||
|
|
||
|
Comment on lines
+360
to
+388
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strengthen unlimited-depth tests to verify correctness, not just output length. Both new tests currently pass even if predictions are wrong, because they only assert Suggested test hardening+from sklearn.ensemble import RandomForestRegressor as skrfr
@@
def test_unlimited_max_depth_classifier(client):
@@
- X_dask, y_dask = _prep_training_data(client, X, y, partitions_per_worker=1)
- clf = cuRFC_mg(n_estimators=n_workers * 5, max_depth=None)
- clf.fit(X_dask, y_dask)
- preds = cp.asnumpy(cp.array(clf.predict(X_dask).compute()))
- assert len(preds) == len(y)
+ X_train, X_test, y_train, y_test = train_test_split(
+ X, y, test_size=n_workers * 40, random_state=42
+ )
+ X_train_dask, y_train_dask = _prep_training_data(
+ client, X_train, y_train, partitions_per_worker=1
+ )
+ X_test_dask = from_array(X_test)
+
+ clf = cuRFC_mg(n_estimators=n_workers * 5, max_depth=None, random_state=42)
+ clf.fit(X_train_dask, y_train_dask)
+ preds = cp.asnumpy(cp.array(clf.predict(X_test_dask).compute()))
+ acc = accuracy_score(y_test, preds)
+
+ sk_clf = skrfc(
+ n_estimators=n_workers * 5, max_depth=None, random_state=42, n_jobs=-1
+ )
+ sk_clf.fit(X_train, y_train)
+ sk_acc = accuracy_score(y_test, sk_clf.predict(X_test))
+ assert acc >= (sk_acc - 0.07)
@@
def test_unlimited_max_depth_regressor(client):
@@
- X_dask, y_dask = _prep_training_data(client, X, y, partitions_per_worker=1)
- reg = cuRFR_mg(n_estimators=n_workers * 5, max_depth=None)
- reg.fit(X_dask, y_dask)
- preds = cp.asnumpy(cp.array(reg.predict(X_dask).compute()))
- assert len(preds) == len(y)
+ X_train, X_test, y_train, y_test = train_test_split(
+ X, y, test_size=n_workers * 40, random_state=42
+ )
+ X_train_dask, y_train_dask = _prep_training_data(
+ client, X_train, y_train, partitions_per_worker=1
+ )
+ X_test_dask = from_array(X_test)
+
+ reg = cuRFR_mg(n_estimators=n_workers * 5, max_depth=None, random_state=42)
+ reg.fit(X_train_dask, y_train_dask)
+ preds = cp.asnumpy(cp.array(reg.predict(X_test_dask).compute()))
+ r2 = r2_score(y_test, preds)
+
+ sk_reg = skrfr(
+ n_estimators=n_workers * 5, max_depth=None, random_state=42, n_jobs=-1
+ )
+ sk_reg.fit(X_train, y_train)
+ sk_r2 = r2_score(y_test, sk_reg.predict(X_test))
+ assert r2 >= (sk_r2 - 0.10)As per coding guidelines for 🤖 Prompt for AI Agents |
||
|
|
||
| @pytest.mark.parametrize("estimator_type", ["regression", "classification"]) | ||
| def test_rf_get_combined_model_right_aftter_fit(client, estimator_type): | ||
| max_depth = 3 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.