com.linkedin.isolation-forest

A distributed Spark/Scala implementation of the isolation forest algorithm for unsupervised outlier detection, featuring support for scalable training and ONNX export for easy cross-platform inference.

https://github.com/linkedin/isolation-forest

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.5%) to scientific vocabulary

Keywords

anomaly-detection isolation-forest linkedin machine-learning onnx outlier-detection scala spark unsupervised-learning

Keywords from Contributors

data-profilers datacleaner pipeline-testing mesh sequences interactive data-engineering hacking network-simulation
Last synced: 6 months ago · JSON representation ·

Repository

A distributed Spark/Scala implementation of the isolation forest algorithm for unsupervised outlier detection, featuring support for scalable training and ONNX export for easy cross-platform inference.

Basic Info
  • Host: GitHub
  • Owner: linkedin
  • License: other
  • Language: Scala
  • Default Branch: master
  • Homepage:
  • Size: 2.06 MB
Statistics
  • Stars: 248
  • Watchers: 14
  • Forks: 52
  • Open Issues: 3
  • Releases: 39
Topics
anomaly-detection isolation-forest linkedin machine-learning onnx outlier-detection scala spark unsupervised-learning
Created over 6 years ago · Last pushed 8 months ago
Metadata Files
Readme Contributing License Citation

README.md

Logo

isolation-forest

Build Status Release License

Table of contents

Introduction

This is a distributed Scala/Spark implementation of the Isolation Forest unsupervised outlier detection algorithm. It features support for ONNX export for easy cross-platform inference. This library was created by James Verbus from the LinkedIn Anti-Abuse AI team.

Features

  • Distributed training and scoring: The isolation-forest module supports distributed training and scoring in Scala using Spark data structures. It inherits from the Estimator and Model classes in Spark's ML library in order to take advantage of machinery such as Pipelines. Model persistence on HDFS is supported.
  • Broad portability via ONNX: The isolation-forest-onnx module provides Python-based converter to convert a trained model to ONNX format for broad portability across platforms and languages. ONNX is an open format built to represent machine learning models.

Getting started

Building the library

To build using the default of Scala 2.13.14 and Spark 3.5.1, run the following:

bash ./gradlew build This will produce a jar file in the ./isolation-forest/build/libs/ directory.

If you want to use the library with arbitrary Spark and Scala versions, you can specify this when running the build command.

bash ./gradlew build -PsparkVersion=3.5.1 -PscalaVersion=2.13.14

To force a rebuild of the library, you can use: bash ./gradlew clean build --no-build-cache

To just run the tests: bash ./gradlew test

Add an isolation-forest dependency to your project

Please check Maven Central for the latest artifact versions.

Gradle example

The artifacts are available in Maven Central, so you can specify the Maven Central repository in the top-level build.gradle file.

repositories { mavenCentral() }

Add the isolation-forest dependency to the module-level build.gradle file. Here is an example for a recent spark scala version combination.

dependencies { compile 'com.linkedin.isolation-forest:isolation-forest_3.5.1_2.13:3.2.3' }

Maven example

If you are using the Maven Central repository, declare the isolation-forest dependency in your project's pom.xml file. Here is an example for a recent Spark/Scala version combination.

<dependency> <groupId>com.linkedin.isolation-forest</groupId> <artifactId>isolation-forest_3.5.1_2.13</artifactId> <version>3.2.3</version> </dependency>

Usage examples

Model parameters

