Skip to content
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

Fix 'RF' error for LightGBM Classifier #1302

Merged
merged 12 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions docs/source/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Release Notes
* Added `PCA Transformer` component for dimensionality reduction :pr:`1270`
* Fixes
* Fixed ML performance issue with ordered datasets: always shuffle data in automl's default CV splits :pr:`1265`
* Fixed ``boosting type='rf'`` for LightGBM Classifier, as well as ``num_leaves`` error :pr:`1302`
* Changes
* Allow ``add_to_rankings`` to be called before AutoMLSearch is called :pr:`1250`
* Documentation Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LightGBMClassifier(Estimator):
"boosting_type": ["gbdt", "dart", "goss", "rf"],
"n_estimators": Integer(10, 100),
"max_depth": Integer(0, 10),
"num_leaves": Integer(1, 100),
"num_leaves": Integer(2, 100),
bchen1116 marked this conversation as resolved.
Show resolved Hide resolved
"min_child_samples": Integer(1, 100)
}
model_family = ModelFamily.LIGHTGBM
Expand All @@ -30,7 +30,7 @@ class LightGBMClassifier(Estimator):
SEED_MIN = 0
SEED_MAX = SEED_BOUNDS.max_bound

def __init__(self, boosting_type="gbdt", learning_rate=0.1, n_estimators=100, max_depth=0, num_leaves=31, min_child_samples=20, n_jobs=-1, random_state=0, **kwargs):
def __init__(self, boosting_type="gbdt", learning_rate=0.1, n_estimators=100, max_depth=0, num_leaves=31, min_child_samples=20, n_jobs=-1, random_state=0, bagging_fraction=0.9, bagging_freq=0, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why default bagging_freq to 0? Won't that cause the bug when boosting_type="rf"? What default does lightgbm choose for this parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LightGBM defaults to 0 for bagging_freq. Users can set it to 1 and change bagging_fraction if they want to speed up computation and randomly select data for other boosting types, but it's required to be 1 for boosting_type=rf (along with 0 < bagging_fraction < 1.0).

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. This looks good. Is 0.9 the default bagging_fraction in lightgbm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dsherry it defaults to 1.0

# lightGBM's current release doesn't currently support numpy.random.RandomState as the random_state value so we convert to int instead
random_seed = get_random_seed(random_state, self.SEED_MIN, self.SEED_MAX)

Expand All @@ -40,9 +40,15 @@ def __init__(self, boosting_type="gbdt", learning_rate=0.1, n_estimators=100, ma
"max_depth": max_depth,
"num_leaves": num_leaves,
"min_child_samples": min_child_samples,
"n_jobs": n_jobs}
"n_jobs": n_jobs,
"bagging_freq": bagging_freq,
"bagging_fraction": bagging_fraction}
Copy link
Contributor

Choose a reason for hiding this comment

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

@bchen1116 could you please explain why adding these two parameters fixed the bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As some background, LightGBM has 4 boosting types: "gbdt", "dart", "goss", "rf". Bagging_freq refers to the frequency of bagging, where it bags every bagging_freq = k iterations (0 means it doesn't bag). bagging_fraction refers to the amount of data randomly selected without resampling (1 means select all, 0 means none). This can help speed up the training process.

The default bagging_freq that LightGBM sets is 0, which works with gbdt, dart, and goss. However, for rf, since its random forest, LightGBM requires that it uses bagging, which means bagging_freq must be 1 and bagging_fraction must be set to be below 1.0. By adding those two parameters and changing bagging_freq when the boosting_type=rf, we do a simple fix to avoid this bug.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the clear explanation! That makes sense.

Can we tweak the comment you left on line 48:

if the boosting type is random forest, bagging is required by lightgbm, so we set bagging_freq to 1 in order to avoid errors

parameters.update(kwargs)

# if the boosting type is random forest, we want to change the bagging_freq to 1 so that we avoid errors
if boosting_type == "rf" and not bagging_freq:
parameters.update({'bagging_freq': 1})
dsherry marked this conversation as resolved.
Show resolved Hide resolved

lgbm_error_msg = "LightGBM is not installed. Please install using `pip install lightgbm`."
lgbm = import_or_raise("lightgbm", error_msg=lgbm_error_msg)
self._ordinal_encoder = None
Expand Down
3 changes: 2 additions & 1 deletion evalml/tests/component_tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def test_describe_component():
pass
try:
lg_classifier = LightGBMClassifier()
assert lg_classifier.describe(return_dict=True) == {'name': 'LightGBM Classifier', 'parameters': {'boosting_type': 'gbdt', 'learning_rate': 0.1, 'n_estimators': 100, 'max_depth': 0, 'num_leaves': 31, 'min_child_samples': 20, 'n_jobs': -1}}
assert lg_classifier.describe(return_dict=True) == {'name': 'LightGBM Classifier', 'parameters': {'boosting_type': 'gbdt', 'learning_rate': 0.1, 'n_estimators': 100, 'max_depth': 0, 'num_leaves': 31,
'min_child_samples': 20, 'n_jobs': -1, 'bagging_fraction': 0.9, 'bagging_freq': 0}}
except ImportError:
pass

Expand Down
38 changes: 38 additions & 0 deletions evalml/tests/component_tests/test_lgbm_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal, assert_series_equal
from pytest import importorskip

Expand Down Expand Up @@ -281,3 +282,40 @@ def test_binary_label_encoding(mock_fit, mock_predict, X_y_binary):
assert_series_equal(y_arg, y_numeric)

clf.predict(X)


def test_binary_rf_not_defaults(X_y_binary):
bchen1116 marked this conversation as resolved.
Show resolved Hide resolved
X, y = X_y_binary

with pytest.raises(lgbm.basic.LightGBMError, match="bagging_fraction"):
clf = LightGBMClassifier(boosting_type="rf", bagging_freq=1, bagging_fraction=1.01)
clf.fit(X, y)

clf = LightGBMClassifier(boosting_type="rf", bagging_freq=0)
clf.fit(X, y)
assert clf.parameters['bagging_freq'] == 1
assert clf.parameters['bagging_fraction'] == 0.9


def test_binary_rf(X_y_binary):
X, y = X_y_binary

clf = LightGBMClassifier()
clf.fit(X, y)
assert clf.parameters['bagging_freq'] == 0
assert clf.parameters['bagging_fraction'] == 0.9

clf = LightGBMClassifier(boosting_type="rf")
clf.fit(X, y)
assert clf.parameters['bagging_freq'] == 1
assert clf.parameters['bagging_fraction'] == 0.9

clf = LightGBMClassifier(boosting_type="rf", bagging_freq=1, bagging_fraction=0.5)
clf.fit(X, y)
assert clf.parameters['bagging_freq'] == 1
assert clf.parameters['bagging_fraction'] == 0.5

clf = LightGBMClassifier(bagging_freq=1, bagging_fraction=0.5)
clf.fit(X, y)
assert clf.parameters['bagging_freq'] == 1
assert clf.parameters['bagging_fraction'] == 0.5