Implementation:Scikit learn contrib Imbalanced learn Package Init
Implementation: Scikit-learn-contrib Imbalanced-learn Package Init
Source: imblearn/__init__.py (lines 1-127)
Purpose: Top-level package initializer for the imbalanced-learn library. Imports all subpackages, exposes FunctionSampler, show_versions, and __version__ at the top level. Uses a LazyLoader for the keras subpackage to avoid pulling in heavy TensorFlow dependencies until they are actually needed.
Import:
import imblearn
Key Exports
The module defines an explicit __all__ list controlling the public API surface:
| Export | Type | Description |
|---|---|---|
combine |
subpackage | Methods based on over-sampling and under-sampling combined |
ensemble |
subpackage | Methods generating an ensemble of under-sampled subsets |
exceptions |
subpackage | Custom warnings and error classes used across imbalanced-learn |
keras |
subpackage (lazy) | Custom generators and layers for deep learning using Keras |
metrics |
subpackage | Metrics to quantify classification performance with imbalanced datasets |
model_selection |
subpackage | Methods to split the dataset into training and test sets |
over_sampling |
subpackage | Methods to over-sample a dataset |
tensorflow |
subpackage | Custom generators and layers for deep learning using TensorFlow |
under_sampling |
subpackage | Methods to under-sample a dataset |
utils |
subpackage | Various utilities |
pipeline |
subpackage | Pipeline creation with scikit-learn estimators |
FunctionSampler |
class | User-defined sampling callable wrapped as an estimator |
__version__ |
string | Library version string |
Build-time Guard
The module uses a conditional import guard (__IMBLEARN_SETUP__) to support partial imports during the build process. When this flag is set (injected into __builtins__ by the build system), only a warning is written to stderr and no subpackages are imported:
try:
__IMBLEARN_SETUP__ # type: ignore
except NameError:
__IMBLEARN_SETUP__ = False
if __IMBLEARN_SETUP__:
sys.stderr.write("Partial import of imblearn during the build process.\n")
else:
from . import (
combine,
ensemble,
exceptions,
metrics,
model_selection,
over_sampling,
pipeline,
tensorflow,
under_sampling,
utils,
)
from ._version import __version__
from .base import FunctionSampler
from .utils._show_versions import show_versions # noqa: F401
LazyLoader
The keras subpackage is loaded through a custom LazyLoader class adapted from TensorFlow's lazy loading pattern. This avoids importing keras (and transitively TensorFlow) until the user actually accesses imblearn.keras:
class LazyLoader(types.ModuleType):
"""Lazily import a module, mainly to avoid pulling in large dependencies.
Adapted from TensorFlow:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/
python/util/lazy_loader.py
"""
def __init__(self, local_name, parent_module_globals, name, warning=None):
self._local_name = local_name
self._parent_module_globals = parent_module_globals
self._warning = warning
super().__init__(name)
def _load(self):
"""Load the module and insert it into the parent's globals."""
module = importlib.import_module(self.__name__)
self._parent_module_globals[self._local_name] = module
self.__dict__.update(module.__dict__)
return module
def __getattr__(self, item):
module = self._load()
return getattr(module, item)
def __dir__(self):
module = self._load()
return dir(module)
The lazy loader is instantiated as:
keras = LazyLoader("keras", globals(), "imblearn.keras")
On first attribute access, _load() calls importlib.import_module("imblearn.keras"), replaces the LazyLoader instance in globals() with the real module, and updates the loader's own __dict__ so subsequent lookups on stale references are still efficient.
Top-level Convenience Imports
Two additional symbols are imported at the top level for convenience:
from .base import FunctionSampler
from ._version import __version__
from .utils._show_versions import show_versions
This allows users to write imblearn.FunctionSampler or imblearn.__version__ directly without diving into submodules.