- Notifications
You must be signed in to change notification settings - Fork8
Isolation forest implementation in Go
License
e-XpertSolutions/go-iforest
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Go implementation of Isolation Forest algorithm.
Isolation Forest is an unsupervised learning algorithm that is able to detect anomalies (data patterns that differ from normal instances). Detection is performed by recursive data partitioning, which can be represented by a tree structure. At each iteration data is splitted using randomly chosen feature and its value (random number between maximum and minimum value of chosen feature). Due to the fact that anomalies are rare and different from other instances, smaller number of partitions is needed to isolate them. This is equivalent to the path length in created tree. Shorter path means that given instance can be an anomaly. To improve accuracy the ensemble of such trees is created and result is averaged over all trees.
To get more information about algorithm, please refer to this paper:IFOREST.
Stable release can be downloaded by issuing the following command:
go get github.com/e-XpertSolutions/go-iforest/v2
This example shows how to use the Isolation Forest. You will need to load the data, initialize iforest with proper parameters and use two functions: Train(), Test() to create the model. First one is used to build the trees, second one to find proper "anomaly threshold" and detect anomalies in given data. After that you can pass new instances to Predict() which result in labeling them as normal "0" or anomaly "1".It is possible to use parallel versions of testing and detecting functions - they use multiple go routines to speed up computations.Created models can be saved and read from the files using Save() and Load() methods.
package mainimport("fmt""github.com/e-XpertSolutions/go-iforest/iforest")funcmain(){// input data must be loaded into two dimensional array of the type float64// please note: loadData() is some custom function - not included in the// libraryvarinputData [][]float64inputData=loadData("filename")// input parameterstreesNumber:=100subsampleSize:=256outliersRatio:=0.01routinesNumber:=10//model initializationforest:=iforest.NewForest(treesNumber,subsampleSize,outliers)//training stage - creating treesforest.Train(inputData)//testing stage - finding anomalies//Test or TestParaller can be used, concurrent version needs one additional// parameterforest.Test(inputData)forest.TestParallel(inputData,routinesNumber)//after testing it is possible to access anomaly scores, anomaly bound// and labels for the input datasetthreshold:=forest.AnomalyBoundanomalyScores:=forest.AnomalyScoreslabelsTest:=forest.Labels//to get information about new instances pass them to the Predict function// to speed up computation use concurrent version of PredictvarnewData [][]float64newData=loadData("someNewInstances")labels,scores:=forest.Predict(newData)}
Contributions are greatly appreciated. The project follows the typicalGitHub pull request modelfor contribution.
The sources are release under a BSD 3-Clause License. The full terms of thatlicense can be found inLICENSE
file of this repository.
About
Isolation forest implementation in Go
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.