dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.46k stars 324 forks source link

LinearRegression unknown keyword normalize #506

Closed huangtu577 closed 4 months ago

huangtu577 commented 6 months ago

I just installed the PySINDy library, and it throws an TypeError: LinearRegression.__init__() got an unexpected keyword argument 'normalize' out of the box. The version that conda installed for scikit-learn without any specification is 1.4.2. According to the Changelog from scikit-learn , the normalize keyword has been deprecated since version 1.0 and removed since 1.2. However there is a proposed fix in the above linked changelog.

Reproducing code example:

import pysindy
differentiation_method = ps.FiniteDifference(order=2)
feature_library = ps.PolynomialLibrary(degree=3)
optimizer = ps.STLSQ(threshold=0.2)

Error message:


TypeError                                 Traceback (most recent call last)
Cell In[15], [line 3](vscode-notebook-cell:?execution_count=15&line=3)
      [1](vscode-notebook-cell:?execution_count=15&line=1) differentiation_method = ps.FiniteDifference(order=2)
      [2](vscode-notebook-cell:?execution_count=15&line=2) feature_library = ps.PolynomialLibrary(degree=3)
----> [3](vscode-notebook-cell:?execution_count=15&line=3) optimizer = ps.STLSQ(threshold=0.2)

File ~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:105, in STLSQ.__init__(self, threshold, alpha, max_iter, ridge_kw, normalize, fit_intercept, copy_X, initial_guess)
     [94](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:94) def __init__(
     [95](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:95)     self,
     [96](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:96)     threshold=0.1,
   (...)
    [103](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:103)     initial_guess=None,
    [104](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:104) ):
--> [105](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:105)     super(STLSQ, self).__init__(
    [106](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:106)         max_iter=max_iter,
    [107](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:107)         normalize=normalize,
    [108](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:108)         fit_intercept=fit_intercept,
    [109](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:109)         copy_X=copy_X,
    [110](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:110)     )
    [112](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:112)     if threshold < 0:
    [113](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/stlsq.py:113)         raise ValueError("threshold cannot be negative")

File ~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:81, in BaseOptimizer.__init__(self, max_iter, normalize, fit_intercept, initial_guess, copy_X)
     [73](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:73) def __init__(
     [74](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:74)     self,
     [75](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:75)     max_iter=20,
   (...)
     [79](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:79)     copy_X=True,
     [80](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:80) ):
---> [81](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:81)     super(BaseOptimizer, self).__init__(
     [82](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:82)         fit_intercept=fit_intercept, normalize=normalize, copy_X=copy_X
     [83](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:83)     )
     [85](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:85)     if max_iter <= 0:
     [86](https://file+.vscode-resource.vscode-cdn.net/home/fabian/Documents/Studium/rebound/~/anaconda3/envs/rebound/lib/python3.10/site-packages/pysindy/optimizers/base.py:86)         raise ValueError("max_iter must be positive")

TypeError: LinearRegression.__init__() got an unexpected keyword argument 'normalize'

PySINDy/Python version information:

1.3.0 3.10.14 (main, May 6 2024, 19:42:50) [GCC 11.2.0]

Jacob-Stevens-Haas commented 6 months ago

Thank you!! This is a great find. I'll get on it soon. Since SINDy.model is a Pipeline, this should be a reasonably easy fix

ggmirandac commented 6 months ago

Hi,

I just wanted to mention that the version that you have on Conda-forge is not up to date, and results on the same issues.

Just that.

ddeboerfluid commented 4 months ago

Hi,

Is there any (quick) fix to this available?

Jacob-Stevens-Haas commented 4 months ago

@ddeboerfluid The quick fix is to install an updated version, either from pypi or here. Actually, it looks like this bug was fixed at least two years ago, which is where the blame of the most recent normalization code points (b9fc48c2).

We'll be releasing a new version shortly, but you can always get the current version with

pip install git+https://github.com/dynamicslab/pysindy

@ggmirandac, it was only put on conda-forge a few years ago manually by a previous maintainer. I don't personally use conda, but I would be happy for a PR into .github/workflows/release.yml to automate that release.

Dipa-chiniyaa commented 3 months ago

from sklearn.linear_model import LinearRegression

lr = LinearRegression()

can also works