Add sklearn-compatible adapter (anomaly, forecasting, embedding) - #86
Open
DrLHS wants to merge 1 commit into
Open
Add sklearn-compatible adapter (anomaly, forecasting, embedding)#86DrLHS wants to merge 1 commit into
DrLHS wants to merge 1 commit into
Conversation
Three sklearn estimator-API adapters that let MOMENT drop into sklearn.pipeline.Pipeline, ColumnTransformer, GridSearchCV, and any other tooling that follows the sklearn contract. - MOMENTAnomalyDetector (TransformerMixin): reconstruction-MSE per-sample anomaly score. - MOMENTForecaster (RegressorMixin): predict(X) returns flat 2D forecast horizon. - MOMENTEmbedder (TransformerMixin): transform(X) returns encoder embeddings. All three adapters accept 2D (n_samples, n_channels * context_length) or 3D (n_samples, n_channels, context_length) input. fit() loads the pretrained weights zero-shot; predict/transform perform inference. Tests: 13 sklearn-API unit tests pass; 5 end-to-end smoke tests are marked skip by default since they need downloaded weights. README adds a scikit-learn Integration section.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
momentfm.sklearn_adapter, three sklearn estimator-API adapters that let MOMENT drop intosklearn.pipeline.Pipeline,ColumnTransformer,GridSearchCV, and any other tooling that follows the sklearn contract.Adapters
MOMENTAnomalyDetector(TransformerMixin)— reconstruction-MSE per-sample anomaly score.transform(X)returns shape(n_samples,);score_samples(X)returns the negated score in the sklearn outlier-detector convention (higher = more normal).MOMENTForecaster(RegressorMixin)—predict(X)returns the forecast horizon as a flat 2D array of shape(n_samples, n_channels * forecast_horizon)so it slots into sklearn pipelines without a custom reshape step. Construct withMOMENTForecaster(forecast_horizon=H).MOMENTEmbedder(TransformerMixin)—transform(X)returns the encoder embedding as a 2D feature matrix(n_samples, embedding_dim)so downstream sklearn estimators (LogisticRegression, KMeans, t-SNE) can consume them directly.Input shape
All three adapters accept either 2D
(n_samples, n_channels * context_length)input (sklearn pipeline convention) or 3D(n_samples, n_channels, context_length)(MOMENT native). Shape coercion is centralised in a_to_3dhelper with explicit error messages.fit contract
fitis zero-shot by default — it loads the pretrained weights and sets the model toeval(). This matches MOMENT's intended zero-shot-foundation-model usage. Callingpredictortransformbeforefitis equivalent to callingfitfirst.Tests
tests/test_sklearn_adapter.pyadds 18 tests:13 unit tests pass on the current branch; 5 integration tests correctly skip.
README
A new
## 🧩 scikit-learn Integrationsection just before the tutorials section shows the three usage idioms.What this enables
Files changed
Companion to #85 (CI) and #84 (release-cut request).