Skip to content

Commit

Permalink
BUG: Set ind_, intercept_, coef_ correctly in WrappedOptimizer
Browse files Browse the repository at this point in the history
Previously, The didn't play nice with what BaseOptimizer expected.
  • Loading branch information
Jacob-Stevens-Haas committed Jul 24, 2023
1 parent d9984a4 commit 705964a
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions pysindy/optimizers/wrapped_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ def _reduce(self, x, y):
):
raise AttributeError("optimizer does not have a callable predict method")

self.optimizer.fit(x, y)
if not hasattr(self.optimizer, "coef_"):
raise AttributeError("optimizer has no attribute coef_")
coef_shape = (y.shape[1], x.shape[1])
self.coef_ = np.zeros(coef_shape)
self.ind_ = np.ones(coef_shape)
for tgt in range(y.shape[-1]):
self.optimizer.fit(x, y[..., tgt])
self.coef_[tgt] = self.optimizer.coef_
self.ind_ = np.abs(self.coef_) > COEF_THRESHOLD
self.coef_ = self.optimizer.coef_
if self.unbias:
self._unbias(x, y)
if hasattr(self.optimizer, "intercept_"):
self.intercept_ = self.optimizer.intercept_
else:
self.intercept_ = 0.0
return self

def predict(self, x):
Expand All @@ -55,20 +61,6 @@ def predict(self, x):
else:
return prediction

@property
def coef_(self):
if self.optimizer.coef_.ndim == 1:
return self.optimizer.coef_[np.newaxis, :]
else:
return self.optimizer.coef_

@property
def intercept_(self):
if hasattr(self.optimizer, "intercept_"):
return self.optimizer.intercept_
else:
return 0.0

@property
def complexity(self):
return np.count_nonzero(self.coef_) + np.count_nonzero(self.intercept_)

0 comments on commit 705964a

Please sign in to comment.