Skip to main content

Hybrid semantic anomaly detector: row text embeddings + scaled numeric features.

Project description

semantic_sense

Semantic-Sense is a hybrid anomaly detection package that combines semantic embeddings with numeric features to find unusual rows in tabular datasets.
It is designed for Data Scientists who want to go beyond traditional anomaly detection and capture semantic relationships between categorical/textual values while still respecting numeric signals.


What It Does

  • Converts each row of a DataFrame into a text string (e.g., item=bread, Price=10).
  • Embeds that row with a lightweight sentence-transformer model (all-MiniLM-L6-v2).
  • (Optional Hybrid Mode) Concatenates scaled numeric features into the same representation.
  • Scores anomalies by cosine distance from the centroid of all embeddings.
  • Flags the top N% farthest rows as anomalies.

How It’s Different

  • Traditional methods (e.g., Local Outlier Factor, Isolation Forest) treat categorical variables as dummy/one-hot encoded → every unique value looks “equally far”.
  • Semantic-Sense understands relationships between values:
    • "bread" and "sandwich" are closer than "bread" and "meat".
  • By combining embeddings with numeric features, you get both:
    • Semantic closeness on categorical/text data.
    • Proper scaling on numeric outliers.

Workflow (What happens after passing a DataFrame)

flowchart TD
    A["Input: pandas DataFrame"] --> B{"Mode?"}

    %% TEXT MODE
    B -->|text| C1["Convert each row to Col=val string\n(row_to_text)"]
    C1 --> D1["Embed rows with SentenceTransformer\n(all-MiniLM-L6-v2)"]
    D1 --> E1["L2-normalize text embeddings"]

    %% HYBRID MODE
    B -->|hybrid| C2["Convert each row to Col=val string\n(row_to_text)\n+ Extract numeric cols"]
    C2 --> D2a["Embed rows with SentenceTransformer"]
    C2 --> D2b["Scale numeric columns (StandardScaler)"]
    D2a --> D2c["Concatenate text embeddings + scaled numerics"]
    D2b --> D2c
    D2c --> E2["L2-normalize hybrid vectors"]

    %% MERGE PATHS
    E1 --> F
    E2 --> F["Compute centroid (mean of unit vectors)"]

    F --> G["Compute cosine distance\nrow vs centroid"]
    G --> H["Rank rows by distance"]
    H --> I["Flag top_percent rows as anomalies"]

    I --> J["Output DataFrame: row_text, centroid_distance,rank, is_anomaly"]

Install

  • From GitHub:
    pip install git+https://github.com/rezuanurdip/semantic-sense.git
    
  • From PyPI:
    pip install semantic-sense
    

Examples

Check out the example/ directory for usage examples.


📜 License

This project is licensed under the Apache 2.0 License.

It bundles the all-MiniLM-L6-v2 model
and relies on the Sentence-Transformers library,
both of which are licensed under Apache 2.0.

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

semantic_sense-0.1.1.tar.gz (83.5 MB view details)

Uploaded Source

Built Distribution

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

semantic_sense-0.1.1-py3-none-any.whl (83.5 MB view details)

Uploaded Python 3

File details

Details for the file semantic_sense-0.1.1.tar.gz.

File metadata

  • Download URL: semantic_sense-0.1.1.tar.gz
  • Upload date:
  • Size: 83.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for semantic_sense-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bea49fa5bc9591e40073790e6303202fdd6584002aa353cf7957406e760836ac
MD5 e5dc3754c203e8e221ae47044dc83ee9
BLAKE2b-256 b1539659f5074d70f7a84a316d1bbcde0f0bddf05ee262a9cf1b377094471484

See more details on using hashes here.

File details

Details for the file semantic_sense-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: semantic_sense-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 83.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for semantic_sense-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00c6680f9d81de594259ff91942e525e8249c86be325bc5b528b477b49130692
MD5 e1d56971d4786637065126c64f767925
BLAKE2b-256 a2e28b416a2b66d7fbb8ba75084ce62fe7eea20d0512a4aa03312818bf612702

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