go_iforest

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 3 Imported by: 4

README

go-iforest

godocs.io

Golang implementation of Isolation Forest.

Example Usage

package main

import (
	go_iforest "github.com/codegaudi/go-iforest"
	"log"
	"math/rand"
	"time"
)


func main() {

	// load input data into two-dimensional slice
	var inputData [][]float64
	
	// seed the random generator before generating an isolation forest
	rand.Seed(time.Now().UnixNano())

	// generate an isolation forest with 10'000 trees and a sub-sampling size of 256
	f, err := go_iforest.NewIForest(inputData, 10000, 256)
	if err != nil {
		log.Fatal(err)
	}

	// calculate an anomaly score for a sample
	score := f.CalculateAnomalyScore(inputData[0])
}

License

MIT

Documentation

Overview

Package go_iforest is an implementation of isolation forests as defined in https://cs.nju.edu.cn/zhouzh/zhouzh.files/publication/icdm08b.pdf.

Index

Constants

View Source
const EulersConstant = 0.5772156649

Variables

View Source
var (
	ErrTooLargeSubSamplingSize = errors.New("specified sub-sampling size is larger than amount of provided samples")
	ErrNoSamplesProvided       = errors.New("no samples provided")
)

Functions

func PathLength

func PathLength(x []float64, T *Node, e int) float64

PathLength derives the path length for an instance x using a Tree T e is the current path length (0 on the first call to this method)

Types

type IForest

type IForest struct {
	Trees           []*ITree
	SubSamplingSize int
}

func NewIForest

func NewIForest(X [][]float64, trees int, subSamplingSize int) (*IForest, error)

NewIForest creates a new IForest and trains it on the provided data X make sure to call rand.Seed() before calling this function to ensure that a sufficiently random sub-sample is chosen.

func (*IForest) CalculateAnomalyScore

func (f *IForest) CalculateAnomalyScore(x []float64) float64

CalculateAnomalyScore calculates an anomaly score based for a sample x

type ITree

type ITree struct {
	// contains filtered or unexported fields
}

func NewITree

func NewITree(X [][]float64) *ITree

type Node

type Node struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL