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:
- Open an issue on the GitLab Issues page.
- 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.
Metadata
Release files for pt-loop 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pt_loop-0.1.0.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pt_loop-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.1 kB
Release files / pt_loop-0.1.0.tar.gz
| Download URL | pt_loop-0.1.0.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dfe89b602665b428bc05200b9a8047604c605397bc26eb485376190b9ff436b8
|
|
BLAKE2b-256 checksum How to use checksums |
590b5b06c6928ca2ccdb184463a1248a8764f51d51d7b6cf9bd2dbcdc70b12d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|
Release files / pt_loop-0.1.0-py3-none-any.whl
| Download URL | pt_loop-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0c7bffebab38febbcd9710d31ded07419f83555f920efd0315fa53fc2ced212
|
|
BLAKE2b-256 checksum How to use checksums |
dabf6c1c67a99f1b181ac4a3bbaeb5d2004a703d4a81bbc37cd9706e2e124e62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.11.13
|