Skip to content
/ librcf Public

Random Cut Forest anomaly detection for C/C++

License

Notifications You must be signed in to change notification settings

ankane/librcf

Repository files navigation

Random Cut Forest C/C++

Random Cut Forest (RCF) anomaly detection for C/C++

🌲 Also available for Ruby and PHP, and as a CLI

Build Status

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:

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

About

Random Cut Forest anomaly detection for C/C++

Resources

License

Stars

Watchers

Forks

Packages

No packages published