MilesCranmer / PySR

High-Performance Symbolic Regression in Python and Julia
https://astroautomata.com/PySR
Apache License 2.0
2.09k stars 197 forks source link

[BUG]: TypeError: _check_feature_names_in() got an unexpected keyword argument 'generate_names' #392

Closed noneaddress closed 11 months ago

noneaddress commented 11 months ago

What happened?

I use anaconda to manage my Python pkgs. But conda install -c conda-forge pysr failed to install pysr because it spent lots of time on solving environment. Thus I installed Julia and pysr with pip in conda prompt. However, when running demos in Quickstart section of README.md, an error occurred. TypeError: _check_feature_names_in() got an unexpected keyword argument 'generate_names'

Version

0.15.0

Operating System

Windows

Package Manager

pip

Interface

IPython Terminal

Relevant log output

raceback (most recent call last):
  File "E:/PCprofessional/PyCharm 2022.2.3/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "E:\PCprofessional\PyCharm 2022.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "F:\MasterDegreeCode\EnDeCoder\symboltest.py", line 17, in <module>
    model.fit(X, y)
  File "E:\CondaFolder\envs\Symbol\lib\site-packages\pysr\sr.py", line 1910, in fit
    X, y, Xresampled, weights, variable_names, X_units, y_units
  File "E:\CondaFolder\envs\Symbol\lib\site-packages\pysr\sr.py", line 1442, in _validate_and_set_fit_params
    self, variable_names, generate_names=False
TypeError: _check_feature_names_in() got an unexpected keyword argument 'generate_names'

Extra Info

Here is the script.

import numpy as np
import pysr
from pysr import PySRRegressor
#%%
X = 2 * np.random.randn(100, 5)
y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5

from pysr import PySRRegressor

model = PySRRegressor(
    niterations=40,  # < Increase me for better results
    binary_operators=["+", "*"],
    unary_operators=[
        "cos",
        "exp",
        "sin",
        "inv(x) = 1/x",
        # ^ Custom operator (julia syntax)
    ],
    extra_sympy_mappings={"inv": lambda x: 1 / x},
    # ^ Define operator for SymPy as well
    loss="loss(prediction, target) = (prediction - target)^2",
    # ^ Custom loss function (julia syntax)
)

model.fit(X, y)

print(model)

I created a new anaconda env and installed pysr in it. Both the envs throw the same error. I have run pysr.install() in these two envs before running the script above. I used Debug in Pycharm and found that function _check_feature_names_in defined in \Lib\site-packages\sklearn\utils\validation.py only has input_features=None. When generate_names=False in _check_feature_names_in(line 1441 in \Lib\site-packages\pysr\sr.py) is commented, the script runs normally. And I got the following results.

complexity loss score equation sympy_format lambda_format
0 1 37.918518 0.0 4.501644 4.50164400000000 PySRFunction(X=>4.50164400000000)
1 3 2.887665 1.287495688636072 (x0 * x0) x0**2 PySRFunction(X=>x0**2)
2 5 2.8626113 0.0043569834259625665 (x0 (x0 0.97915745)) 0.97915745*x0**2 PySRFunction(X=>0.97915745*x0**2)
3 6 1.1332736 0.9266238145378896 ((x0 * x0) + cos(x3)) x0**2 + cos(x3) PySRFunction(X=>x0**2 + cos(x3))
4 8 0.23931366 0.777545319110213 ((x0 x0) + (cos(x3) 2.3865998)) x0*2 + 2.3865998cos(x3) PySRFunction(X=>x0*2 + 2.3865998cos(x3))
5 9 0.23632519 0.012566296155662461 ((x0 x0) + (sin(cos(x3)) 2.7109928)) x0*2 + 2.7109928sin(cos(x3)) PySRFunction(X=>x0*2 + 2.7109928sin(cos(x3)))
6 10 2.2233993e-13 27.692022469244964 ((x0 x0) + ((cos(x3) + -0.19699001) 2.5382)) x0*2 + 2.5382cos(x3) - 0.500000043382 PySRFunction(X=>x0*2 + 2.5382cos(x3) - 0.500000043382)
7 12 1.9617641e-13 0.0625965607621989 ((x0 x0) + ((cos(x3) (2.0843496 * 1.2177418)) + -0.50000006)) x0*2 + 2.53819963373328cos(x3) - 0.50000006 PySRFunction(X=>x0*2 + 2.53819963373328cos(x3) - 0.50000006)

I wonder whether there are something wrong with my pkgs or project structures. Looking forward to your reply.


Besides, when I add print(model.latex_table(precision=3, include_score=True)) at the end of the above script, an error occurred. TypeError: latex_table() got an unexpected keyword argument 'include_score'


I just started learning programming, and my English is not very proficient. Please forgive me if I have offended or made low-level mistakes.

Best, Ker

MilesCranmer commented 11 months ago

Thanks very much for the bug report. This looks like the scikit-learn version being installed is out-of-date, so I either need to implement a version requirement, or need to add a fallback method.

MilesCranmer commented 11 months ago

Let me know if you still run into any issues

noneaddress commented 11 months ago

Let me know if you still run into any issues

Hi Miles Cranmer. pysr runs normally after upgrading to 0.15.1 with pip. However, I encountered these warnings .

`UserWarning: `torch` was loaded before the Julia instance started. This may cause a segfault when running `PySRRegressor.fit`. To avoid this, please run `pysr.julia_helpers.init_julia()` *before* importing `torch`. For updates, see https://github.com/pytorch/pytorch/issues/78829
  "`torch` was loaded before the Julia instance started. "`

`E:\CondaFolder\envs\env_one\lib\site-packages\pysr\julia_helpers.py:209: UserWarning: Your system's Python library is static (e.g., conda), so precompilation will be turned off. For a dynamic library, try using `pyenv` and installing with `--enable-shared`: https://github.com/pyenv/pyenv/blob/master/plugins/python-build/README.md#building-with---enable-shared.
  "Your system's Python library is static (e.g., conda), so precompilation will be turned off. For a dynamic library, try using `pyenv` and installing with `--enable-shared`: https://github.com/pyenv/pyenv/blob/master/plugins/python-build/README.md#building-with---enable-shared."`

It provides detailed info, and I am confident to handle these. Thank you for developing pysr and fixing the bug so quickly~

MilesCranmer commented 11 months ago

Cool. Yes, those warnings are unrelated – feel free to address if you desire.