gbt is a library for gradient boosted trees with minimal coding required. It is a thin wrapper around lightgbm.
- Zero feature engineering needed - automatic encoding of categorical features
- Built-in train/validation splitting
- Automatic artifact saving and loading
- Pre-configured model presets for common tasks (binary, multiclass, regression)
What you need:
- a pandas dataframe,
- the target column to predict on,
- categorical feature columns (can be empty),
- numerical feature columns (can be empty, but you should have at least one categorical or numerical feature),
- model_lib: "binary", "multiclass", "mape", "l2" to specify what type of prediction objective and default hyperparameters to use.
You don't need to (though you are welcome to):
- normalize the numerical feature values
- construct the encoder to one-hot encode categorical features
- manage saving of artifacts for above feature transformation
- implement evaluation metrics
- Python 3.7+
- pandas, numpy, scikit-learn, lightgbm
pip install gbt
import pandas as pd
from gbt import train
# Your data
df = pd.DataFrame({
"feature_1": [1, 2, 3, 4, 5],
"category": ["A", "B", "A", "C", "B"],
"target": [0, 1, 0, 1, 1]
})
# Train model
model = train(
df,
model_lib="binary", # binary, multiclass, mape, or l2
label_column="target",
categorical_feature_columns=["category"],
numerical_feature_columns=["feature_1"]
)
# Make predictions
new_data = pd.DataFrame({
"feature_1": [6, 7],
"category": ["A", "B"]
})
predictions = model.predict(new_data)
print(predictions) # [0.23, 0.78] # Save model
model.save("my_model")
# Load model later
from gbt import load
loaded_model = load("my_model")
predictions = loaded_model.predict(new_data)For more control over training:
from gbt import TrainingPipeline
# Custom training configuration
pipeline = TrainingPipeline(
categorical_feature_columns=["category"],
numerical_feature_columns=["feature_1"],
params_preset="binary",
params_override={"num_leaves": 50}, # Custom hyperparameters
val_size=0.3, # 30% validation split
verbose=False # Quiet training
)
# Train with custom data loader
class DatasetBuilder:
def training_dataset(self):
return pd.read_csv("train.csv")
def testing_dataset(self):
return pd.read_csv("test.csv")
pipeline.fit(DatasetBuilder())
# Get model for deployment
model = pipeline.create_model()
model.save("production_model")train(df, model_lib="l2", label_column, categorical_feature_columns, numerical_feature_columns, **kwargs)
Train a gradient boosting model.
Parameters:
df: Training dataframemodel_lib: Model type -"binary","multiclass","mape", or"l2"label_column: Target column namecategorical_feature_columns: List of categorical feature namesnumerical_feature_columns: List of numerical feature namesval_size: Validation split fraction (default 0.2)log_dir: Directory to save artifacts (optional)
Returns: Trained model ready for prediction
Load a saved model.
Returns: Model ready for inference
model_lib |
Use Case | Loss Function |
|---|---|---|
"binary" |
Binary classification | Log loss |
"multiclass" |
Multi-class classification | Multi-class log loss |
"l2" |
Regression | Mean squared error |
"mape" |
Regression | Mean absolute percentage error |
.predict(df): Make predictions.save(path): Save model to disk
For advanced usage, see TrainingPipeline class documentation.