A comprehensive suite of unsupervised machine learning models for detecting anomalous vessel behavior in AIS (Automatic Identification System) data, specifically designed for oil spill detection and maritime safety applications.
🎯 Model Overview
This repository contains 5 trained unsupervised anomaly detection models optimized for maritime AIS data analysis:
Model
Type
Accuracy
Anomaly Rate
Best For
Size
IsolationForest 🌟
Ensemble
100% contamination match
10.0%
Recommended - Best overall
1.1MB
LocalOutlierFactor
Density-based
100% contamination match
10.0%
Local anomaly detection
104MB
OneClassSVM
SVM-based
100% contamination match
10.0%
Non-linear patterns
3.9MB
EllipticEnvelope
Statistical
100% contamination match
10.0%
Gaussian distributed data
3.6MB
DBSCAN
Clustering
N/A (parameter-free)
2.1%
Cluster-based anomalies
39MB
🛢️ Oil Spill Detection Pipeline
These models are designed as Step 2A in a comprehensive oil spill detection system:
Run the main script to train the Isolation Forest model:
python ais_anomaly_detection.py
This will:
Load the AIS data from /Users/lakshmikotaru/Downloads/ais_data.csv
Preprocess the data and create features
Train the Isolation Forest model
Generate analysis and visualizations
Save the trained model and results
Using the Trained Model
Use the prediction script to detect anomalies in new data:
bash
1# Use with new data file2python predict_anomalies.py path/to/new_ais_data.csv
34# Or run without arguments to use original data as example5python predict_anomalies.py
Model Configuration
You can adjust the model parameters in ais_anomaly_detection.py:
python
1detector = AISAnomalyDetector(2 contamination=0.1,# Expected fraction of anomalies (10%)3 random_state=42# For reproducible results4)
Data Format
The AIS data should be a CSV file with the following columns:
mmsi: Maritime Mobile Service Identity
navigationalstatus: Current navigation status
sog: Speed Over Ground (knots)
cog: Course Over Ground (degrees)
heading: Vessel heading (degrees)
shiptype: Type of vessel
width: Vessel width (meters)
length: Vessel length (meters)
draught: Vessel draught (meters)
Output Files
ais_isolation_forest_model.joblib
The trained model file that can be loaded for future predictions.
detected_anomalies.csv
Detailed information about all detected anomalies, including:
Original vessel data
Anomaly scores
Binary anomaly flags
anomaly_analysis_plots.png
Comprehensive visualization showing:
Anomaly score distributions
Feature comparisons between normal and anomalous vessels
Scores are relative to the training data distribution
Common Anomaly Types
The model may detect:
Vessels with unusual speed patterns
Ships with inconsistent course/heading relationships
Vessels with atypical dimensions for their type
Unusual combinations of vessel characteristics
Example Output
============================================================
AIS DATA ANOMALY DETECTION USING ISOLATION FOREST
============================================================
Loading data from /Users/lakshmikotaru/Downloads/ais_data.csv...
Loaded 358351 records with 9 columns
Training Isolation Forest model...
Model training completed!
Number of anomalies detected: 35835 out of 358351 samples
Anomaly rate: 10.00%
ANOMALY ANALYSIS SUMMARY
============================================================
Normal samples: 322516 (90.0%)
Anomalous samples: 35835 (10.0%)
Customization
Adding New Features
To add new derived features, modify the preprocess_data method in the AISAnomalyDetector class.
Changing Model Parameters
Adjust the IsolationForest parameters in the __init__ method:
n_estimators: Number of trees in the forest
contamination: Expected proportion of anomalies
max_samples: Number of samples to draw for each tree
Visualization
Modify the visualize_results method to add new plots or change existing ones.
Notes
The model is unsupervised, so it learns patterns without labeled anomalies
Results should be validated by domain experts
The contamination parameter significantly affects the number of detected anomalies
Missing values are handled automatically during preprocessing
Troubleshooting
Import errors: Make sure all requirements are installed
File not found: Check that the AIS data file path is correct
Memory issues: For very large datasets, consider processing in chunks
Plotting issues: Ensure matplotlib backend is properly configured
Contact
Generated for AIS Anomaly Detection Project - 2025-01-11