This project provides a powerful and modular implementation for activity recognition and anomaly detection in smart home environments using the CASAS dataset. It supports multi-class classification using traditional machine learning (Random Forest) and neural networks (MLP, Adaline, Linear Regression) with options for anomaly alerting and explainable outputs.
- Activity classification from raw timestamped sensor data
- Multiple learning algorithms:
random forest,MLP,Adaline,linear regression - Customizable training modes:
TRAIN,CV,PARTITION,LOO,TEST,ANNOTATE - Anomaly alerts for irregular behaviors (e.g., "Leave_Home", "Enter_Home", "Evening_Meds")
- Automatic clustering of "Other_Activity" into subclasses using k-means
- Visualization of irregular activity alerts over time
- Flexible feature extraction and preprocessing pipeline
al.py # Main script for training/testing/predicting
config.py # Global settings and argparse parsing
data/ # Sensor data file (default: "data")
model/ # Trained models and config files saved here
# (Recommended) Create a virtual environment
python -m venv .venv
source .venv/bin/activate # or .venv\Scripts\activate on Windows
# Install required packages
pip install -r requirements.txtpython al.py --mode PARTITION --data data --algorithm rfpython al.py --mode PARTITION --data data --algorithm adaline --epochs 50 --logepochspython al.py --mode PARTITION --data data --algorithm mlp --epochs 50 --irregular_threshold 0.4python al.py --mode LOO --data data/f1 data/f2 data/f3 --algorithm rfpython al.py --mode ANNOTATE --data data/newdata.txt --model model| Feature | Description |
|---|---|
| ✅ Gradient-based learners | Added Adaline, Linear Regression, and MLP support |
| ✅ Epoch logging | --logepochs prints training loss and accuracy per epoch |
| ✅ Irregular behavior alerts | Activity-specific alerts for nurse or police actions |
| ✅ Threshold control | --irregular_threshold determines confidence threshold for alerts |
| ✅ Cluster "Other_Activity" | --clusterother splits ambiguous activity into meaningful clusters |
| ✅ PCA augmentation | Optional dimensionality boost with --add_pca (manually set) |
| ✅ Leave-one-out testing | Cross-subject generalization via --mode LOO |
✅ Modular torch.nn.Module training |
Unified gradient descent training via _train_gradient() |
Epoch 1/50: loss=180.272980 acc=0.038
...
[ALERT] Irregular “Evening_Meds” in sample 20501 (conf=0.04) – calling nurse!
[INFO] No irregular activities detected.
If irregular alerts are triggered, a scatter plot of alert timestamps is generated by plot_irregular_events():
- X-axis: Hour of Day
- Y-axis: Day of Week
- Color: Day of Week
- Sensor and activity mappings are defined in
config.py - Feature vectors include 14 statistical/temporal attributes + per-sensor counts
- Window size = 30 events; configurable in
Config
Diane J. Cook, Washington State University – Original CASAS codebase and datasets
2010-01-02 00:00:05.173837 M001 M001 ON Bed_Toilet_Transition
2010-01-02 00:00:14.205359 M002 M002 ON Bed_Toilet_Transition
...
| Use Case | Command |
|---|---|
| Debug first feature vector | --featuredump |
| Ignore "Other_Activity" in training | --ignoreother |
| Automatically cluster "Other_Activity" | --clusterother |
| Anomaly = low model confidence | --irregular_threshold 0.4 (default 0.80) |
python al.py --mode PARTITION --algorithm adaline --data data --activities "Enter_Home,Leave_Home,Personal_Hygiene,Evening_Meds,Sleep_Out_Of_Bed,Step_Out,Morning_Meds,Bathe" --epochs 50 --logepochsThis code and data are adapted from the CASAS Smart Home Project by WSU and are for academic use only.