Skip to content

Commit

Permalink
Merge pull request dynamicslab#193 from Jacob-Stevens-Haas/frols-lin-…
Browse files Browse the repository at this point in the history
…dependence-err

ENH: Raise explicit error when FROLS has linearly dependent columns.
  • Loading branch information
akaptano committed Jun 5, 2022
2 parents c8fdb43 + 192112a commit 1b48108
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pysindy/optimizers/frols.py
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from sklearn.linear_model import ridge_regression

Expand Down Expand Up @@ -104,7 +106,14 @@ def __init__(
self.verbose = verbose

def _normed_cov(self, a, b):
return np.vdot(a, b) / np.vdot(a, a)
with warnings.catch_warnings():
warnings.filterwarnings("error", category=RuntimeWarning)
try:
return np.vdot(a, b) / np.vdot(a, a)
except RuntimeWarning:
raise ValueError(
"Trying to orthogonalize linearly dependent columns created NaNs"
)

def _select_function(self, x, y, sigma, skip=[]):
n_features = x.shape[1]
Expand Down
8 changes: 8 additions & 0 deletions test/optimizers/test_optimizers.py
Expand Up @@ -1020,3 +1020,11 @@ def test_optimizers_verbose_cvxpy(data_lorenz, optimizer):
model = optimizer(verbose_cvxpy=True)
model.fit(x, x_dot)
check_is_fitted(model)


def test_frols_error_linear_dependence():
opt = FROLS(normalize_columns=True)
x = np.array([[1.0, 1.0]])
y = np.array([[1.0, 1.0]])
with pytest.raises(ValueError):
opt.fit(x, y)

0 comments on commit 1b48108

Please sign in to comment.