| Parameter | Default Value | Description | |--------------------|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | numEstimators | 100 | The number of trees in the ensemble. | | maxSamples | 256 | The number of samples used to train each tree. If this value is between 0.0 and 1.0, then it is treated as a fraction. If it is >1.0, then it is treated as a count. | | contamination | 0.0 | The fraction of outliers in the training data set. If this is set to 0.0, it speeds up the training and all predicted labels will be false. The model and outlier scores are otherwise unaffected by this parameter. | | contaminationError | 0.0 | The error allowed when calculating the threshold required to achieve the specified contamination fraction. The default is 0.0, which forces an exact calculation of the threshold. The exact calculation is slow and can fail for large datasets. If there are issues with the exact calculation, a good choice for this parameter is often 1% of the specified contamination value. | | maxFeatures | 1.0 | The number of features used to train each tree. If this value is between 0.0 and 1.0, then it is treated as a fraction. If it is >1.0, then it is treated as a count. | | bootstrap | false | If true, draw sample for each tree with replacement. If false, do not sample with replacement. | | randomSeed | 1 | The seed used for the random number generator. | | featuresCol | "features" | The feature vector. This column must exist in the input DataFrame for training and scoring. | | predictionCol | "predictedLabel" | The predicted label. This column is appended to the input DataFrame upon scoring. | | scoreCol | "outlierScore" | The outlier score. This column is appended to the input DataFrame upon scoring. |

Training and scoring

Here is an example demonstrating how to import the library, create a new IsolationForest instance, set the model hyperparameters, train the model, and then score the training data. data is a Spark DataFrame with a column named features that contains a org.apache.spark.ml.linalg.Vector of the attributes to use for training. In this example, the DataFrame data also has a labels column; it is not used in the training process, but could be useful for model evaluation.

```scala import com.linkedin.relevance.isolationforest._ import org.apache.spark.ml.feature.VectorAssembler

/** * Load and prepare data */

// Dataset from http://odds.cs.stonybrook.edu/shuttle-dataset/ val rawData = spark.read .format("csv") .option("comment", "#") .option("header", "false") .option("inferSchema", "true") .load("isolation-forest/src/test/resources/shuttle.csv")

val cols = rawData.columns val labelCol = cols.last

val assembler = new VectorAssembler() .setInputCols(cols.slice(0, cols.length - 1)) .setOutputCol("features") val data = assembler .transform(rawData) .select(col("features"), col(labelCol).as("label"))

// scala> data.printSchema // root // |-- features: vector (nullable = true) // |-- label: integer (nullable = true)

/** * Train the model */

val contamination = 0.1 val isolationForest = new IsolationForest() .setNumEstimators(100) .setBootstrap(false) .setMaxSamples(256) .setMaxFeatures(1.0) .setFeaturesCol("features") .setPredictionCol("predictedLabel") .setScoreCol("outlierScore") .setContamination(contamination) .setContaminationError(0.01 * contamination) .setRandomSeed(1)

val isolationForestModel = isolationForest.fit(data)

/** * Score the training data */

val dataWithScores = isolationForestModel.transform(data)

// scala> dataWithScores.printSchema // root // |-- features: vector (nullable = true) // |-- label: integer (nullable = true) // |-- outlierScore: double (nullable = false) // |-- predictedLabel: double (nullable = false) ```

The output DataFrame, dataWithScores, is identical to the input data DataFrame but has two additional result columns appended with their names set via model parameters; in this case, these are named predictedLabel and outlierScore.

Saving and loading a trained model

Once you've trained an isolationForestModel instance as per the instructions above, you can use the following commands to save the model to HDFS and reload it as needed.

```scala val path = "/user/testuser/isolationForestWriteTest"

/** * Persist the trained model on disk */

// You can ensure you don't overwrite an existing model by removing .overwrite from this command isolationForestModel.write.overwrite.save(path)

/** * Load the saved model from disk */

val isolationForestModel2 = IsolationForestModel.load(path) ```

ONNX conversion for portable inference

Converting a trained model to ONNX

The artifacts associated with the isolation-forest-onnx module are available in PyPI.

The ONNX converter can be installed using pip. It is recommended to use the same version of the converter as the version of the isolation-forest library used to train the model.

bash pip install isolation-forest-onnx==3.2.7

You can then import and use the converter in Python.

