Skip to content

Commit

Permalink
be compatible with check_is_fitted sklearn function (#3329)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrikerRUS authored Sep 2, 2020
1 parent 8fc80bb commit ca066d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python-package/lightgbm/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ def _get_meta_data(collection, name, i):

self._best_score = self._Booster.best_score

self.fitted_ = True

# free dataset
self._Booster.free_dataset()
del train_set, valid_sets
Expand Down
21 changes: 21 additions & 0 deletions tests/python_package_test/test_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
RegressorChain)
from sklearn.utils.estimator_checks import (_yield_all_checks, SkipTest,
check_parameters_default_constructible)
from sklearn.utils.validation import check_is_fitted


decreasing_generator = itertools.count(0, -1)
Expand Down Expand Up @@ -1091,3 +1092,23 @@ def test_continue_training_with_model(self):
self.assertEqual(len(init_gbm.evals_result_['valid_0']['multi_logloss']), 5)
self.assertLess(gbm.evals_result_['valid_0']['multi_logloss'][-1],
init_gbm.evals_result_['valid_0']['multi_logloss'][-1])

# sklearn < 0.22 requires passing "attributes" argument
@unittest.skipIf(sk_version < '0.22.0', 'scikit-learn version is less than 0.22')
def test_check_is_fitted(self):
X, y = load_digits(n_class=2, return_X_y=True)
est = lgb.LGBMModel(n_estimators=5, objective="binary")
clf = lgb.LGBMClassifier(n_estimators=5)
reg = lgb.LGBMRegressor(n_estimators=5)
rnk = lgb.LGBMRanker(n_estimators=5)
models = (est, clf, reg, rnk)
for model in models:
self.assertRaises(lgb.compat.LGBMNotFittedError,
check_is_fitted,
model)
est.fit(X, y)
clf.fit(X, y)
reg.fit(X, y)
rnk.fit(X, y, group=np.ones(X.shape[0]))
for model in models:
check_is_fitted(model)

0 comments on commit ca066d4

Please sign in to comment.