Add initial anomaly_detection task with IsolationForest - #1567
Add initial anomaly_detection task with IsolationForest#1567Muhammad Rashid (PhD) (rashidrao-pk) wants to merge 6 commits into
Conversation
|
@microsoft-github-policy-service agree |
|
cc Kevin Chen (@int-chaos) for review, as discussed in #413. |
There was a problem hiding this comment.
Pull request overview
This PR introduces an initial anomaly detection task type to FLAML, centered around a first estimator implementation based on scikit-learn’s IsolationForest, plus a basic unit test to validate anomaly scoring behavior.
Changes:
- Adds a new task identifier
anomaly_detectionand a correspondingis_anomaly_detection()helper onTask. - Registers a new estimator name
isolation_forestand adds anIsolationForestEstimator(subclassingSKLearnEstimator). - Adds a unit test using synthetic point anomalies to validate prediction output shape and anomaly ranking quality.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/automl/test_anomaly_detection.py | Adds a unit test validating IsolationForest-based anomaly scoring on synthetic data. |
| flaml/automl/task/task.py | Introduces the ANOMALY_DETECTION task constant and Task.is_anomaly_detection(). |
| flaml/automl/task/generic_task.py | Registers the isolation_forest estimator and wires anomaly detection defaults (estimator + metric). |
| flaml/automl/model.py | Adds IsolationForestEstimator implementation and exposes score_samples / decision_function. |
|
Thanks for the review. I will fix the search-space init value, formatting, and Spark-dataframe error path. For the |
|
Hi Muhammad Rashid (PhD) (@rashidrao-pk) , could you add an e2e test for anomaly_detection? Thanks a lot! |
|
Thanks for the suggestion. I've added an end-to-end test for The test now exercises the In addition, I reran the test suite and the project's pre-commit checks locally:
Please let me know if you'd like the e2e test to cover any additional scenarios or edge cases. |
| from flaml import AutoML | ||
| from flaml.automl.model import IsolationForestEstimator |
| elif self.is_anomaly_detection(): | ||
| assert split_type in ["auto", "uniform", "time", "group"] | ||
| return split_type if split_type != "auto" else "uniform" |
| elif self.is_anomaly_detection(): | ||
| return "ap" |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (5)
flaml/automl/task/generic_task.py:1096
- The new split type is accepted, but holdout preparation only creates a validation split for classification or regression (
prepare_data()lines 969–1016). For anomaly detection without an explicitX_val,state.X_valremainsNone, and evaluation subsequently callspredict(None). Extend the uniform holdout branch to split anomaly-detection data as well.
elif self.is_anomaly_detection():
assert split_type in ["auto", "uniform", "time", "group"]
return split_type if split_type != "auto" else "uniform"
flaml/automl/task/generic_task.py:1378
apdoes not currently evaluate anomaly scores for this task.get_y_pred()only uses probability scores for binary tasks, so anomaly detection falls through topredict()and supplies hard labels where1means normal and-1means anomalous. With the 0/1 anomaly labels used by the added test, average precision therefore ranks normals as positives. Add anomaly-specific prediction/label polarity handling (for example, using negatedscore_samples) before makingapthe default.
elif self.is_anomaly_detection():
return "ap"
test/automl/test_anomaly_detection.py:6
- This import is unused and will be flagged by the repository's Ruff F401 check. Remove it unless the test directly instantiates the estimator.
from flaml.automl.model import IsolationForestEstimator
flaml/automl/model.py:1531
IsolationForestsupportsn_jobs, so removing it forces every forest fit to run serially and ignores FLAML's configured worker count. Preserve this parameter as the other sklearn forest estimators do.
params.pop("n_jobs", None)
test/automl/test_anomaly_detection.py:64
- The PR adds
decision_function()as part of the public anomaly-detection API, but this end-to-end test only exercisespredict()andscore_samples(). Add a call and shape assertion so regressions in the third exposed method are covered.
preds = automl.predict(X_val)
scores = automl.model.score_samples(X_val)
assert preds.shape == y_val.shape
assert scores.shape == y_val.shape
Closes #413
This draft PR adds initial support for anomaly detection in FLAML using IsolationForest.
Scope:
anomaly_detectiontask typeis_anomaly_detection()isolation_forestIsolationForestEstimatorsubclassingSKLearnEstimatorpredict,score_samples, anddecision_function1= normal,-1= anomalyNotes:
AutoML.fit(X_train=..., task="anomaly_detection")integration can be completed after maintainer feedback.cc Li Jiang (@thinkall)