"include/git@developer.sourcefind.cn:tianlh/lightgbm-dcu.git" did not exist on "16004228e75dce0a577979875506eec772dc7ba5"
Unverified Commit ca066d49 authored by Nikita Titov's avatar Nikita Titov Committed by GitHub
Browse files

be compatible with check_is_fitted sklearn function (#3329)

parent 8fc80bb4
...@@ -607,6 +607,8 @@ class LGBMModel(_LGBMModelBase): ...@@ -607,6 +607,8 @@ class LGBMModel(_LGBMModelBase):
self._best_score = self._Booster.best_score self._best_score = self._Booster.best_score
self.fitted_ = True
# free dataset # free dataset
self._Booster.free_dataset() self._Booster.free_dataset()
del train_set, valid_sets del train_set, valid_sets
......
...@@ -20,6 +20,7 @@ from sklearn.multioutput import (MultiOutputClassifier, ClassifierChain, MultiOu ...@@ -20,6 +20,7 @@ from sklearn.multioutput import (MultiOutputClassifier, ClassifierChain, MultiOu
RegressorChain) RegressorChain)
from sklearn.utils.estimator_checks import (_yield_all_checks, SkipTest, from sklearn.utils.estimator_checks import (_yield_all_checks, SkipTest,
check_parameters_default_constructible) check_parameters_default_constructible)
from sklearn.utils.validation import check_is_fitted
decreasing_generator = itertools.count(0, -1) decreasing_generator = itertools.count(0, -1)
...@@ -1091,3 +1092,23 @@ class TestSklearn(unittest.TestCase): ...@@ -1091,3 +1092,23 @@ class TestSklearn(unittest.TestCase):
self.assertEqual(len(init_gbm.evals_result_['valid_0']['multi_logloss']), 5) self.assertEqual(len(init_gbm.evals_result_['valid_0']['multi_logloss']), 5)
self.assertLess(gbm.evals_result_['valid_0']['multi_logloss'][-1], self.assertLess(gbm.evals_result_['valid_0']['multi_logloss'][-1],
init_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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment