Random Cut Forest C/C++
Random Cut Forest (RCF) anomaly detection for C/C++
Installation
Download the latest version:
You can also install it with Homebrew:
brew install ankane/brew/librcf
Getting Started
Include the header
#include "rcf.h"
Create a forest with 3 dimensions
rcf_forest *forest = rcf_create(3);
Set parameters
rcf_set_param(forest, "number_of_trees", "100");
Score a point
float point[] = {1.0, 2.0, 3.0};
double score = rcf_score(forest, point);
Update with a point
rcf_update(forest, point);
Free the forest
rcf_free(forest);
Example
#include <stdio.h>
#include <stdlib.h>
#include "rcf.h"
float randf() {
return rand() / (float) RAND_MAX;
}
int main() {
rcf_forest *forest = rcf_create(3);
rcf_set_param(forest, "number_of_trees", "100");
for (int i = 0; i < 200; i++) {
float point[] = {randf(), randf(), randf()};
// make the second to last point an anomaly
if (i == 198) {
point[1] = 2;
}
double score = rcf_score(forest, point);
printf("point = %d, score = %f\n", i, score);
rcf_update(forest, point);
}
rcf_free(forest);
return 0;
}
Parameters
Name | Description | Default Value |
---|---|---|
shingle_size |
Shingle size to use | 1 |
sample_size |
Points to keep in sample for each tree | 256 |
number_of_trees |
Number of trees to use in the forest | 100 |
random_seed |
Random seed to use | 42 |
parallel |
Enable parallel execution | false |
Parameter values should always be passed as strings.
rcf_set_param(forest, "sample_size", "256");
rcf_set_param(forest, "parallel", "true");
rcf_set_param
returns zero if successful and nonzero if the name or value is invalid or if it’s called after rcf_score
or rcf_update
.
References
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/ankane/librcf.git
cd librcf
cargo build
cargo test
To generate headers:
cbindgen > include/rcf.h