Tabular Classification
Scikit-learn
English
logistic_regression
cybersecurity
security
logs
anomaly-detection
intrusion-detection
siem
mitre-attack
explainable-ai
logistic-regression
skops
Eval Results (legacy)
Instructions to use DaveyD347/log-anomaly-context-lr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use DaveyD347/log-anomaly-context-lr with Scikit-learn:
# ⚠️ Model filename not specified in config.json
- Notebooks
- Google Colab
- Kaggle
Download src/common.py from DaveyD347/log-anomaly-context-lr: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/DaveyD347/log-anomaly-context-lr/resolve/main/src/common.py
- Command line
-
hf download hf://DaveyD347/log-anomaly-context-lr/src/common.py
-
curl -L -o common.py https://huggingface.co/DaveyD347/log-anomaly-context-lr/resolve/main/src/common.py
2.58 kB
| """Shared helpers: load the incident-logs dataset, featurize it, build matrices.""" | |
| from __future__ import annotations | |
| import os | |
| import re | |
| import numpy as np | |
| import pandas as pd | |
| from scipy.sparse import csr_matrix, hstack | |
| from features import NUMERIC, featurize_stream | |
| HF_DATASET = "DaveyD347/sanitized-incident-logs" | |
| def load_events(data_dir: str | None, split: str) -> pd.DataFrame: | |
| """Events of one split, ordered as streams (session, then seq). Reads a local copy if given, else the Hub.""" | |
| if data_dir and os.path.exists(f"{data_dir}/events/{split}.parquet"): | |
| df = pd.read_parquet(f"{data_dir}/events/{split}.parquet") | |
| else: | |
| from huggingface_hub import hf_hub_download | |
| df = pd.read_parquet(hf_hub_download(HF_DATASET, f"data/events/{split}.parquet", repo_type="dataset")) | |
| return df.sort_values(["session_id", "seq"]).reset_index(drop=True) | |
| def featurize_df(df: pd.DataFrame): | |
| """Run the stream featurizer over every session. Returns (token lists, numeric array) aligned with df rows.""" | |
| toks, nums = [], [] | |
| for _, g in df.groupby("session_id", sort=False): | |
| for _, t, x in featurize_stream(g.raw_log.tolist()): | |
| toks.append(t) | |
| nums.append(x) | |
| return toks, np.asarray(nums, dtype=np.float64) | |
| def build_vocab(token_lists, min_count=3): | |
| counts = {} | |
| for ts in token_lists: | |
| for t in set(ts): | |
| counts[t] = counts.get(t, 0) + 1 | |
| vocab = sorted(t for t, c in counts.items() if c >= min_count) | |
| return {t: i for i, t in enumerate(vocab)} | |
| def token_matrix(token_lists, vocab): | |
| rows, cols = [], [] | |
| for r, ts in enumerate(token_lists): | |
| for c in sorted({vocab[t] for t in ts if t in vocab}): | |
| rows.append(r) | |
| cols.append(c) | |
| return csr_matrix((np.ones(len(rows)), (rows, cols)), shape=(len(token_lists), len(vocab))) | |
| def design_matrix(token_lists, nums, vocab): | |
| return hstack([token_matrix(token_lists, vocab), csr_matrix(nums)]).tocsr() | |
| def feature_names(vocab): | |
| return [t for t, _ in sorted(vocab.items(), key=lambda kv: kv[1])] + NUMERIC | |
| # ---- text normalisation for the line-only TF-IDF baseline ------------------------------------------------------ | |
| _MASKS = [ | |
| (re.compile(r"\b\d{1,3}(?:\.\d{1,3}){3}\b"), " IP "), | |
| (re.compile(r"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b"), " UUID "), | |
| (re.compile(r"\bAKIA[0-9A-Z]{16}\b"), " AKEY "), | |
| (re.compile(r"\d+"), "0"), | |
| ] | |
| def mask_line(s: str) -> str: | |
| for rx, rep in _MASKS: | |
| s = rx.sub(rep, s) | |
| return s | |