Skip to main content

isolation-forest-onnx

A converter for the LinkedIn Spark/Scala isolation forest model format to ONNX format for broad portability across platforms and languages.

Note: ONNX conversion is supported for the standard IsolationForestModel only. The ExtendedIsolationForestModel uses hyperplane-based splits that are not compatible with the axis-aligned tree ensemble representation used by the ONNX converter.

Installation

pip install isolation-forest-onnx

It is recommended to use the same version of the converter as the version of the isolation-forest library used to train the model.

Converting a trained model to ONNX

import os
from isolationforestonnx.isolation_forest_converter import IsolationForestConverter

# Path where the trained IsolationForestModel was saved in Scala
path = '/user/testuser/isolationForestWriteTest'

# Get model data path
data_dir_path = path + '/data'
avro_model_file = os.listdir(data_dir_path)
model_file_path = data_dir_path + '/' + avro_model_file[0]

# Get model metadata file path
metadata_dir_path = path + '/metadata'
metadata_file = os.listdir(metadata_dir_path)
metadata_file_path = metadata_dir_path + '/' + metadata_file[0]

# Convert the model to ONNX format (returns the ONNX model in memory)
converter = IsolationForestConverter(model_file_path, metadata_file_path)
onnx_model = converter.convert()

# Convert and save the model in ONNX format
onnx_model_path = '/user/testuser/isolationForestWriteTest.onnx'
converter.convert_and_save(onnx_model_path)

Using the ONNX model for inference

import numpy as np
import onnx
from onnxruntime import InferenceSession

onnx_model_path = '/user/testuser/isolationForestWriteTest.onnx'
dataset_path = 'shuttle.csv'

# Load data
input_data = np.loadtxt(dataset_path, delimiter=',')
num_features = input_data.shape[1] - 1
last_col_index = num_features

# The last column is the label column
input_dict = {'features': np.delete(input_data, last_col_index, 1).astype(dtype=np.float32)}

# Load the ONNX model and run inference
onx = onnx.load(onnx_model_path)
sess = InferenceSession(onx.SerializeToString())
res = sess.run(None, input_dict)

# Print scores
outlier_scores = res[0]
print(np.transpose(outlier_scores[:10])[0])

License

BSD 2-Clause License. See LICENSE for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isolation_forest_onnx-4.1.7.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

isolation_forest_onnx-4.1.7-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file isolation_forest_onnx-4.1.7.tar.gz.

File metadata

  • Download URL: isolation_forest_onnx-4.1.7.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for isolation_forest_onnx-4.1.7.tar.gz
Algorithm Hash digest
SHA256 9bd2f2075dfbe008bcef83f03a3a7f3a3182286c5fa72a82b7e67134a28746a8
MD5 4f1ef0afe0d8f70b9e5ff461cd60e34c
BLAKE2b-256 d207a6ead28c6a8a9d0c97d022ea619123e83ff2ad8d5e274438982c4e53570a

See more details on using hashes here.

File details

Details for the file isolation_forest_onnx-4.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for isolation_forest_onnx-4.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a686833dd9626ed27484c03785f8253e41a5cf63c52e483eeed3d950f17bc3b3
MD5 a6761f74258de668d014f5e1107b3511
BLAKE2b-256 37285491b04b4896397988ae8ef80b4ec486e46d7d1c77c8c2744561d1f3c578

See more details on using hashes here.

Release history Release notifications | RSS feed

4.1.8

2 files

This release

4.1.7 This release

2 files

4.1.6

2 files

4.1.5

2 files

4.1.4

2 files

4.1.3

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.12

2 files

4.0.11

2 files

4.0.9

2 files

4.0.8

2 files

4.0.7

2 files

4.0.6

2 files

4.0.4

2 files

4.0.1

2 files

4.0.0

2 files

3.2.14

2 files

3.2.13

2 files

3.2.12

2 files

3.2.10

2 files

3.2.9

2 files

3.2.7

2 files

3.2.5

2 files

3.2.3

2 files

3.2.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page