Note
Click here to download the full example code
FFX2ΒΆ
Out:
Actual function: x_0^2 + sin(x_1) + x_2 cos(x_1)
Fit result: 0.005 x_0**2 + 0.021 x_0**2*x_1**2 + 0.021 x_0**2*x_2**2 + -0.008 x_1**2*x_2**2 + 6.301
fit score (on training data): 0.05223809944750665
fitting took 163.28 seconds.
import datetime
import numpy as np
from sparsereg.model.ffx import FFX
np.random.seed(42)
x = np.random.normal(scale=3.0, size=(33333, 3))
print("Actual function: x_0^2 + sin(x_1) + x_2 cos(x_1)")
y = x[:, 0] ** 2 + np.sin(x[:, 1]) + x[:, 2] * np.cos(x[:, 1])
model = FFX(
n_jobs=-1,
l1_ratios=(0.001, 0.01, 0.5, 0.9, 0.999),
exponents=[1, 2],
operators={"sin": np.sin, "cos": np.cos},
target_score=1e-6,
max_complexity=250,
num_alphas=1000,
eps=1e-70,
rational=False,
)
t0 = datetime.datetime.utcnow()
model.fit(x, y)
dur = datetime.datetime.utcnow() - t0
dur = dur.total_seconds()
print(f"Fit result: {model.print_model()}")
print(f"fit score (on training data): {model.score(x, y)}")
print(f"fitting took {dur:.2f} seconds.")
Total running time of the script: ( 2 minutes 44.718 seconds)