Uma biblioteca para detecção de outliers em embeddings
Project description
Outliers Detect
A Python library for outlier detection in embeddings using multiple statistical and machine learning methods.
Features
- 🎯 5 different methods for outlier detection
- 📊 Automatic visualization of results
- 🐼 Simple interface with pandas DataFrames
- 📈 Detailed metrics for each observation
- 📚 Demo notebook included
Installation
pip install outliers-detect
For development:
git clone https://github.com/your-username/outliers-detect
cd outliers-detect
pip install -e ".[dev]"
Basic Usage
import pandas as pd
from outliers_detect import outliers_detect_df
# Creating a DataFrame with embeddings
df = pd.DataFrame({
'id': [1, 2, 3, 4],
'embedding': [
'[0.1, 0.2, 0.3]',
'[0.2, 0.3, 0.4]',
'[0.15, 0.25, 0.35]',
'[5.0, 5.1, 5.2]' # This is an outlier
]
})
# Detecting outliers using the percentile method
result = outliers_detect_df(
df,
metodo='percentil',
threshold_percentil=95,
plotar=True
)
# Viewing the results
print(result[['id', 'eh_outlier', 'score']])
# Filtering only outliers
outliers = result[result['eh_outlier']]
print(f"Found {len(outliers)} outliers")
Available Methods
1. Percentile
Detects outliers based on a specific percentile of distances.
result = outliers_detect_df(
df,
metodo='percentil',
threshold_percentil=95 # Considers top 5% as outliers
)
2. Z-Score
Uses standard deviation to identify outliers.
result = outliers_detect_df(
df,
metodo='zscore',
threshold_z=2.0 # Points with z-score > 2.0 are outliers
)
3. IQR (Interquartile Range)
Uses the Interquartile Range method.
result = outliers_detect_df(
df,
metodo='iqr' # Uses standard IQR criteria
)
4. Cosine Distance
Based on cosine distance to the centroid of embeddings.
result = outliers_detect_df(
df,
metodo='cosine',
threshold_cosine=0.10 # Distances > 0.10 are outliers
)
5. PCA Reconstruction
Uses PCA reconstruction error to detect outliers.
result = outliers_detect_df(
df,
metodo='pca_reconstruction',
n_components=2 # Number of principal components
)
Function Parameters
outliers_detect_df(df, metodo, **kwargs)
Required parameters:
df: DataFrame with 'embedding' column containing list stringsmetodo: Detection method ('percentil', 'zscore', 'iqr', 'cosine', 'pca_reconstruction')
Optional parameters:
threshold_percentil: Percentile for 'percentil' method (default: 95)threshold_z: Z-score threshold for 'zscore' method (default: 2.0)threshold_cosine: Cosine distance threshold for 'cosine' method (default: 0.10)n_components: PCA components for 'pca_reconstruction' (default: 2)plotar: Whether to plot graphs (default: False)
Returns: DataFrame with additional columns:
eh_outlier: Boolean indicating if it's an outlierscore: Score/metric from the used methoddistancia: Calculated distance (when applicable)
Examples and Demonstrations
Demo Notebook
A complete Jupyter notebook is available at exemplos/outliers_detect_demo.ipynb with:
- 📊 Creation of synthetic data with known outliers
- 🎨 3D visualizations of embeddings
- 🔍 Testing of all available methods
- 📈 Comparison between different methods
- ⚙️ Parameter tuning and their effects
To run the notebook:
# Install additional dependencies if needed
pip install jupyter matplotlib
# Run the notebook
jupyter notebook exemplos/outliers_detect_demo.ipynb
Advanced Example
import numpy as np
import pandas as pd
from outliers_detect import outliers_detect_df
# Creating more realistic data
np.random.seed(42)
# Normal embeddings
normal_data = np.random.normal(0, 1, (100, 5))
# Outliers
outlier_data = np.random.normal(4, 0.5, (5, 5))
# Combining data
all_embeddings = np.vstack([normal_data, outlier_data])
# Creating DataFrame
df = pd.DataFrame({
'id': range(len(all_embeddings)),
'embedding': [str(emb.tolist()) for emb in all_embeddings],
'category': ['normal'] * 100 + ['outlier'] * 5
})
# Testing different methods
methods = ['percentil', 'zscore', 'iqr', 'cosine', 'pca_reconstruction']
for method in methods:
print(f"\n=== Method: {method} ===")
result = outliers_detect_df(df, metodo=method, plotar=True)
detected_outliers = result['eh_outlier'].sum()
print(f"Detected outliers: {detected_outliers}")
# Calculating precision (if you know the true outliers)
true_outliers = df['category'] == 'outlier'
precision = (result['eh_outlier'] & true_outliers).sum() / detected_outliers
print(f"Precision: {precision:.2%}")
Development
Environment Setup
- Clone the repository:
git clone https://github.com/your-username/outliers-detect
cd outliers-detect
- Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- Windows:
venv\Scripts\activate - Unix/MacOS:
source venv/bin/activate
- Windows:
-
Install development dependencies:
pip install -e ".[dev]"
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=outliers_detect
# Run specific tests
pytest tests/test_outliers_detect.py::test_percentil_method
Project Structure
outliers-detect/
├── outliers_detect/
│ ├── __init__.py
│ └── outliers_detect.py
├── tests/
│ └── test_outliers_detect.py
├── exemplos/
│ └── outliers_detect_demo.ipynb
├── pyproject.toml
├── setup.py
├── README.md
└── requirements.txt
Contributing
Contributions are welcome! Please:
- Fork the project
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v0.1.0
- ✨ Initial implementation with 5 detection methods
- 📊 Support for automatic visualizations
- 📚 Demo notebook included
- 🧪 Complete unit test suite
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file outliers_detect-0.1.0.tar.gz.
File metadata
- Download URL: outliers_detect-0.1.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf08d6a7e9741dc756d18f4807e8e7ecb5402395eb3a358c3b3bc01f55173865
|
|
| MD5 |
3b24f4bb011b184c3d5feabf590caa02
|
|
| BLAKE2b-256 |
3146a5c0712d2c440fe47a3297efcc3675fb2958b50c3e4b393144802f83f9a6
|
File details
Details for the file outliers_detect-0.1.0-py3-none-any.whl.
File metadata
- Download URL: outliers_detect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed7f213b9752d2b462cf3c3657cf60be459b82a4877157cf31476277be2401b1
|
|
| MD5 |
b2df02751dc9b7f367d8d109408aa1c2
|
|
| BLAKE2b-256 |
bf4863e9f95a268f3467599682ce2be7c2b912b126686d74c37c5d96e24b25ed
|