fix: D5 downsampling index mismatch + add 1M-row medium-scale test to docs

This commit is contained in:
PM-pinou
2026-07-17 14:02:30 +08:00
parent 70e2167fea
commit fa305515c1
2 changed files with 31 additions and 4 deletions
+22 -4
View File
@@ -848,11 +848,14 @@ async def _handle_run_clustering(
idx = rng.choice(len(data), 50000, replace=False)
data = data[idx]
# ── D5: General downsampling for big data (>50K rows → 50K) ───────
# ── D5: Downsampling → extract matching indices ──────────────
# Downsample to 50K max, store indices so _handle_extract_features
# can filter the same rows.
_ds_idx = None
if len(data) > 50000:
rng = np.random.default_rng(42)
idx = rng.choice(len(data), 50000, replace=False)
data = data[idx]
_ds_idx = rng.choice(len(data), 50000, replace=False)
data = data[_ds_idx]
# ── H17: Variance filtering ───────────────────────────────────────
# Remove features with zero / near-zero variance (< 1e-10)
@@ -983,6 +986,7 @@ async def _handle_run_clustering(
'params': p,
'random_state': random_state,
'feature_columns': feature_cols,
'_ds_idx': _ds_idx.tolist() if _ds_idx is not None else None,
},
parent_dataset_id=dataset_id,
)
@@ -1092,9 +1096,23 @@ async def _handle_extract_features(
try:
lf: pl.LazyFrame = dataset_entry['lazyframe']
df = lf.collect(streaming=True)
labels = cluster_entry['labels']
# D5: If clustering downsampled the data, filter matching rows
_ds_idx = cluster_entry.get('params', {}).get('_ds_idx')
if _ds_idx is not None:
df = lf.collect(streaming=True)
# Add row index, filter, then drop it
df = df.with_row_index('__ds_idx')
df = df.filter(pl.col('__ds_idx').is_in(_ds_idx))
df = df.drop('__ds_idx')
else:
df = lf.collect(streaming=True)
# Truncate labels if df was filtered but labels weren't
if len(labels) != len(df):
labels = labels[:len(df)]
# Only use numeric columns for feature extraction
numeric_cols = [c for c in df.columns
if df[c].dtype in (pl.Float32, pl.Float64,