A probabilistic machine learning system for predicting area occupancy based on sensor data. This project uses Naive Bayes and Hidden Markov Models to analyze historical sensor readings and predict whether areas are currently occupied.
- Time-based Priors: Learn occupancy patterns by day of week and time of day
- Sensor Likelihoods: Calculate probability of sensor readings given occupancy state
- Naive Bayes Prediction: Real-time occupancy prediction from current sensor readings
- HMM Smoothing: Temporal smoothing of occupancy predictions over time
- Multi-sensor Support: Handles motion sensors, light sensors, and other binary sensors
- CLI Interface: Command-line tools for training models and making predictions
- Clone the repository:
git clone <repository-url>
cd area_probability- Install dependencies:
pip install -r requirements.txt- Initialize the database:
python main.py initImport sensor data from a SQL file:
python main.py import --file your_data.sqlCompute time-based priors and sensor likelihoods for all areas:
python main.py learn-all --slot 60Or train for a specific area:
python main.py priors living_room --slot 60
python main.py likelihoods living_roomPredict occupancy based on current sensor readings:
python main.py predict living_room --entities motion_sensor light_sensor --values 1 0Run HMM analysis over a CSV timeline:
python main.py hmm living_room --timeline sensor_data.csv --slot 15The system uses the following main tables:
area_occupancy: Area configurations and thresholdsentities: Sensor entity definitionsarea_entity_config: Sensor configurations per area with learned likelihoodsarea_time_priors: Time-based occupancy priors (day of week + time slot)state_intervals: Historical sensor state data
| Command | Description |
|---|---|
init |
Initialize database schema |
import |
Import data from SQL file |
priors |
Compute time-based occupancy priors |
likelihoods |
Compute sensor likelihoods |
predict |
Make real-time occupancy prediction |
hmm |
Run HMM analysis over timeline |
learn-all |
Train models for all areas |
reset |
Reset all learned parameters |
help |
Show detailed help |
# 1. Set up database and import data
python main.py init
python main.py import --file export.sql
# 2. Train models for all areas
python main.py learn-all --slot 30
# 3. Make a prediction
python main.py predict bedroom --entities motion_sensor door_sensor --values 1 0
# Output: Naïve Bayes P(occupied) = 0.847
# 4. Compute area prior
python main.py area-prior bedroom
# Output: Updated area prior for bedroom: 0.234The --slot parameter controls the time resolution for learning patterns:
60minutes: Hourly patterns (24 slots per day)30minutes: Half-hourly patterns (48 slots per day)15minutes: Quarter-hourly patterns (96 slots per day)
For HMM timeline analysis, use CSV format:
timestamp,entity_id,value
2024-01-01T10:00:00,motion_sensor,1
2024-01-01T10:00:00,light_sensor,0
2024-01-01T10:15:00,motion_sensor,0[Add your license here]