Skip to content

Commit d0517b7

Browse files
authored
Merge pull request #193 from Jacob-Stevens-Haas/frols-lin-dependence-err
ENH: Raise explicit error when FROLS has linearly dependent columns.
2 parents 37350fe + a1d4b21 commit d0517b7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pysindy/optimizers/frols.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
from sklearn.linear_model import ridge_regression
35

@@ -104,7 +106,14 @@ def __init__(
104106
self.verbose = verbose
105107

106108
def _normed_cov(self, a, b):
107-
return np.vdot(a, b) / np.vdot(a, a)
109+
with warnings.catch_warnings():
110+
warnings.filterwarnings("error", category=RuntimeWarning)
111+
try:
112+
return np.vdot(a, b) / np.vdot(a, a)
113+
except RuntimeWarning:
114+
raise ValueError(
115+
"Trying to orthogonalize linearly dependent columns created NaNs"
116+
)
108117

109118
def _select_function(self, x, y, sigma, skip=[]):
110119
n_features = x.shape[1]

test/optimizers/test_optimizers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,3 +1020,11 @@ def test_optimizers_verbose_cvxpy(data_lorenz, optimizer):
10201020
model = optimizer(verbose_cvxpy=True)
10211021
model.fit(x, x_dot)
10221022
check_is_fitted(model)
1023+
1024+
1025+
def test_frols_error_linear_dependence():
1026+
opt = FROLS(normalize_columns=True)
1027+
x = np.array([[1.0, 1.0]])
1028+
y = np.array([[1.0, 1.0]])
1029+
with pytest.raises(ValueError):
1030+
opt.fit(x, y)

0 commit comments

Comments
 (0)