This project implements a KNN algorithm from scratch in C++ to detect and classify engine failures based on sensor data. It includes data preprocessing, normalisation, and a distance-weighted voting mechanism to evaluate predictive accuracy.
The data used for this project is the Engine Failure Detection Dataset available on Kaggle.
Important Data Preprocessing Note: Before running the program, the timestamp column was manually removed from the downloaded CSV file.
- Custom KNN Implementation: No external machine learning libraries are used. The algorithm calculates the Euclidean distance between data points.
- Distance-Weighted Voting: Rather than a simple majority vote, the algorithm gives closer neighbours a higher influence on the final prediction, improving accuracy.
- Automatic Normalisation: Features with vastly different scales (like RPM in the thousands vs. Fuel Efficiency in the tens) are automatically normalised so they contribute equally to the distance calculation.
- Configurable Hyperparameters: Easily adjust the
kvalue, training/testing split ratio, and seed for shuffling directly at the top of the source file.
Ensure your dataset is placed correctly within the data folder before running the code so that the relative file paths resolve correctly:
├── CMakeLists.txt # CMake configuration file
├── CMakePresets.json # CMake build and configuration presets
├── EngineFailureKNN.cpp # The main C++ source code for the KNN algorithm
├── LICENSE.txt # License information
├── README.md # Project documentation
├── data/ # Directory containing the dataset
│ └── engine_failure_dataset.csv
└── out/ # Build output directory (generated by CMake/your IDE)
You can tweak the behavior of the model by modifying the global variables at the top of the main.cpp file:
DATA_FILE: Path to your dataset (default is"data/engine_failure_dataset.csv").Train_Split: The ratio of data used for training vs testing (default0.7for a 70/30 split).k: The number of nearest neighbors to check.
This project uses CMake for its build system.
- Open a terminal or command prompt in the project's root directory.
- Create a build directory and navigate into it:
mkdir build
cd build
- Generate the build files using CMake:
cmake ..
- Compile the project:
cmake --build .
- Run the compiled executable:
# On Linux/macOS
./EngineFailureKNN
# On Windows
Debug\EngineFailureKNN.exe
This project was purely to learn C++ syntax so the focus wasn't squeezing performance. It averages 70-75% accuracy.