pykoopman icon indicating copy to clipboard operation
pykoopman copied to clipboard

Bug in model.fit with timedelays

Open moonlians opened this issue 2 years ago • 4 comments

from pykoopman.regression import EDMD
from sklearn.metrics import mean_absolute_percentage_error
from sklearn.metrics import mean_absolute_error
pred = 4
S = np.array(data)
n_output_vars = S.shape[1]  # Get the number of output variables
n_delays = 100
n = 310
regressor = EDMD()

# Loop through each timestep
for timestep in range(n, len(S) - pred):
    try:
        obs = TimeDelay(n_delays=n_delays)

        # Prepare input data for the current timestep
        X1 = S[:timestep-1].T
        X2 = S[1:timestep].T

        model = pk.Koopman(observables=obs, regressor=regressor)
        model.fit(X1.T, y=X2.T) 

        n_steps = timestep - n_delays + pred
        x0_td = X1[:,:n_delays+1].T

        Xkoop = model.simulate(x0_td, n_steps=n_steps)
        Xkoop2 = np.vstack([x0_td, Xkoop]) # add initial condition to simulated data for comparison below

    except ValueError as ve:
        print("Timestep:", timestep, "X1 shape:", X1.shape)
        print(ve)
        continue
        ```
        
```Timestep: 401 X1 shape: (488, 400)
x has too few rows. To compute time-delay features with delay = 1 and n_delays = 100 x must have at least 101 rows.```

It happens at timestep 301 too.
All other timesteps complete fine. 

99 time delays seems fine. Delays >100 error.

moonlians avatar Jul 29 '23 19:07 moonlians

could you please provide the data as well so maybe I can replicate this issue?

pswpswpsw avatar Feb 08 '24 05:02 pswpswpsw

f.csv

Thanks. You can use this but any array >200 should work. Delays >100 produce errors. If it's possible to fix it would be very helpful

liano159 avatar Mar 18 '24 02:03 liano159

I'm running into the same error. The n_delays seems to arbitrarily throw error for valid scenarios. E.g. If I have a trajectory of length 200, i can do 198 delays. However, if I extend the trajectory to 400, then I can no longer do 198 delays

niniack avatar Jun 27 '24 10:06 niniack

Can I ask, is this likely to be fixed soon? It's been a year. I am now running out of time on my project. It's a shame because I otherwise get solid results.

liano159 avatar Jul 09 '24 03:07 liano159