OPE-means / benchmarks /get_robust_data.py
Nabidnur's picture
OPE-means v5: code, results, technical report, figures, notebooks, tests
f640c46 verified
Raw History Blame Contribute Delete
3.96 kB
"""Prepare the robust-clustering benchmark of Jiang & Zhou 2026 (arXiv:2605.07130), exactly as
their MIT-licensed code (github.com/MasterZhou1/Robust-Clustering, dataset/preprocess_datasets.py):
* SKIN (Skin_NonSkin.txt, first 3 columns), SUSY (SUSY.csv, columns 1..18): z-score each feature
(std 0 -> 1), then append int(0.01 n) outliers from np.random.default_rng(42).uniform(-xi, xi),
xi in {5, 10}; k = 10, z = number of injected rows.
* SHUTTLE (shuttle.trn): z-score; the two smallest classes are outliers; k = 10.
* KDDFULL (kddcup.data): numeric columns [0] + 4..11 + 12..20 + 22..40 (37 features), z-score;
the three largest classes are inliers, the rest outliers; k = 3.
Usage: python benchmarks/get_robust_data.py OUT_DIR [SKIN SUSY SHUTTLE KDDFULL]
Writes OUT_DIR/<NAME>.npz with data (float64), outlier_mask, k.
"""
import gzip, os, shutil, subprocess, sys, urllib.request
from collections import Counter
import numpy as np
UCI = "https://archive.ics.uci.edu/ml/machine-learning-databases"
FILES = {
"Skin_NonSkin.txt": UCI + "/00229/Skin_NonSkin.txt",
"SUSY.csv.gz": UCI + "/00279/SUSY.csv.gz",
"shuttle.trn.Z": UCI + "/statlog/shuttle/shuttle.trn.Z",
}
def fetch(name, raw):
path = os.path.join(raw, name)
if not os.path.exists(path):
urllib.request.urlretrieve(FILES[name], path + ".part")
os.replace(path + ".part", path)
return path
def znorm(X):
mu, sd = X.mean(0), X.std(0)
sd[sd == 0] = 1.0
return (X - mu) / sd
def inject(X, xi):
rng = np.random.default_rng(42)
m = int(len(X) * 0.01)
out = rng.uniform(-xi, xi, size=(m, X.shape[1]))
mask = np.zeros(len(X) + m, bool); mask[len(X):] = True
return np.vstack([X, out]), mask
def save(out, name, X, mask, k):
np.savez(os.path.join(out, name + ".npz"), data=X, outlier_mask=mask, k=k)
print(f"{name}: n={len(X)} d={X.shape[1]} k={k} z={int(mask.sum())}")
def main(out, names):
raw = os.path.join(out, "raw"); os.makedirs(raw, exist_ok=True)
if "SKIN" in names:
d = np.loadtxt(fetch("Skin_NonSkin.txt", raw))
X = znorm(d[:, :-1].astype(np.float64))
for xi in (5, 10):
save(out, f"SKIN-{xi}", *inject(X, xi), 10)
if "SUSY" in names:
import pandas as pd
d = pd.read_csv(fetch("SUSY.csv.gz", raw), header=None).values
X = znorm(d[:, 1:].astype(np.float64))
for xi in (5, 10):
save(out, f"SUSY-{xi}", *inject(X, xi), 10)
if "SHUTTLE" in names:
z = fetch("shuttle.trn.Z", raw); txt = z[:-2]
if not os.path.exists(txt):
with open(txt, "wb") as f:
f.write(subprocess.run(["gzip", "-dc", z], capture_output=True, check=True).stdout)
d = np.loadtxt(txt); y = d[:, -1].astype(int)
small = [c for c, _ in Counter(y).most_common()[-2:]]
save(out, "SHUTTLE", znorm(d[:, :-1].astype(np.float64)), np.isin(y, small), 10)
if "KDDFULL" in names:
import pandas as pd
cols = [0] + list(range(4, 12)) + list(range(12, 21)) + list(range(22, 41))
# The UCI paths now serve a 53-byte placeholder; this is the gzip of kddcup.data that
# scikit-learn's fetch_kddcup99 uses (4,898,431 rows). Parsed column-wise to save memory.
gz = os.path.join(raw, "kddcup.data.gz")
if not os.path.exists(gz):
urllib.request.urlretrieve("https://ndownloader.figshare.com/files/5976045", gz + ".part")
os.replace(gz + ".part", gz)
df = pd.read_csv(gz, header=None, usecols=cols + [41], low_memory=False,
dtype={c: np.float64 for c in cols})
y = df[41].astype(str).str.rstrip(".").values
X = znorm(df[cols].values.astype(np.float64))
inl = [c for c, _ in Counter(y).most_common(3)]
save(out, "KDDFULL", X, ~np.isin(y, inl), 3)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2:] or ["SKIN", "SUSY", "SHUTTLE", "KDDFULL"])