This repository contains the source code, data preprocessing pipelines, and experiments developed as part of a bachelor's thesis focused on anomaly detection using machine learning methods under conditions of limited anomalous data.
The objective of this thesis was to analyze and compare the performance of three anomaly detection methods:
- Isolation Forest
- One-Class Support Vector Machine (One-Class SVM)
- Autoencoder
The experiments were conducted on three datasets representing different types of data:
- NAB (Numenta Anomaly Benchmark) – industrial time-series data,
- ECG5000 – biomedical ECG signals,
- NASA Battery Dataset – lithium-ion battery degradation data.
In all experiments, the models were trained exclusively on normal data. Anomalous samples were used only during the testing and evaluation stages.
A detailed theoretical background, proposed methodology, dataset preprocessing, implementation of the anomaly detection methods, and analysis of the experimental results are presented in the bachelor's thesis.
Bachelor's Thesis (PDF):
- Anaconda – Python distribution for data analysis and machine learning.
- Jupyter Notebook – Interactive environment for running experimental notebooks.
- Python – Programming language used for the implementation.
- pandas – Data manipulation and analysis.
- NumPy – Numerical computing.
- scikit-learn – Implementation of the Isolation Forest and One-Class SVM algorithms.
- TensorFlow – Neural network implementation.
- Keras – Building and training Autoencoder models.
- Matplotlib – Data visualization.
BachelorThesis-Anomaly-Detection-EN
│
├── notebooks/
│ │
│ ├── Autoencoder_ECG5000.ipynb
│ ├── Autoencoder_NAB.ipynb
│ ├── Autoencoder_NASA.ipynb
│ │
│ ├── Isolation_Forest_ECG5000.ipynb
│ ├── Isolation_Forest_NAB.ipynb
│ ├── Isolation_Forest_NASA.ipynb
│ │
│ ├── OneClass_SVM_ECG5000.ipynb
│ ├── OneClass_SVM_NAB.ipynb
│ ├── OneClass_SVM_NASA.ipynb
│ │
│ ├── nab.ipynb
│ └── nasa_battery.ipynb
│
├── data_raw/
│ │
│ ├── ECG5000/
│ ├── nab/
│ └── nasa_battery/
│
├── data_processed/
│ │
│ ├── NAB/
│ └── NASA/
│
├── docs/
│ │
│ └── BP_RK_IevgeniiaIevgrafova.pdf
│
└── README.md
The NASA Battery Dataset contains measurements collected from lithium-ion batteries throughout their operational lifetime.
During preprocessing, only discharge cycles were used. The original measurements were transformed into derived features describing the battery's condition during the degradation process.
- Voltage_measured
- Current_measured
- Temperature_measured
- Current_load
- Voltage_load
- Time
In addition to the original variables, the following derived features were created:
- first-order differences,
- rolling means,
- rolling standard deviations,
- rolling minimum values,
- rolling maximum values.
Anomalous cycles were identified based on battery capacity. Cycles with a capacity below the 20th percentile were labeled as anomalous.
The NAB dataset consists of an industrial time series containing temperature measurements collected from industrial equipment.
During preprocessing, the following derived features were generated:
- signal value,
- first-order difference,
- rolling mean,
- rolling standard deviation,
- rolling minimum,
- rolling maximum.
Anomalies were labeled according to the official anomaly windows defined by the NAB benchmark.
The ECG5000 dataset contains electrocardiogram (ECG) recordings, where each sample consists of 140 points representing a single heartbeat.
- Class 1 represents normal ECG recordings.
- Classes 2–5 were treated as anomalous.
During the experiments:
- models were trained exclusively on normal samples,
- anomalous samples were used only during testing,
- Z-score normalization based on the training data was applied.
This notebook performs:
- loading the NAB dataset,
- labeling anomaly windows,
- generating derived features,
- saving the processed dataset for the experiments.
This notebook performs:
- loading the NASA Battery Dataset,
- selecting discharge cycles,
- generating derived features,
- labeling anomalous cycles,
- saving the processed dataset.
The resulting file:
data_processed/nasa_battery_features_rolling.csv
is subsequently used by all experiments conducted on the NASA Battery Dataset.
Isolation_Forest_ECG5000.ipynbIsolation_Forest_NAB.ipynbIsolation_Forest_NASA.ipynb
The implementation includes model training, anomaly score computation, threshold application, and performance evaluation.
OneClass_SVM_ECG5000.ipynbOneClass_SVM_NAB.ipynbOneClass_SVM_NASA.ipynb
The implementation uses the RBF kernel and performs classification based on the decision score.
Autoencoder_ECG5000.ipynbAutoencoder_NAB.ipynbAutoencoder_NASA.ipynb
The Autoencoder is implemented using TensorFlow/Keras. Anomaly detection is based on the reconstruction error.
The following principles were applied across all experiments:
- models were trained exclusively on normal data,
- anomalous samples were used only during testing,
- performance was evaluated using Accuracy, Precision, Recall, and F1-score,
- confusion matrices were generated for each experiment,
- score histograms were used to visualize the distribution of anomaly scores.
The following default parameters were used in all Isolation Forest experiments:
n_estimators = 300contamination = "auto"random_state = 42for the ECG5000 and NAB datasetsseeds = [42, 43, 44, 45, 46]for the NASA Battery dataset
The following default parameters were used in all One-Class SVM experiments:
kernel = "rbf"nu = 0.05gamma = "auto"seeds = [42, 43, 44, 45, 46]for the NASA Battery dataset
The same base architecture was used in all Autoencoder experiments:
hidden_dim = 32encoding_dim = 8batch_size = 64learning_rate = 0.001epochs = 100patience = 10validation_split = 0.2optimizer = Adamloss function = Mean Squared Error (MSE)seeds = [42, 43, 44, 45, 46]for the NASA Battery dataset
The following table summarizes the best-performing configurations and the corresponding results achieved by each anomaly detection method.
| Method | Dataset | Threshold | Train Fraction | Accuracy (%) | Precision (%) | Recall (%) | F1-score (%) |
|---|---|---|---|---|---|---|---|
| iForest | NASA Battery | 0.065 | 0.90 | 85.28 | 85.58 | 94.09 | 89.54 |
| iForest | ECG5000 | 0.0 | – | 93.42 | 90.09 | 94.61 | 92.29 |
| iForest | NAB | -0.15 | 0.80 | 99.29 | 96.68 | 97.71 | 97.19 |
| OCSVM | NASA Battery | 20.0 | 0.90 | 78.86 | 87.85 | 79.42 | 83.39 |
| OCSVM | ECG5000 | -0.3 | – | 96.87 | 94.59 | 98.08 | 96.30 |
| OCSVM | NAB | -5.0 | 0.80 | 97.59 | 90.89 | 89.77 | 90.33 |
| AE | NASA Battery | 0.002 | 0.90 | 86.91 | 86.63 | 95.75 | 90.84 |
| AE | ECG5000 | 0.7 | – | 94.53 | 94.43 | 92.31 | 93.36 |
| AE | NAB | 0.00006 | 0.80 | 95.85 | 80.22 | 88.71 | 84.25 |
The following example presents the results obtained using the Autoencoder on the ECG5000 dataset with the following decision threshold:
Threshold = 0.7
The reconstruction error histogram demonstrates the separation of normal and anomalous samples using the selected decision threshold.
The ECG reconstruction plots illustrate the Autoencoder's ability to reconstruct normal heartbeats more accurately, while anomalous samples exhibit significantly higher reconstruction errors.
The project was developed using:
- Python 3.x
- Jupyter Notebook
- Anaconda
Required libraries:
- pandas
- numpy
- scikit-learn
- tensorflow
- keras
- matplotlib
- seaborn
- scipy
- pathlib
The NASA Battery Dataset is not included in this repository due to its size.
Before running the experiments:
- Download the NASA Battery Dataset.
- Copy its contents into:
data_raw/nasa_battery/
- Run:
notebooks/nasa_battery.ipynb
The notebook will automatically generate:
data_processed/nasa_battery_features_rolling.csv
After that, you can run the experimental notebooks for the NASA Battery dataset.
The data_processed directory must exist.
If the nasa_battery_features_rolling.csv file is missing, it will be generated automatically by the notebook.
The NAB dataset is already included in the repository.
To generate the processed dataset, run:
notebooks/nab.ipynb
After preprocessing, the experimental notebooks can be executed.
The ECG5000 dataset is already included in the repository.
No separate preprocessing step is required.
The experimental notebooks load the dataset directly.
Ievgeniia Ievgrafova
Bachelor's Thesis
Faculty of Electrical Engineering and Information Technology
Slovak University of Technology in Bratislava
2026