Imbalance-XGBoost

raw JSON →
0.8.1 verified Sat May 09 auth: no python maintenance

Imbalance-XGBoost is a Python package that extends XGBoost with weighted and focal loss functions for label-imbalanced data. Current version 0.8.1 requires XGBoost >=1.1.1. Release cadence is sporadic; last release was June 2022.

pip install imbalance-xgboost
error ModuleNotFoundError: No module named 'imxgboost'
cause Common typo: package is installed as 'imbalance-xgboost' but imported as 'imxgboost' (no underscore).
fix
Ensure you have installed imbalance-xgboost and use import imxgboost correctly.
error AttributeError: module 'imxgboost' has no attribute 'XGBClassifier'
cause Attempting to import from wrong submodule or using outdated import pattern.
fix
Use from imxgboost import XGBClassifier.
error xgboost.core.XGBoostError: No available GPU?
cause Trying to use GPU with a CPU-only XGBoost installation or mismatched XGBoost version.
fix
Install XGBoost with GPU support: pip install xgboost-gpu or set tree_method='hist'.
breaking Requires XGBoost >=1.1.1. Older XGBoost versions will cause import errors or silent failures.
fix Upgrade XGBoost: pip install --upgrade xgboost>=1.1.1
gotcha The package is often imported as `imxgboost` (not `imbalance_xgboost`). Using the wrong module name leads to ModuleNotFoundError.
fix Use `from imxgboost import ...` (no underscore).
gotcha Focal loss and weighted loss parameters are passed as strings (e.g., objective='focal'). Misspelling or case mismatch (e.g., 'Focal') will silently fall back to default objective.
fix Use exactly 'focal' or 'weighted' for objective.
deprecated The package is in maintenance mode; no new features expected. Consider using XGBoost native weighted loss or focal loss via custom objective as alternatives.
fix For active development, use XGBoost's native `scale_pos_weight` or implement custom focal loss.

Train XGBoost with focal loss and weighted loss on imbalanced data.

import pandas as pd
from sklearn.model_selection import train_test_split
from imxgboost import XGBClassifier

# Load data
from sklearn.datasets import make_classification
X, y = make_classification(n_classes=2, weights=[0.9, 0.1], n_samples=1000, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Model with focal loss
model = XGBClassifier(objective='focal', focal_gamma=2.0, scale_pos_weight=None)
model.fit(X_train, y_train)
preds = model.predict(X_test)
print('Accuracy:', (preds == y_test).mean())

# Weighted loss (sample weights)
weights = [10 if yi == 0 else 1 for yi in y_train]
model2 = XGBClassifier(objective='weighted')
model2.fit(X_train, y_train, sample_weight=weights)
print('Weighted model accuracy:', (model2.predict(X_test) == y_test).mean())