Skip to main content

Local Outlier Probabilities (LoOP) implementation in PyTorch

Project description

PyTorch LoOP

Introduction

pt_loop is a pure PyTorch implementation of the Local Outlier Probabilities (LoOP) algorithm, designed for seamless integration into PyTorch-based machine learning pipelines. It offers high performance on both CPU and GPU (CUDA), leveraging PyTorch's tensor capabilities throughout the entire computation.

Unlike traditional implementations that might require data transfers to other libraries, pt_loop keeps your data on the PyTorch device (CPU or GPU) from start to finish, minimizing overhead and maximizing efficiency for large-scale anomaly detection tasks.

The original paper can be found here: https://www.dbs.ifi.lmu.de/Publikationen/Papers/LoOP1649.pdf

Installation

pt_loop requires PyTorch.

First, ensure you have PyTorch installed (refer to the official PyTorch website for installation instructions specific to your system and CUDA version).

Then, install pt_loop directly from PyPI:

pip install pt_loop

Quick Start & Usage Examples

Here's how to get started with pt_loop.

import torch
import matplotlib.pyplot as plt

from pt_loop import loop

Basic LoOP Usage

# 1. Generate some synthetic data with an obvious outlier
data = torch.cat([
    torch.randn(100, 2) * 0.5 + torch.tensor([0.0, 0.0]), # Cluster 1
    torch.randn(100, 2) * 0.5 + torch.tensor([5.0, 5.0]), # Cluster 2
    torch.tensor([[2.5, 2.5]]),                           # Clear outlier
])

# Move data to GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data = data.to(device)

# 2. Run LoOP
print(f"Running LoOP on {device}...")
loop_scores = loop(
    data,
    k=10,                      # Number of nearest neighbors
    lambda_=3.0,               # Scaling factor
    distance_metric="l2",      # or "cosine"
)

print("\nLoOP Results:")
print(f"Scores Shape: {loop_scores.shape}")
print(f"First 5 Scores: {loop_scores[:5].tolist()}")
print(f"Last score (outlier candidate): {loop_scores[-1].item()}")

# 3. (Optional) Visualize the scores
plt.figure(figsize=(8, 6))
scatter = plt.scatter(data[:, 0].cpu(), data[:, 1].cpu(), c=loop_scores.cpu(), cmap="plasma", s=50, alpha=0.8)
plt.colorbar(scatter, label="LoOP Score (Outlier Probability)")
plt.title("Local Outlier Probabilities (LoOP)")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.grid(True)
plt.show()

Contributing

Contributions are very welcome! If you find a bug, have a feature request, or want to contribute code, please feel free to:

  1. Open an issue on the GitLab Issues page.
  2. Submit a Pull Request.

Please ensure your code adheres to the existing style (Black, isort) and passes all tests.

License

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

Project details


Download files

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

Source Distribution

pt_loop-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

pt_loop-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file pt_loop-0.1.0.tar.gz.

File metadata

  • Download URL: pt_loop-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pt_loop-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dfe89b602665b428bc05200b9a8047604c605397bc26eb485376190b9ff436b8
MD5 a462c48c988b6671d8f1f15dee0f6046
BLAKE2b-256 590b5b06c6928ca2ccdb184463a1248a8764f51d51d7b6cf9bd2dbcdc70b12d3

See more details on using hashes here.

File details

Details for the file pt_loop-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pt_loop-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pt_loop-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0c7bffebab38febbcd9710d31ded07419f83555f920efd0315fa53fc2ced212
MD5 45b1ac9eae2b66e6c495368cd4f4e04a
BLAKE2b-256 dabf6c1c67a99f1b181ac4a3bbaeb5d2004a703d4a81bbc37cd9706e2e124e62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page