| import os |
| import pickle |
| import numpy as np |
| import pandas as pd |
| from arfpy import arf |
|
|
| def _sanitize_for_arf(df: pd.DataFrame) -> pd.DataFrame: |
| """缓解 forge 阶段 scipy.stats.truncnorm / 除零:处理 inf、NaN 与极端尾部。""" |
| df = df.replace([np.inf, -np.inf], np.nan) |
| df = df.dropna(axis=1, how="all") |
| for col in df.select_dtypes(include=[np.number]).columns: |
| med = df[col].median() |
| if pd.isna(med): |
| med = 0.0 |
| df[col] = df[col].fillna(med) |
| nu = int(df[col].nunique(dropna=True)) |
| if nu <= 1: |
| continue |
| q_low = float(os.environ.get("ARF_CLIP_QUANTILE_LOW", "0.001")) |
| q_high = float(os.environ.get("ARF_CLIP_QUANTILE_HIGH", "0.999")) |
| lo, hi = df[col].quantile(q_low), df[col].quantile(q_high) |
| if pd.notna(lo) and pd.notna(hi) and lo < hi: |
| df[col] = df[col].clip(lo, hi) |
| return df |
|
|
| df = pd.read_csv("/work/output-Benchmark-trainonly-v1/m4/arf/arf-m4-20260504_205355/staged/public/train.csv") |
| df = _sanitize_for_arf(df) |
| num_trees = int(os.environ.get("ARF_NUM_TREES", "30")) |
| delta = float(os.environ.get("ARF_DELTA", "0")) |
| max_iters = int(os.environ.get("ARF_MAX_ITERS", "10")) |
| early_stop = (os.environ.get("ARF_EARLY_STOP", "true").strip().lower() in ("1", "true", "yes")) |
| verbose = (os.environ.get("ARF_VERBOSE", "true").strip().lower() in ("1", "true", "yes")) |
| min_node_size = int(os.environ.get("ARF_MIN_NODE_SIZE", "5")) |
| print(f"[ARF] Training on {len(df)} rows, {len(df.columns)} cols") |
| print(f"[ARF] Config num_trees={num_trees} delta={delta} max_iters={max_iters} early_stop={early_stop} min_node_size={min_node_size}") |
|
|
| model = arf.arf(x=df, num_trees=num_trees, delta=delta, max_iters=max_iters, early_stop=early_stop, verbose=verbose, min_node_size=min_node_size) |
| if hasattr(model, "fit"): |
| model.fit() |
| elif hasattr(model, "forde"): |
| model.forde() |
| else: |
| raise RuntimeError("arfpy API: no fit() / forde()") |
|
|
| with open("/work/output-Benchmark-trainonly-v1/m4/arf/arf-m4-20260504_205355/arf_model.pkl", "wb") as f: |
| pickle.dump(model, f) |
| print(f"[ARF] Model saved -> /work/output-Benchmark-trainonly-v1/m4/arf/arf-m4-20260504_205355/arf_model.pkl") |
|
|