```python import os from isolationforestonnx.isolationforestconverter import IsolationForestConverter

This is the same path used in the previous example showing how to save the model in Scala above.

path = '/user/testuser/isolationForestWriteTest'

Get model data path

datadirpath = path + '/data' avromodelfile = os.listdir(datadirpath) modelfilepath = datadirpath + '/' + avromodelfile[0]

Get model metadata file path

metadatadirpath = path + '/metadata' metadatafile = os.listdir(path + '/metadata/') metadatafilepath = metadatadirpath + '/' + metadatafile[0]

Convert the model to ONNX format (this will return the ONNX model in memory)

converter = IsolationForestConverter(modelfilepath, metadatafilepath) onnx_model = converter.convert()

Convert and save the model in ONNX format (this will save the ONNX model to disk)

onnxmodelpath = '/user/testuser/isolationForestWriteTest.onnx' converter.convertandsave(onnxmodelpath) ```

Using the ONNX model for inference (example in Python)

```python import numpy as np import onnx from onnxruntime import InferenceSession

onnx_model_path the same path used above in the convert and save operation

onnxmodelpath = '/user/testuser/isolationForestWriteTest.onnx' dataset_path = 'isolation-forest-onnx/test/resources/shuttle.csv'

Load data

inputdata = np.loadtxt(datasetpath, delimiter=',') numfeatures = inputdata.shape[1] - 1 lastcolindex = numfeatures print(f'Number of features for {datasetname}: {num_features}')

The last column is the label column

inputdict = {'features': np.delete(inputdata, lastcolindex, 1).astype(dtype=np.float32)} actuallabels = inputdata[:, lastcolindex]

Load the ONNX model from local disk and do inference

onx = onnx.load(onnxmodelpath) sess = InferenceSession(onx.SerializeToString()) res = sess.run(None, input_dict)

Print scores

actualoutlierscores = res[0] print('ONNX Converter outlier scores:') print(np.transpose(actualoutlierscores[:numexamplesto_print])[0]) ```

Performance and benchmarks

The original 2008 "Isolation forest" paper by Liu et al. published the AUROC results obtained by applying the algorithm to 12 benchmark outlier detection datasets. We applied our implementation of the isolation forest algorithm to the same 12 datasets using the same model parameter values used in the original paper. We used 10 trials per dataset each with a unique random seed and averaged the result. The quoted uncertainty is the one-sigma error on the mean.

| Dataset | Expected mean AUROC (from Liu et al.) | Observed mean AUROC (from this implementation) | |------------------------------------------------------------------------------------|---------------------------------------|------------------------------------------------| | Http (KDDCUP99) | 1.00 | 0.99973 ± 0.00007 | | ForestCover | 0.88 | 0.903 ± 0.005 | | Mulcross | 0.97 | 0.9926 ± 0.0006 | | Smtp (KDDCUP99) | 0.88 | 0.907 ± 0.001 | | Shuttle | 1.00 | 0.9974 ± 0.0014 | | Mammography | 0.86 | 0.8636 ± 0.0015 | | Annthyroid | 0.82 | 0.815 ± 0.006 | | Satellite | 0.71 | 0.709 ± 0.004 | | Pima | 0.67 | 0.651 ± 0.003 | | Breastw | 0.99 | 0.9862 ± 0.0003 | | Arrhythmia | 0.80 | 0.804 ± 0.002 | | Ionosphere | 0.85 | 0.8481 ± 0.0002 |

Our implementation provides AUROC values that are in very good agreement with the results in the original Liu et al. publication. There are a few very small discrepancies that are likely due to the limited precision of the AUROC values reported in Liu et al.

Copyright and license

Copyright 2019 LinkedIn Corporation All Rights Reserved.

Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information.

Contributing

If you would like to contribute to this project, please review the instructions here.

