Download code/scripts/tree_family.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/scripts/tree_family.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench/code/scripts/tree_family.py
-
curl -L -o tree_family.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/scripts/tree_family.py
3.67 kB
| """Tree-family sweep on the identity-free 398-d chemistry block. | |
| The reported PhysChem-Tree is a random forest with leaf=2. This script asks | |
| whether a stronger tabular learner on the *same* features pushes past it, and | |
| whether a small cross-family stack clears the significance bar. | |
| PYTHONPATH=src python scripts/tree_family.py --models catboost lightgbm xgb | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import time | |
| import numpy as np | |
| import pandas as pd | |
| from scipy import stats | |
| from lnpbench import data, metrics, paths | |
| from lnpbench.store import FeatureStore | |
| def make_catboost(): | |
| from catboost import CatBoostRegressor | |
| return CatBoostRegressor(iterations=800, depth=6, learning_rate=0.05, | |
| l2_leaf_reg=6.0, random_seed=0, verbose=0) | |
| def make_lightgbm(): | |
| import lightgbm as lgb | |
| return lgb.LGBMRegressor(n_estimators=800, num_leaves=31, learning_rate=0.05, | |
| min_child_samples=8, subsample=0.8, subsample_freq=1, | |
| colsample_bytree=0.8, reg_lambda=2.0, random_state=0, verbose=-1) | |
| def make_xgb_strong(): | |
| import xgboost as xgb | |
| return xgb.XGBRegressor(n_estimators=800, max_depth=6, learning_rate=0.05, | |
| subsample=0.8, colsample_bytree=0.8, | |
| reg_lambda=4.0, min_child_weight=4, random_state=0, | |
| n_jobs=8, verbosity=0) | |
| def make_rf(): | |
| from lnpbench.models.classical import make_random_forest | |
| return make_random_forest() | |
| MAKERS = {"catboost": make_catboost, "lightgbm": make_lightgbm, | |
| "xgb_strong": make_xgb_strong, "rf": make_rf} | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--models", nargs="+", default=["catboost", "lightgbm", "xgb_strong"]) | |
| ap.add_argument("--block", default="base") | |
| ap.add_argument("--out", default="results/tree_family.csv") | |
| args = ap.parse_args() | |
| store = FeatureStore() | |
| z = np.load(paths.CACHE_DIR / "schema_blocks.npz", allow_pickle=True) | |
| X = np.asarray(z[args.block], dtype=np.float32) | |
| tasks = data.load_tasks(paths.CACHE_DIR / "tasks.pkl") | |
| tasks = [t for t in tasks if t.name.split("fold")[-1].isdigit()] | |
| rows = [] | |
| for name in args.models: | |
| for task in tasks: | |
| t0 = time.time() | |
| model = MAKERS[name]() | |
| model.fit(X[task.train_idx], store.y[task.train_idx]) | |
| pred = model.predict(X[task.test_idx]) | |
| met = metrics.evaluate(store.y[task.test_idx], pred) | |
| rows.append({"model": name, "task": task.name, "study": task.meta.get("study"), | |
| "seed": 0, "spearman": met["spearman"], "ef10": met["ef10"], | |
| "runtime_s": round(time.time() - t0, 1)}) | |
| print(f"{name:12s} {task.name:22s} rho={met['spearman']:.4f} " | |
| f"({time.time() - t0:.0f}s)", flush=True) | |
| df = pd.DataFrame(rows) | |
| df.to_csv(args.out, index=False) | |
| print("\n== 20-task mean ==") | |
| print(df.groupby("model").spearman.agg(["mean", "std"]).round(4).to_string()) | |
| # paired comparison against the published random forest | |
| ref = pd.read_csv("results/all_results.csv") | |
| ref = ref[(ref.model == "randomforest") & (ref.task.str.contains("_fold"))] | |
| ref = ref.set_index("task").spearman | |
| print("\n== paired vs random forest (Wilcoxon, n=20) ==") | |
| for name in args.models: | |
| g = df[df.model == name].set_index("task").spearman.reindex(ref.index).dropna() | |
| d = g - ref.reindex(g.index) | |
| print(f"{name:12s} d={d.mean():+.4f} p={stats.wilcoxon(d).pvalue:.4g}") | |
| if __name__ == "__main__": | |
| main() |