train_anomaly_model
Train an unsupervised anomaly detection model on healthy machine data. Extracts features, applies PCA, and fits OneClassSVM or LocalOutlierFactor to detect faults.
Instructions
Train ML-based anomaly detection model on healthy data (UNSUPERVISED/SEMI-SUPERVISED).
All signals are referenced by signal_id: load them first with
load_signal — its batch form accepts a list of file paths, e.g.
load_signal(filepath=["real_train/baseline_1.csv", ...]). Each
signal's sampling rate comes from its stored metadata.
Complete pipeline:
1. Extract features from healthy signals (segmentation + time-domain features)
2. Standardize features (StandardScaler - fitted on training data only)
3. Dimensionality reduction (PCA with specified variance explained)
4. Train novelty detection model (OneClassSVM or LocalOutlierFactor) on HEALTHY DATA ONLY
5. Optional hyperparameter tuning using validation data (semi-supervised)
6. Save model, scaler, and PCA transformer
**Training Mode:**
- UNSUPERVISED: Train only on healthy data with automatic hyperparameters
- SEMI-SUPERVISED: Train on healthy data, tune hyperparameters using validation set (healthy + fault)
**Note:** This is NOT supervised learning. OneClassSVM/LOF are trained ONLY on healthy data.
Fault data (if provided) is used ONLY for hyperparameter tuning after training.
**Validation Strategy:**
- If healthy_validation_ids provided: Use those explicitly (no split)
- If healthy_validation_ids NOT provided: Automatic 80/20 split of training data
- If fault_signal_ids provided: Enable semi-supervised mode (hyperparameter tuning)
Args:
healthy_signal_ids: Stored signal IDs with healthy machine data (for training)
segment_duration: Segment duration in seconds (default: 0.1)
overlap_ratio: Overlap ratio 0-1 (default: 0.5)
model_type: 'OneClassSVM' or 'LocalOutlierFactor' (default: 'OneClassSVM')
pca_variance: Cumulative variance to explain with PCA (default: 0.95)
fault_signal_ids: Optional stored signal IDs for HYPERPARAMETER TUNING (semi-supervised)
healthy_validation_ids: Optional stored healthy signal IDs for validation (specificity check).
If not provided, 20% of training data will be used.
model_name: Name for saved model files (default: 'anomaly_model')
ctx: MCP context for progress/logging
Returns:
AnomalyModelResult with model paths and performance metrics
Raises:
ValueError: If a signal_id is not loaded or has no sampling rate,
or model_name/model_type is invalid.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| model_name | No | anomaly_model | |
| model_type | No | OneClassSVM | |
| pca_variance | No | ||
| overlap_ratio | No | ||
| fault_signal_ids | No | ||
| segment_duration | No | ||
| healthy_signal_ids | Yes | ||
| healthy_validation_ids | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pca_path | Yes | Path to saved PCA file (.pkl) | |
| model_name | Yes | Name under which the model was saved — pass this to predict_anomalies(model_name=...) | |
| model_path | Yes | Path to saved model file (.pkl) | |
| model_type | Yes | Type of model: 'OneClassSVM' or 'LocalOutlierFactor' | |
| scaler_path | Yes | Path to saved scaler file (.pkl) | |
| model_params | Yes | Best model hyperparameters | |
| num_features_pca | Yes | Number of PCA components (features after dimensionality reduction) | |
| validation_details | No | Validation details with healthy and fault metrics | |
| validation_metrics | No | Detailed validation metrics (healthy/fault accuracy breakdown) | |
| variance_explained | Yes | Cumulative variance explained by PCA components | |
| validation_accuracy | No | Overall balanced accuracy on healthy + fault validation data | |
| num_training_samples | Yes | Number of healthy samples used for training | |
| num_features_original | Yes | Number of original features |