Package: lightgbm-tools
LightGBM ToolsThis Python package implements tools forLightGBM.
In the current version lightgbm-tools focuses on binary classification metrics.What exact problem does this tool solve?LightGBM has somebuilt-in metricsthat can be used.
These are useful but limited. Some important metrics are missing.
These are, among others, theF1-scoreand theaverage precision (AP).These metrics can be easily added using this tool.
This happens through a mechanism built into LightGBM where we can assign a callback to thefevalparameter oflightgbm.train(seelightgbm.traindocumentation).MaintainersThis project is maintained by theOne Conversationteam ofDeutsche Telekom AG.UsageYou can find a fully functional example here:https://github.com/telekom/lightgbm-tools/blob/main/examples/main_usage.pyThe easiest way is to use the predefined callback functions. These are:lightgbm_tools.metrics.lgbm_f1_score_callbacklightgbm_tools.metrics.lgbm_accuracy_score_callbacklightgbm_tools.metrics.lgbm_average_precision_score_callbacklightgbm_tools.metrics.lgbm_roc_auc_score_callbacklightgbm_tools.metrics.lgbm_recall_score_callbacklightgbm_tools.metrics.lgbm_precision_score_callbackHere F1 is used as an example to show how the predefined callback functions can be used:importlightgbmfromlightgbm_tools.metricsimportlgbm_f1_score_callbackbst=lightgbm.train(params,train_data,valid_sets=val_data,num_boost_round=6,verbose_eval=False,evals_result=evals_result,feval=lgbm_f1_score_callback,# here we pass the callback to LightGBM)You can also reuse other implementations of metrics.
Here is an example of how to do this using thebalanced_accuracy_scorefrom scikit-learn:importlightgbmfromsklearn.metricsimportbalanced_accuracy_scorefromlightgbm_tools.metricsimportLightGbmEvalFunction,binary_eval_callback_factory# define own custom eval (metric) function for balanced_accuracy_scorelgbm_balanced_accuracy=LightGbmEvalFunction(name="balanced_accuracy",function=balanced_accuracy_score,is_higher_better=True,needs_binary_predictions=True,)# use the factory function to create the callbacklgbm_balanced_accuracy_callback=binary_eval_callback_factory([lgbm_balanced_accuracy])bst=lightgbm.train(params,train_data,valid_sets=val_data,num_boost_round=6,verbose_eval=False,evals_result=evals_result,feval=lgbm_balanced_accuracy_callback,# here we pass the callback to LightGBM)This tool can also be used to calculate multiple metrics at the same time.
It can be done by passing several definitions of metrics (in a list) to thebinary_eval_callback_factory.
The followring predefined metric definitions (LightGbmEvalFunction) are available:lightgbm_tools.metrics.lgbm_f1_scorelightgbm_tools.metrics.lgbm_accuracy_scorelightgbm_tools.metrics.lgbm_average_precision_scorelightgbm_tools.metrics.lgbm_roc_auc_scorelightgbm_tools.metrics.lgbm_recall_scorelightgbm_tools.metrics.lgbm_precision_scoreBelow is an example how to combine F1 and average precision:importlightgbmfromlightgbm_tools.metricsimport(binary_eval_callback_factory,lgbm_average_precision_score,lgbm_f1_score,)# use the factory function to create the callbackcallback=binary_eval_callback_factory([lgbm_average_precision_score,lgbm_f1_score])bst=lightgbm.train(params,train_data,valid_sets=val_data,num_boost_round=6,verbose_eval=False,evals_result=evals_result,feval=callback,# here we pass the callback to LightGBM)Installationlightgbm-tools can be installed with pip:pipinstalllightgbm-toolsTo do development and run unit tests locally, ensure that you have installed all relevant requirements.
You will probably want to install it in "editable mode" if you are developing locally:pipinstall-e.[all]LicensingCopyright (c) 2022Philip May,Deutsche Telekom AGLicensed under theMIT License(the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License by reviewing the fileLICENSEin the repository.