References

  • F. T. Liu, K. M. Ting, and Z.-H. Zhou, Isolation forest, in 2008 Eighth IEEE International Conference on Data Mining, 2008, pp. 413422.
  • F. T. Liu, K. M. Ting, and Z.-H. Zhou, Isolation-based anomaly detection, ACM Transactions on Knowledge Discovery from Data (TKDD), vol. 6, no. 1, p. 3, 2012.
  • Shebuti Rayana (2016). ODDS Library [http://odds.cs.stonybrook.edu]. Stony Brook, NY: Stony Brook University, Department of Computer Science.

Owner

  • Name: LinkedIn
  • Login: linkedin
  • Kind: organization
  • Email: oss@linkedin.com
  • Location: Sunnyvale, CA, USA

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
- family-names: "Verbus"
  given-names: "James"
  orcid: "https://orcid.org/0000-0002-5812-022X"
title: "isolation-forest"
version: 4.0.*
date-released: 2019-08-13
url: "https://github.com/linkedin/isolation-forest"

GitHub Events

Total
  • Create event: 22
  • Issues event: 1
  • Release event: 9
  • Watch event: 18
  • Delete event: 15
  • Issue comment event: 6
  • Push event: 31
  • Pull request review comment event: 2
  • Pull request review event: 5
  • Pull request event: 22
  • Fork event: 3
Last Year
  • Create event: 22
  • Issues event: 1
  • Release event: 9
  • Watch event: 18
  • Delete event: 15
  • Issue comment event: 6
  • Push event: 31
  • Pull request review comment event: 2
  • Pull request review event: 5
  • Pull request event: 22
  • Fork event: 3

Committers

Last synced: 7 months ago

All Time
  • Total Commits: 88
  • Total Committers: 5
  • Avg Commits per committer: 17.6
  • Development Distribution Score (DDS): 0.182
Past Year
  • Commits: 28
  • Committers: 3
  • Avg Commits per committer: 9.333
  • Development Distribution Score (DDS): 0.143
Top Committers
Name Email Commits
James Verbus j****s@l****m 72
shipkit-org s****g@g****m 11
dependabot[bot] 4****] 3
Markus Cozowicz m****o@m****m 1
Marcus Rosti r****s@g****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 40
  • Total pull requests: 84
  • Average time to close issues: about 1 month
  • Average time to close pull requests: about 1 month
  • Total issue authors: 19
  • Total pull request authors: 5
  • Average comments per issue: 5.13
  • Average comments per pull request: 0.49
  • Merged pull requests: 68
  • Bot issues: 0
  • Bot pull requests: 4
Past Year
  • Issues: 1
  • Pull requests: 16
  • Average time to close issues: N/A
  • Average time to close pull requests: 1 day
  • Issue authors: 1
  • Pull request authors: 3
  • Average comments per issue: 2.0
  • Average comments per pull request: 0.25
  • Merged pull requests: 15
  • Bot issues: 0
  • Bot pull requests: 4
Top Authors
Issue Authors
  • Marcus-Rosti (2)
  • TAsUjxnMIL (1)
  • juzheng-zhang-1989 (1)
  • ikoiko (1)
  • ruizcrp (1)
  • jverbus (1)
  • vishalovercome (1)
  • DnyaneshPatil23 (1)
  • hoffrocket (1)
  • nightscape (1)
  • sridhar (1)
  • bgreenwell (1)
  • cchantep (1)
  • bhushanbalki (1)
  • shazriz (1)
Pull Request Authors
  • jverbus (54)
  • dependabot[bot] (6)
  • eisber (3)
  • Marcus-Rosti (2)
  • cchantep (1)
Top Labels
Issue Labels
question (5) enhancement (5) bug (1)
Pull Request Labels
enhancement (8) dependencies (6) python (2) bug (1) codex (1)

Packages

  • Total packages: 18
  • Total downloads:
    • pypi 166 last-month
  • Total dependent packages: 2
    (may contain duplicates)
  • Total dependent repositories: 3
    (may contain duplicates)
  • Total versions: 306
  • Total maintainers: 1
