Getting started with transferboost
This is quick start tutorial providing code snippets for getting started with tboost.
XGBTransferLearner: transfer learning with xgboost
import warnings
warnings.filterwarnings('ignore')
import transferboost as tb
from transferboost.dataset import load_data
# Load the data
X, y1, y2 = load_data(return_X_y=True)
Train an xgboost model and perform the "transfer learning"
Train an xgboost
model on the first target, with y1
import xgboost as xgb
xgb_model = xgb.XGBClassifier(
max_depth = 2,
reg_lambda = 0,
n_estimators=100,
verbosity = 0
)
xgb_model.fit(X,y1)
Transfer Learners expect a fitted model in the constructor.
from transferboost.models import XGBTransferLearner
t_xgb_model = XGBTransferLearner(xgb_model)
Perfrom the "transfer learning" by fitting the XGBTransferLearner on another target, y2
in this case.
t_xgb_model.fit(X,y2)
Get the predicted probabilities with the transfer-learned model.
XGBTransferLearner.predicted_proba(X)
returns the the probabilities (as in any sklearn API).
t_xgb_model.predict_proba(X)
LGBMTransferLearner: transfer learning with ligthgbm
If the baseline model is a lightgbm, the transfer learning procedure is very similar.
Train a lightgbm model and perform the "transfer learning"
As in the xgboost case, train a LGBM classifier on target y1
.
import lightgbm as lgb
lgb_model = lgb.LGBMClassifier(
max_depth = 2,
reg_lambda = 0,
n_estimators=100,
verbosity = 0
)
lgb_model.fit(X,y1)
Use the LGBMTransferLearner
class
from transferboost.models import LGBMTransferLearner
t_lgb_model = LGBMTransferLearner(lgb_model)
Transfer-learn the model to the new target (y2
)
t_lgb_model.fit(X,y2)
Get the predicted probabilities with the transfer-learned model.
LGBMTransferLearner.predicted_proba(X)
returns the the probabilities (as in any sklearn API).
t_lgb_model.predict_proba(X)