Description
Dear all,
sorry for reaching out to you again, after you already cleared up a few things with the WeakPDELibrary, but I encountered a problem with the Generlized library. Here, I am creating a large library only containing specific terms from a set of 6 different smaller libraries. I have a set of 5 variables, but not all variables are implemented in each Library. This works perfectly fine, with the use of the GeneralizedLibrary and I can fit the model normally. Here is the example on how I create the library (I suppose there is an easier way):
A_T=1
P_T=1
G_T=1
E_T=3
lf0 = [
lambda x : x,
]
lf0 = [
lambda x : x,
]
l0=ps.CustomLibrary(library_functions=lf0,function_names=lf0,include_bias=False)
lf1 = [
lambda x,y : x*y,
]
lf1 = [
lambda x,y : x+y,
]
l1=ps.CustomLibrary(library_functions=lf1,function_names=lf1,include_bias=True)
lf2=[
lambda x: A_T-x]
lf2_names=[
lambda x: 'A_T-'+x]
l2=ps.CustomLibrary(library_functions=lf2, function_names=lf2_names, include_bias=False)
lf3=[
lambda x: P_T-x]
lf3_names=[
lambda x: 'P_T-'+x]
l3=ps.CustomLibrary(library_functions=lf3, function_names=lf3_names, include_bias=False)
lf4=[
lambda x: G_T-x]
lf4_names=[
lambda x: 'G_T-'+x]
l4=ps.CustomLibrary(library_functions=lf4, function_names=lf4_names, include_bias=False)
lf5=[
lambda x,y: E_T-x-y]
lf5_names=[
lambda x,y: 'E_T-'+x+'-'+y]
l5=ps.CustomLibrary(library_functions=lf5, function_names=lf5_names, include_bias=False)
inputs_temp=np.tile([0,1,2,3,4],6)
inputs_per_library = np.reshape(inputs_temp, (6, 5))
inputs_per_library[2, :] = 0 # Just use the u input for generating the library
inputs_per_library[3, :] = 4 # Just use the y input for generating the library
inputs_per_library[4, :] = 3 # Just use the x input for generating the library
inputs_per_library[5, :3] = 3 # Just use the x and y input for generating the library
tensor_array=[[1,0,1,0,0,0],[1,0,0,1,0,0], [1,0,0,0,1,0], [1,0,0,0,0,1], [0,0,1,1,0,0], [0,0,1,0,1,0], [0,0,1,0,0,1],
[0,0,0,1,1,0],[0,0,0,1,0,1], [0,0,0,0,1,1]]
custom_library=ps.GeneralizedLibrary(libraries=[l0,l1,l2,l3,l4,l5],inputs_per_library=inputs_per_library, tensor_array=tensor_array)
The problem occurs when I try to run the ensemble method together with the generalized library as follows (x_noisy consists of 5 variables):
ensemble_optimizer = ps.STLSQ(threshold=0.03,alpha=lam)
custom_library=mass_conservation_library()
model = ps.SINDy(feature_names=feature_names, optimizer=ensemble_optimizer, feature_library=custom_library)
model.fit(x_noisy, t=dt, ensemble=True , n_models=n_models, quiet=True)
The error that I get is the following:
File "XXX.py", line 348, in <module>
model.fit(x_noisy_t, t=dt, ensemble=True , n_models=n_models, quiet=True)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\pysindy\pysindy.py", line 471, in fit
self.model.fit(x_ensemble, x_dot_ensemble)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\sklearn\pipeline.py", line 390, in fit
Xt = self._fit(X, y, **fit_params_steps)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\sklearn\pipeline.py", line 348, in _fit
X, fitted_transformer = fit_transform_one_cached(
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\joblib\memory.py", line 349, in __call__
return self.func(*args, **kwargs)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\sklearn\pipeline.py", line 893, in _fit_transform_one
res = transformer.fit_transform(X, y, **fit_params)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\sklearn\base.py", line 855, in fit_transform
return self.fit(X, y, **fit_params).transform(X)
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\pysindy\feature_library\generalized_library.py", line 202, in fit
fitted_libs = [
File "C:\XXX\Anaconda3\envs\mamc\lib\site-packages\pysindy\feature_library\generalized_library.py", line 203, in <listcomp>
lib.fit(x[:, np.unique(self.inputs_per_library_[i, :])], y)
IndexError: index 6 is out of bounds for axis 0 with size 6
The problem here is, that I tried to have a look at what is going on in the code, but changing something in there always breaks the GenerlizedLibrary script. Furthermore, I am confused that this did not happen when I ran the analysis with just the normal SINDy, because the library is the same for both. Maybe you can tell me if I am just doing something wrong, or what the issue could be. Thank you again in advance and I am looking forward to your answer,
Best wishes,
Bartosz