Missforest wrapper
fyt.wrappers.missforest_wrapper
¶
MissForestWrapped
¶
Bases: BaseEstimator, TransformerMixin
A wrapper around MissForest for polars/pandas DataFrame I/O and sklearn compatibility.
This wrapper extends the MissForest imputer from the missforest library to be compatible with scikit-learn pipelines. Accepts polars DataFrames as input, converts internally to pandas for MissForest, and returns polars DataFrames as output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clf
|
BaseEstimator | None
|
Classifier estimator to use for categorical features (optional). If None, uses the default MissForest classifier (LightGBM). |
None
|
rgr
|
BaseEstimator | None
|
Regressor estimator to use for numerical features (optional). If None, uses the default MissForest regressor (LightGBM). |
None
|
categorical
|
list[str] | None
|
List of categorical column names (optional). These columns will be identified and handled appropriately during imputation. |
None
|
random_state
|
int | None
|
Seed for reproducible imputation (optional). MissForest itself has no seed parameter; randomness enters through its internal LightGBM estimators. When random_state is set and no custom estimators are provided, seeded LGBMClassifier/LGBMRegressor instances are built and passed to MissForest so that repeated runs produce identical imputations. Ignored when both clf and rgr are provided (seed those estimators directly instead). |
None
|
**kwargs
|
object
|
Additional parameters to pass to the MissForest constructor (e.g., max_iter, initial_guess, etc.). |
{}
|
Example
Using custom estimators (created via dependency injection)¶
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
clf = RandomForestClassifier(n_estimators=100, random_state=42) rgr = RandomForestRegressor(n_estimators=100, random_state=42)
imputer = MissForestWrapped( clf=clf, rgr=rgr, categorical=["sex", "smoker"], max_iter=10 )
Using default estimators¶
imputer = MissForestWrapped(categorical=["sex", "smoker"])
__getattr__(name)
¶
Delegate attribute access to the underlying MissForest instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
|
Attribute value from MissForest instance. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If attribute not found. |
fit(X, y=None)
¶
Fit the MissForest imputer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame | DataFrame
|
polars or pandas DataFrame with missing values. |
required |
y
|
object
|
Ignored, present for sklearn compatibility. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
MissForestWrapped
|
Fitted transformer. |
fit_transform(X, y=None, **fit_params)
¶
Fit the imputer and transform the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame | DataFrame
|
polars or pandas DataFrame with missing values. |
required |
y
|
object
|
Ignored, present for sklearn compatibility. |
None
|
**fit_params
|
object
|
Additional fit parameters forwarded to fit(). |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
X_imputed |
polars DataFrame with imputed values. |
get_params(deep=True)
¶
Get parameters for this estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
If True, will return the parameters for this estimator and contained subobjects that are estimators. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dictionary of parameter names mapped to their values. |
set_output(*, transform=None)
¶
Set output configuration for compatibility with sklearn pipelines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
str | None
|
Output format for transform method. Not used in this wrapper. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MissForestWrapped |
MissForestWrapped
|
The transformer itself. |
set_params(**params)
¶
Set the parameters of this estimator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
object
|
Estimator parameters. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
MissForestWrapped
|
Estimator instance. |
transform(X)
¶
Transform the data using fitted MissForest parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame | DataFrame
|
polars or pandas DataFrame with missing values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
X_imputed |
polars DataFrame with imputed values. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the imputer has not been fitted yet. |