Skip to content
svganomaly-scores

Anomaly score distribution

How unusual are the most anomalous rows compared with the rest, and where is the cut?

Demo data · seed 20261101 · 1,200 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the histogram as all rows' anomaly scores.
  2. Find the dashed threshold: rows beyond it are flagged in red.
  3. A clear gap before the threshold is reassuring; a smooth tail means the cut is arbitrary.
  1. Callout 1: Most rows: low scores.
  2. Callout 2: Threshold: the flagging cut.
  3. Callout 3: Flagged rows.

Watch out

  • An anomaly isn't an error or a fraud. Investigate each flag.

Defaults

Threshold (0 = smallest flagged score)
0
The score above which rows are flagged.
Range -1,000,000,000 to 1,000,000,000, step 0.01

Pinned in the code

  • Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
  • Scores are oriented so higher = more anomalous (the run converts scikit-learn's negative outlier factor and isotree's depth-based scores to that convention).
  • Bins: about 1.5 √n, between 10 and 60. Without an explicit threshold the line sits at the smallest flagged score (the contamination cut the run used).
# Anomaly score distribution — Codesplash Chart Lab (recipe "anomaly-scores")
# controls: threshold=0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("anomaly-scores-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
def truthy(s):
    return s.astype(str).str.strip().str.lower().isin(["true", "1", "yes", "t"])
S = "anomaly score"
FLAG = "flagged"
THRESHOLD = 0  # 0 = the smallest flagged score
d = df.dropna(subset=[S])
flag = truthy(d[FLAG]) if FLAG else pd.Series(False, index=d.index)
k = int(min(60, max(10, round(1.5 * np.sqrt(len(d))))))  # about 1.5 √n bins
edges = np.linspace(d[S].min(), d[S].max(), k + 1)
normal, _ = np.histogram(d.loc[~flag, S], bins=edges)
flagged, _ = np.histogram(d.loc[flag, S], bins=edges)
thr = THRESHOLD if THRESHOLD != 0 else (d.loc[flag, S].min() if flag.any() else None)
fig, ax = plt.subplots(figsize=(8, 4.5))
w = np.diff(edges)
ax.bar(edges[:-1], normal, width=w, align="edge", color=PALETTE[0], label="normal")
ax.bar(edges[:-1], flagged, width=w, align="edge", bottom=normal, color="#ce5342", label="flagged")
if thr is not None:
    ax.axvline(thr, color=REF, linestyle="--", linewidth=1.2, label=f"threshold {thr:.3g}")
ax.set_xlabel(S)
ax.set_ylabel("rows")
ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads anomaly-scores-demo.csv · app theme from the optional codesplash helper