Skip to content

Commit

Permalink
Merge pull request #158 from PrimozGodec/poly-regression-warning
Browse files Browse the repository at this point in the history
[ENH] Polynomial regression - warn about huge values
  • Loading branch information
janezd committed Apr 4, 2023
2 parents 474dcf6 + 9da80d1 commit 2061612
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 13 additions & 4 deletions orangecontrib/educational/widgets/owpolynomialregression.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@ class Outputs(OWBaseLearner.Outputs):
want_main_area = True
graph_name = 'plot'

class Warning(OWBaseLearner.Warning):
large_diffs = Msg(
"Polynomial feature values are very large. "
"This may cause numerical instabilities."
)

class Error(OWBaseLearner.Error):
all_none = \
Msg("All rows have undefined data.")
all_none = Msg("All rows have undefined data.")
no_cont_variables =\
Msg("Regression requires at least two numeric variables.")
same_dep_indepvar =\
Expand Down Expand Up @@ -418,6 +423,7 @@ def error_and_clear(error=None):

self.Error.all_none.clear()
self.Error.same_dep_indepvar.clear()
self.Warning.large_diffs.clear()
if self.data is None:
error_and_clear()
return
Expand All @@ -443,11 +449,14 @@ def error_and_clear(error=None):
poly_learner.name = self.learner_name

data_table, preprocessed_table, poly_preprocessor, \
expanded_data, predictor = \
poly_learner.data_and_model(self.data)
expanded_data, predictor = poly_learner.data_and_model(self.data)
if preprocessed_table is None:
error_and_clear(self.Error.all_none)
return
if expanded_data.X.max() - expanded_data.X.min() > 1e14:
# the threshold defined with experimenting, instability typically
# started to have effects for values >= 1e15
self.Warning.large_diffs()

model = None
if hasattr(predictor, "model"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ def test_coefficients(self):
# I haven't computed these values manually, I just copied them
np.testing.assert_almost_equal(coef.X.T, [[ 3.1052632, -0.2631579]])

def test_large_polynomial_values(self):
x, y = (ContinuousVariable(n) for n in "xy")
data = Table.from_numpy(Domain([x], y), [[1], [30]], [3, 5])
self.send_signal(self.widget.Inputs.data, data)

self.widget.controls.polynomialexpansion.setValue(9)
self.assertFalse(self.widget.Warning.large_diffs.is_shown())
self.widget.controls.polynomialexpansion.setValue(10)
self.assertTrue(self.widget.Warning.large_diffs.is_shown())
self.widget.controls.polynomialexpansion.setValue(9)
self.assertFalse(self.widget.Warning.large_diffs.is_shown())


class PolynomialFeaturesTest(unittest.TestCase):
def test_1d(self):
Expand Down

0 comments on commit 2061612

Please sign in to comment.