proxy.golang.org: github.com/linkedin/isolation-forest
  • Versions: 41
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 6.4%
Average: 6.6%
Dependent repos count: 6.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.0.0_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 12
  • Dependent Packages: 1
  • Dependent Repositories: 2
Rankings
Dependent repos count: 16.1%
Stargazers count: 22.5%
Average: 23.9%
Forks count: 24.3%
Dependent packages count: 33.0%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.2.0_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 4
  • Dependent Packages: 1
  • Dependent Repositories: 0
Rankings
Stargazers count: 16.3%
Forks count: 16.9%
Average: 24.3%
Dependent repos count: 32.0%
Dependent packages count: 32.0%
Last synced: 6 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_2.4.3_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 36
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 16.3%
Forks count: 16.9%
Average: 28.5%
Dependent repos count: 32.0%
Dependent packages count: 48.9%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_2.3.0_2.11

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 13
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 16.3%
Forks count: 16.9%
Average: 28.5%
Dependent repos count: 32.0%
Dependent packages count: 48.9%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.1.1_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 9
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 16.3%
Forks count: 16.9%
Average: 28.5%
Dependent repos count: 32.0%
Dependent packages count: 48.9%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.2.0_2.13

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 16.3%
Forks count: 17.0%
Average: 28.6%
Dependent repos count: 32.0%
Dependent packages count: 48.9%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_2.4.3_2.11

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 15
  • Dependent Packages: 0
  • Dependent Repositories: 1
Rankings
Dependent repos count: 20.8%
Stargazers count: 22.5%
Forks count: 24.4%
Average: 29.4%
Dependent packages count: 50.1%
Last synced: 7 months ago
pypi.org: isolation-forest-onnx

A converter for the LinkedIn Spark/Scala isolation forest model format to ONNX format.

  • Versions: 13
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 166 Last month
Rankings
Dependent packages count: 10.4%
Average: 34.4%
Dependent repos count: 58.5%
Maintainers (1)
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.3.4_2.13

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 20
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.3.4_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 21
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.4.3_2.13

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 21
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.1.3_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 21
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.2.4_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 24
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.5.5_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.5.5_2.13

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 6
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.4.3_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 21
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago
repo1.maven.org: com.linkedin.isolation-forest:isolation-forest_3.0.3_2.12

A Spark/Scala implementation of the isolation forest unsupervised outlier detection algorithm.

  • Versions: 21
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 33.4%
Average: 40.6%
Dependent packages count: 47.8%
Last synced: 7 months ago

Dependencies

isolation-forest/build.gradle maven
  • com.chuusai:shapeless_ * compile
  • com.databricks:spark-avro_ * compile
  • org.apache.spark:spark-avro_ * compile
  • org.apache.spark:spark-core_ * compile
  • org.apache.spark:spark-mllib_ * compile
  • org.apache.spark:spark-sql_ * compile
  • org.scalatest:scalatest_ * compile
  • org.testng:testng 6.8.8 compile
.github/workflows/ci.yml actions
  • actions/checkout v2 composite
  • actions/setup-java v1 composite
build.gradle maven
isolation-forest-onnx/build.gradle maven
isolation-forest-onnx/pyproject.toml pypi
isolation-forest-onnx/requirements-dev.txt pypi
  • coverage ==7.6.1 development
  • flake8 ==7.1.1 development
  • mypy ==1.11 development
  • pandas ==2.2.3 development
  • pytest ==8.3.2 development
  • setuptools ==74.0.0 development
  • twine ==5.1.1 development
  • wheel ==0.38.1 development
isolation-forest-onnx/requirements.txt pypi
  • avro-python3 ==1.9.1
  • numpy ==1.26.4
  • onnx ==1.17.0
  • onnxruntime ==1.18.0
  • protobuf ==5.26.1
isolation-forest-onnx/setup.py pypi