Environment:Scikit learn contrib Imbalanced learn Keras TensorFlow
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Imbalanced_Classification |
| Last Updated | 2026-02-09 03:00 GMT |
Overview
Python 3.10+ environment with Keras >= 3.3.3 or TensorFlow >= 2.16.1 for balanced deep learning batch generation.
Description
This environment extends the core Python/scikit-learn environment with deep learning support. It provides the `BalancedBatchGenerator` class which creates balanced mini-batches for training Keras/TensorFlow models on imbalanced datasets. The generator inherits from either `keras.utils.PyDataset` (Keras 3.x) or `keras.utils.Sequence` (older versions), enabling seamless integration with `model.fit()`. The library supports two import paths: standalone Keras or TensorFlow-bundled Keras.
Usage
Use this environment when training deep learning models on imbalanced data using Keras or TensorFlow. It is the mandatory prerequisite for the BalancedBatchGenerator implementation and the `balanced_batch_generator` function.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux or macOS | Windows is not supported for Keras/TensorFlow integration |
| Hardware | CPU (GPU optional) | GPU acceleration depends on backend framework configuration |
| Platforms (pixi) | linux-64, osx-arm64, osx-64 | win-64 excluded for Keras/TensorFlow features |
Dependencies
System Packages
No additional system packages beyond those required by TensorFlow/Keras.
Python Packages (Core, inherited from Python_Scikit_learn)
- `python` >= 3.10
- `numpy` >= 1.25.2, < 3
- `scipy` >= 1.11.4, < 2
- `scikit-learn` >= 1.4.2, < 2
- `sklearn-compat` >= 0.1.5, < 0.2
- `joblib` >= 1.2.0, < 2
- `threadpoolctl` >= 2.0.0, < 4
Python Packages (Deep Learning, choose one)
Option A: Standalone Keras
- `keras` >= 3.3.3, < 4
Option B: TensorFlow (includes Keras)
- `tensorflow` >= 2.16.1, < 3
- `keras` >= 3.3.3, < 3.9 (when used with TensorFlow)
Credentials
No credentials or environment variables are required.
Quick Install
# Option A: With standalone Keras
pip install imbalanced-learn keras>=3.3.3
# Option B: With TensorFlow (includes Keras)
pip install imbalanced-learn tensorflow>=2.16.1
Code Evidence
Dual import mechanism from `imblearn/keras/_generator.py:10-48`:
def import_keras():
"""Try to import keras from keras and tensorflow."""
def import_from_keras():
try:
import keras
if hasattr(keras.utils, "Sequence"):
return (keras.utils.Sequence,), True
else:
return (keras.utils.PyDataset,), True
except ImportError:
return tuple(), False
def import_from_tensforflow():
try:
from tensorflow import keras
if hasattr(keras.utils, "Sequence"):
return (keras.utils.Sequence,), True
else:
return (keras.utils.PyDataset,), True
except ImportError:
return tuple(), False
ParentClassKeras, has_keras_k = import_from_keras()
ParentClassTensorflow, has_keras_tf = import_from_tensforflow()
has_keras = has_keras_k or has_keras_tf
Lazy loading to avoid import-time errors from `imblearn/__init__.py:109-111`:
# delay the import of keras since we are going to import either tensorflow
# or keras
keras = LazyLoader("keras", globals(), "imblearn.keras")
Test-time skip when TensorFlow is not installed from `conftest.py:27-30`:
try:
import tensorflow # noqa
except ImportError:
pytest.skip("The tensorflow package is not installed.")
Platform restriction from `pyproject.toml:132-134`:
[tool.pixi.feature.keras]
platforms = ["linux-64", "osx-arm64", "osx-64"]
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `ImportError: 'No module named 'keras'` | Neither keras nor tensorflow installed | `pip install keras>=3.3.3` or `pip install tensorflow>=2.16.1` |
| `pytest.skip: The tensorflow package is not installed` | TensorFlow not available during testing | Install tensorflow to run Keras/TF generator tests |
| `AttributeError: module 'keras.utils' has no attribute 'Sequence'` | Using Keras 3.x which renamed Sequence to PyDataset | The library handles this automatically; ensure keras >= 3.3.3 |
Compatibility Notes
- Windows: Not supported. The pixi configuration explicitly excludes win-64 from Keras/TensorFlow features.
- Keras 3.x vs older: The library auto-detects whether `keras.utils.Sequence` (old API) or `keras.utils.PyDataset` (Keras 3.x API) is available and uses the appropriate parent class.
- Standalone Keras vs TensorFlow Keras: Both import paths are supported. Standalone Keras is tried first; if unavailable, `tensorflow.keras` is used as fallback.
- TensorFlow Keras version cap: When using TensorFlow, keras must be < 3.9 due to compatibility constraints.