Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] model.score does not return correct values when using default r2_score #80

Closed
ruslanmustafin opened this issue Jun 30, 2020 · 1 comment
Assignees

Comments

@ruslanmustafin
Copy link

As per scikit-learn r2_score documentation, the function expects the arguments to be y_true followed by y_pred, however, in your implementation the order is reversed. Since R^2 is non-symmetric, it will produce a different score compared to correct order of arguments. This is hardly noticeable when predictions closely match the gt (score is close to 1.0), but will produce incorrect results otherwise.

Reproducing code example:

import numpy as np

from scipy.integrate import odeint
from sklearn.metrics import r2_score

import pysindy as ps

# Control input
def u_fun(t):
    return np.column_stack([np.sin(2 * t), t ** 2])

# Lorenz equations with control input
def lorenz_control(z, t):
    u = u_fun(t)
    return [
        10 * (z[1] - z[0]) + u[0, 0] ** 2,
        z[0] * (28 - z[2]) - z[1],
        z[0] * z[1] - 8 / 3 * z[2] - u[0, 1],
    ]

# Generate measurement data
dt = .002

t_train = np.arange(0, 10, dt)
x0_train = [-8, 8, 27]
x_train = odeint(lorenz_control, x0_train, t_train)
u_train = u_fun(t_train)

# Instantiate and fit the SINDYc model
model = ps.SINDy()
model.fit(x_train, u=u_train, t=dt)

print(model.score(x_train, u=u_train, t=dt))

x_dot_test_predicted = model.predict(x_train, u=u_train)
x_dot_test_computed = model.differentiate(x_train, t=dt)

print(r2_score(x_dot_test_computed, x_dot_test_predicted))

This should print 0.999999960658854 and 0.999999960658851 (sorry for not providing a better example, I would have to give away our internal dataset to reproduce the issue, but in our case the score changes from 0.41 to 0.05)

PySINDy/Python version information:

1.0.1 3.6.10 |Anaconda, Inc.| (default, Mar 25 2020, 23:51:54)
[GCC 7.3.0]

@ruslanmustafin ruslanmustafin changed the title [BUG] [BUG] model.score does not return correct values Jun 30, 2020
@ruslanmustafin ruslanmustafin changed the title [BUG] model.score does not return correct values [BUG] model.score does not return correct values when using default r2_score Jun 30, 2020
@briandesilva briandesilva self-assigned this Jun 30, 2020
@briandesilva
Copy link
Member

Thanks for pointing this out. The latest release, 1.0.2, fixes this issue.

jpcurbelo pushed a commit to jpcurbelo/pysindy_fork that referenced this issue May 9, 2024
Fix evaluation of validation set outside of training.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants