Skip to content

Commit

Permalink
Updated support scripts for LightGBM 4 - #8 [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Nuno Silva <nunosilva800@gmail.com>
  • Loading branch information
ankane and nunosilva800 committed Dec 16, 2024
1 parent 1596a9e commit dce4dd9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion test/support/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

print()
print('test_early_stopping')
model.fit(X_train, ym_train, eval_set=[(X_test, ym_test)], early_stopping_rounds=5, verbose=True)
model.fit(X_train, ym_train, eval_set=[(X_test, ym_test)], callbacks=[lgb.early_stopping(stopping_rounds=5), lgb.log_evaluation()])

print()
print('test_missing_numeric')
Expand Down
32 changes: 16 additions & 16 deletions test/support/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@
regression_params = {'objective': 'regression', 'verbosity': -1}
regression_train = lgb.Dataset(X_train, label=y_train)
eval_hist = lgb.cv(regression_params, regression_train, shuffle=False, stratified=False)
print(eval_hist['l2-mean'][0])
print(eval_hist['l2-mean'][-1])
print(eval_hist['l2-stdv'][0])
print(eval_hist['l2-stdv'][-1])
print(eval_hist['valid l2-mean'][0])
print(eval_hist['valid l2-mean'][-1])
print(eval_hist['valid l2-stdv'][0])
print(eval_hist['valid l2-stdv'][-1])

print()
print('test_binary')

binary_params = {'objective': 'binary', 'verbosity': -1}
binary_train = lgb.Dataset(X_train, label=y_train.replace(2, 1))
eval_hist = lgb.cv(binary_params, binary_train, shuffle=False, stratified=False)
print(eval_hist['binary_logloss-mean'][0])
print(eval_hist['binary_logloss-mean'][-1])
print(eval_hist['binary_logloss-stdv'][0])
print(eval_hist['binary_logloss-stdv'][-1])
print(eval_hist['valid binary_logloss-mean'][0])
print(eval_hist['valid binary_logloss-mean'][-1])
print(eval_hist['valid binary_logloss-stdv'][0])
print(eval_hist['valid binary_logloss-stdv'][-1])

print()
print('test_multiclass')

multiclass_params = {'objective': 'multiclass', 'num_class': 3, 'verbosity': -1}
multiclass_train = lgb.Dataset(X_train, label=y_train)
eval_hist = lgb.cv(multiclass_params, multiclass_train, shuffle=False, stratified=False)
print(eval_hist['multi_logloss-mean'][0])
print(eval_hist['multi_logloss-mean'][-1])
print(eval_hist['multi_logloss-stdv'][0])
print(eval_hist['multi_logloss-stdv'][-1])
print(eval_hist['valid multi_logloss-mean'][0])
print(eval_hist['valid multi_logloss-mean'][-1])
print(eval_hist['valid multi_logloss-stdv'][0])
print(eval_hist['valid multi_logloss-stdv'][-1])

print('')
print('test_early_stopping_early')

eval_hist = lgb.cv(regression_params, regression_train, shuffle=False, stratified=False, verbose_eval=True, early_stopping_rounds=5)
print(len(eval_hist['l2-mean']))
eval_hist = lgb.cv(regression_params, regression_train, shuffle=False, stratified=False, callbacks=[lgb.log_evaluation(), lgb.early_stopping(stopping_rounds=5)])
print(len(eval_hist['valid l2-mean']))

print('')
print('test_early_stopping_not_early')

eval_hist = lgb.cv(regression_params, regression_train, shuffle=False, stratified=False, verbose_eval=True, early_stopping_rounds=500)
print(len(eval_hist['l2-mean']))
eval_hist = lgb.cv(regression_params, regression_train, shuffle=False, stratified=False, callbacks=[lgb.log_evaluation(), lgb.early_stopping(stopping_rounds=500)])
print(len(eval_hist['valid l2-mean']))

2 changes: 1 addition & 1 deletion test/support/regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
print('feature_importances', model.feature_importances_.tolist())

print('early_stopping')
model.fit(X_train, y_train, eval_set=[(X_test, y_test)], early_stopping_rounds=5, verbose=True)
model.fit(X_train, y_train, eval_set=[(X_test, y_test)], callbacks=[lgb.early_stopping(stopping_rounds=5), lgb.log_evaluation()])
12 changes: 6 additions & 6 deletions test/support/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
regression_params = {'objective': 'regression', 'verbosity': -1}
regression_train = lgb.Dataset(X_train, label=y_train)
regression_test = lgb.Dataset(X_test, label=y_test)
bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test], verbose_eval=False)
bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test])
y_pred = bst.predict(X_test)
print(np.sqrt(np.mean((y_pred - y_test)**2)))

Expand All @@ -27,7 +27,7 @@
binary_params = {'objective': 'binary', 'verbosity': -1}
binary_train = lgb.Dataset(X_train, label=y_train.replace(2, 1))
binary_test = lgb.Dataset(X_test, label=y_test.replace(2, 1))
bst = lgb.train(binary_params, binary_train, valid_sets=[binary_train, binary_test], verbose_eval=False)
bst = lgb.train(binary_params, binary_train, valid_sets=[binary_train, binary_test])
y_pred = bst.predict(X_test)
print(y_pred[0])

Expand All @@ -37,28 +37,28 @@
multiclass_params = {'objective': 'multiclass', 'num_class': 3, 'verbosity': -1}
multiclass_train = lgb.Dataset(X_train, label=y_train)
multiclass_test = lgb.Dataset(X_test, label=y_test)
bst = lgb.train(multiclass_params, multiclass_train, valid_sets=[multiclass_train, multiclass_test], verbose_eval=False)
bst = lgb.train(multiclass_params, multiclass_train, valid_sets=[multiclass_train, multiclass_test])
y_pred = bst.predict(X_test)
print(y_pred[0].tolist())

print('')
print('test_early_stopping_early')

bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test], early_stopping_rounds=5)
bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test], callbacks=[lgb.early_stopping(stopping_rounds=5), lgb.log_evaluation()])
print(bst.best_iteration)

print('')
print('test_early_stopping_not_early')

bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test], early_stopping_rounds=500)
bst = lgb.train(regression_params, regression_train, valid_sets=[regression_train, regression_test], callbacks=[lgb.early_stopping(stopping_rounds=500), lgb.log_evaluation()])
# appears to be using training set for best iteration instead of validation set
print(bst.best_iteration)

print('')
print('test_early_stopping_early_higher_better')

params = {'objective': 'binary', 'metric': 'auc', 'verbosity': -1}
bst = lgb.train(params, binary_train, valid_sets=[binary_train, binary_test], early_stopping_rounds=5, verbose_eval=False)
bst = lgb.train(params, binary_train, valid_sets=[binary_train, binary_test], callbacks=[lgb.early_stopping(stopping_rounds=5)])
print(bst.best_iteration)

print('')
Expand Down

0 comments on commit dce4dd9

Please sign in to comment.