Skip to content

Commit bf35f98

Browse files
Copilotthinkall
andauthored
Document missing value handling behavior for AutoML estimators (#1473)
* Initial plan * Add comprehensive documentation on missing value handling in FAQ Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> * Apply mdformat to FAQ.md Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> * Correct FAQ: FLAML does preprocess missing values with SimpleImputer Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: thinkall <3197038+thinkall@users.noreply.github.com> Co-authored-by: Li Jiang <bnujli@gmail.com>
1 parent 1687ca9 commit bf35f98

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

website/docs/FAQ.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,107 @@
1515

1616
- How does it work: `low_cost_partial_config` if configured, will be used as an initial point of the search. It also affects the search trajectory. For more details about how does it play a role in the search algorithms, please refer to the papers about the search algorithms used: Section 2 of [Frugal Optimization for Cost-related Hyperparameters (CFO)](https://arxiv.org/pdf/2005.01571.pdf) and Section 3 of [Economical Hyperparameter Optimization with Blended Search Strategy (BlendSearch)](https://openreview.net/pdf?id=VbLH04pRA3).
1717

18+
### How does FLAML handle missing values?
19+
20+
FLAML automatically preprocesses missing values in the input data through its `DataTransformer` class (for classification/regression tasks) and `DataTransformerTS` class (for time series tasks). The preprocessing behavior differs based on the column type:
21+
22+
**Automatic Missing Value Preprocessing:**
23+
24+
FLAML performs the following preprocessing automatically when you call `AutoML.fit()`:
25+
26+
1. **Numerical/Continuous Columns**: Missing values (NaN) in numerical columns are imputed using `sklearn.impute.SimpleImputer` with the **median strategy**. This preprocessing is applied in the `DataTransformer.fit_transform()` method (see `flaml/automl/data.py` lines 357-369 and `flaml/automl/time_series/ts_data.py` lines 429-440).
27+
28+
1. **Categorical Columns**: Missing values in categorical columns (object, category, or string dtypes) are filled with a special placeholder value `"__NAN__"`, which is treated as a distinct category.
29+
30+
**Example of automatic preprocessing:**
31+
32+
```python
33+
from flaml import AutoML
34+
import pandas as pd
35+
import numpy as np
36+
37+
# Data with missing values
38+
X_train = pd.DataFrame(
39+
{
40+
"num_feature": [1.0, 2.0, np.nan, 4.0, 5.0],
41+
"cat_feature": ["A", "B", None, "A", "B"],
42+
}
43+
)
44+
y_train = [0, 1, 0, 1, 0]
45+
46+
# FLAML automatically handles missing values
47+
automl = AutoML()
48+
automl.fit(X_train, y_train, task="classification", time_budget=60)
49+
# Numerical NaNs are imputed with median, categorical None becomes "__NAN__"
50+
```
51+
52+
**Estimator-Specific Native Handling:**
53+
54+
After FLAML's preprocessing, some estimators have additional native missing value handling capabilities:
55+
56+
- **`lgbm`** (LightGBM): After preprocessing, can still handle any remaining NaN values natively by learning optimal split directions.
57+
- **`xgboost`** (XGBoost): After preprocessing, can handle remaining NaN values by learning the best direction during training.
58+
- **`xgb_limitdepth`** (XGBoost with depth limit): Same as `xgboost`.
59+
- **`catboost`** (CatBoost): After preprocessing, has additional sophisticated missing value handling strategies. See [CatBoost documentation](https://catboost.ai/en/docs/concepts/algorithm-missing-values-processing).
60+
- **`histgb`** (HistGradientBoosting): After preprocessing, can still handle NaN values natively.
61+
62+
**Estimators that rely on preprocessing:**
63+
64+
These estimators rely on FLAML's automatic preprocessing since they cannot handle missing values directly:
65+
66+
- **`rf`** (RandomForest): Requires preprocessing (automatically done by FLAML).
67+
- **`extra_tree`** (ExtraTrees): Requires preprocessing (automatically done by FLAML).
68+
- **`lrl1`**, **`lrl2`** (LogisticRegression): Require preprocessing (automatically done by FLAML).
69+
- **`kneighbor`** (KNeighbors): Requires preprocessing (automatically done by FLAML).
70+
- **`sgd`** (SGDClassifier/Regressor): Require preprocessing (automatically done by FLAML).
71+
72+
**Advanced: Customizing Missing Value Handling**
73+
74+
In most cases, FLAML's automatic preprocessing (median imputation for numerical, "__NAN__" for categorical) works well. However, if you need custom preprocessing:
75+
76+
1. **Skip automatic preprocessing** using the `skip_transform` parameter:
77+
78+
```python
79+
from flaml import AutoML
80+
from sklearn.impute import SimpleImputer
81+
import numpy as np
82+
83+
# Custom preprocessing with different strategy
84+
imputer = SimpleImputer(strategy="mean") # Use mean instead of median
85+
X_train_preprocessed = imputer.fit_transform(X_train)
86+
X_test_preprocessed = imputer.transform(X_test)
87+
88+
# Skip FLAML's automatic preprocessing
89+
automl = AutoML()
90+
automl.fit(
91+
X_train_preprocessed,
92+
y_train,
93+
task="classification",
94+
time_budget=60,
95+
skip_transform=True, # Skip automatic preprocessing
96+
)
97+
```
98+
99+
2. **Use sklearn Pipeline** for integrated custom preprocessing:
100+
101+
```python
102+
from flaml import AutoML
103+
from sklearn.pipeline import Pipeline
104+
from sklearn.impute import SimpleImputer, KNNImputer
105+
106+
# Custom pipeline with KNN imputation
107+
pipeline = Pipeline(
108+
[
109+
("imputer", KNNImputer(n_neighbors=5)), # Custom imputation strategy
110+
("automl", AutoML()),
111+
]
112+
)
113+
114+
pipeline.fit(X_train, y_train)
115+
```
116+
117+
**Note on time series forecasting**: For time series tasks (`ts_forecast`, `ts_forecast_panel`), the `DataTransformerTS` class applies the same preprocessing approach (median imputation for numerical columns, "__NAN__" for categorical). Missing values handling in the time dimension may require additional consideration depending on your specific forecasting model.
118+
18119
### How does FLAML handle imbalanced data (unequal distribution of target classes in classification task)?
19120

20121
Currently FLAML does several things for imbalanced data.

0 commit comments

Comments
 (0)