Keplearn:几秒内从数据中提取方程
Keplearn: Extract Equations from Data in Seconds

原始链接: https://github.com/owls-on-wires/keplearn

Keplearn 是一款确定性的、基于 Rust 的符号回归工具,可从表格数据中发现紧凑的闭式方程。它从标准输入读取 TSV 或 CSV,并输出 JSON 候选结果,作为静态单二进制文件运行,通常可在可配置超时内以毫秒级解析出精确规律。结果是确定性的,报告的 R² 由打印出的方程计算。它通过递归可逆化简、仿射单项式、稀疏项、复合形式进行搜索,并在验证后将常数吸附为有理数或 π、e 和 √2 的倍数。在 SRBench 上,它取得了较强的 Feynman 真值解率(零噪声下为 64.7%),而黑盒中位 R² 接近零,反映了其专注于精确解析规律的目标。由 Chandler Freeman 基于 MIT 许可证发布。

Keplearn,一个从数据中提取方程的工具,在 Hacker News 引发讨论。评论者强调了开普勒的智识谦逊:当布拉赫的观测与之矛盾时,他放弃了优雅的嵌套多面体理论,随后发展出行星运动定律。其他人指出,在没有现代工具的情况下进行此类几何运算是困难的。一位评论者推广了未发表的 Lean/Rust 重力与双缝物理研究,引发批评称该帖术语堆砌且不连贯;他们为其辩护。其他评论质疑部分内容是否为 AI/vibe-coded,还有一人询问 TabPFN 的对比情况。总体而言,该帖将对开普勒名字与工具的欣赏与对推测性物理帖子和代码质量的辩论混在一起。
相关文章

原文

Symbolic regression in seconds.

Keplearn is named after Johannes Kepler, who deduced his laws of planetary motion by studying tables of astronomical observations taken by Tycho Brahe. That is what this program attempts to do: given columns of measurements, find the compact law that relates them.

    m1   |  m2   |  r   | force
   ------+-------+------+-------
    1.0  |  2.0  | 1.0  |  2.00
    2.0  |  2.0  | 2.0  |  1.00        ┌──────────┐
    1.5  |  3.0  | 3.0  |  0.50  ──►   │ keplearn │ ──►   force = m1 * m2 / r**2
    2.5  |  1.0  | 0.5  | 10.00        └──────────┘
    3.0  |  4.0  | 2.0  |  3.00

More formally, it's a deterministic symbolic regression engine (written in Rust) that recovers closed-form equations from tabular data by best-first recursive reduction.

  • Reads a TSV or CSV from stdin, prints candidate equations as JSON
  • Single binary, zero dependencies (fully static with the musl target)
  • Designed to answer in seconds: exact laws usually resolve in milliseconds, and --timeout caps the hard cases
  • Deterministic; the same input produces the same output, byte for byte
  • The reported r2 is computed from the printed equation itself, never from an internal fitting stage
  • Fitted constants snap to rationals and multiples of pi, e, and sqrt(2), with every snap verified before it is kept

macOS Windows Linux

macOS (universal: Apple Silicon and Intel)

curl -L -o keplearn https://github.com/owls-on-wires/keplearn/releases/latest/download/keplearn-macos-universal
chmod +x keplearn
./keplearn --help

Windows (PowerShell)

curl.exe -L -o keplearn.exe https://github.com/owls-on-wires/keplearn/releases/latest/download/keplearn-windows-x86_64.exe
.\keplearn.exe --help

Linux (x86_64, fully static)

curl -L -o keplearn https://github.com/owls-on-wires/keplearn/releases/latest/download/keplearn-linux-x86_64
chmod +x keplearn
./keplearn --help

Name the column to predict with --target (the default is a column called target) and pipe in a TSV or CSV with a header row. The delimiter comes from the header: tab if present, else comma.

$ ./keplearn --target force --top-k 1 --pretty --out laws.json < gravity.tsv
$ cat laws.json
{
  "results": [
    {
      "expr": "((x1**(1))*(x2**(1))*(x3**(-2)))",
      "r2": 1.0000000000,
      "search_r2": 1.0000000000,
      "holdout_r2": 0.9999999999998754,
      "scale": 0.9999984680738088,
      "offset": 0.000011830325937959667,
      "columns": [
        0,
        1,
        2
      ],
      "factor": "direct",
      "model": "((x1*x2)*((x3)**(-2)))"
    }
  ],
  "time_ms": 0,
  "n_expressions": 0,
  "n_evaluated": 0,
  "stopped_by": "exact_exit"
}

x1..xN are the feature columns in file order, target excluded; here x1=m1, x2=m2, x3=r, so the recovered law reads m1*m2/r**2. Candidates arrive best-first: results[0].model is the answer and its r2 is scored on the supplied data. Diagnostics print to stderr, so stdout is always one JSON object.

Useful flags:

./keplearn --target Tc --timeout 60000 < superconductors.csv   # cap the search at 60 s
./keplearn --top-k 3 < data.tsv                                # emit the 3 best candidates
./keplearn --target y --sample 1000 < big.tsv                  # screen on more rows
./keplearn --help                                              # full flag list and output schema

The engine searches over chains of invertible reductions applied to the target column: it divides out a fitted factor, inverts a transform such as log, sqrt, or arcsin, fits an inner affine node, and repeats this to a fixed depth. At each node it attempts a terminal fit of the reduced target with an affine monomial whose exponents come from a log-linear regression, with a sparse k-term monomial, or with one of a small set of composite forms (L2 norms, rationals, trig). Candidates are ranked on an internal 25% holdout with an MDL complexity penalty.

Fitted constants are snapped to nearby rationals and to multiples of pi, e, and sqrt(2). A snap is kept only when a verification pass shows the model still scores as well on holdout and full data. The winning expression is then simplified algebraically, refit for scale and offset against the full dataset, and re-scored. The r2 in the output comes from evaluating the printed model string; it is not carried over from any internal fitting stage.

Keplearn is a method in SRBench, the standard symbolic regression benchmark, added in cavalab/srbench#223.

These numbers come from SRBench's own containerized pipeline, graded by its sympy equivalence check, at one trial per dataset and a 60 second per-fit budget. Ground-truth symbolic solution rate by target noise:

suite noise 0 0.001 0.01 0.1
Feynman (116) 75 (64.7%) 65 (56.0%) 51 (44.0%) 10 (8.6%)
Strogatz (14) 6 (42.9%) 6 (42.9%) 3 (21.4%) 1 (7.1%)

The highest published Feynman symbolic rate on SRBench is AIFeynman at 55.8%, with the next group near 27%; Keplearn reaches 64.7% at a fraction of their compute budget.

On the 138 black-box datasets, which have no closed-form ground truth, the median test R2 is near zero (about -0.003). Keplearn targets exact analytic laws rather than general curve fitting, and that result reflects the trade-off.

Fit time under the same 60 second cap, one core per fit:

suite median mean max fits under 1 s
Feynman, noise 0 1.3 s 13.3 s 60.7 s 55/116
Feynman, noise 0.001-0.1 11-14 s 15-23 s 60.7 s 0/348
Strogatz, all noise levels 1.3 s 1.5 s 3.6 s 8/56
Black-box 60.0 s 39.1 s 65.8 s 15/138

Solved cases exit early. At noise 0, half the Feynman suite finishes in under a second; under noise the exact-exit path cannot fire, so those fits run to the cap.

Keplearn is released under the MIT license.

Created by Chandler Freeman [email protected] (https://github.com/owls-on-wires).

联系我们 contact @ memedata.com