Skip to content

Commit

Permalink
Fix impute.Model for derived domains
Browse files Browse the repository at this point in the history
The compute_value was missing transformation into the variable space it
was working upon.
  • Loading branch information
markotoplak committed Dec 7, 2023
1 parent ab868dc commit 53429a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 3 additions & 4 deletions Orange/preprocess/impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def copy(self):
return FixedValueByType(*self.defaults.values())


class ReplaceUnknownsModel(Reprable):
class ReplaceUnknownsModel(Transformation):
"""
Replace unknown values with predicted values using a `Orange.base.Model`
Expand All @@ -185,15 +185,14 @@ class ReplaceUnknownsModel(Reprable):
"""
def __init__(self, variable, model):
assert model.domain.class_var == variable
self.variable = variable
super().__init__(variable)
self.model = model

def __call__(self, data):
if isinstance(data, Orange.data.Instance):
data = Orange.data.Table.from_list(data.domain, [data])
domain = data.domain
column = data.get_column(self.variable, copy=True)

column = data.transform(self._target_domain).get_column(self.variable, copy=True)
mask = np.isnan(column)
if not np.any(mask):
return column
Expand Down
23 changes: 22 additions & 1 deletion Orange/tests/test_impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from Orange import preprocess
from Orange.preprocess import impute, SklImpute
from Orange import data
from Orange.data import Unknown, Table
from Orange.data import Unknown, Table, Domain

from Orange.classification import MajorityLearner, SimpleTreeLearner
from Orange.regression import MeanLearner
Expand Down Expand Up @@ -293,6 +293,27 @@ def test_bad_domain(self):
self.assertRaises(ValueError, imputer, data=table,
variable=table.domain[0])

def test_missing_imputed_columns(self):
housing = Table("housing")

learner = SimpleTreeLearner(min_instances=10, max_depth=10)
method = preprocess.impute.Model(learner)

ivar = method(housing, housing.domain.attributes[0])
imputed = housing.transform(
Domain([ivar],
housing.domain.class_var)
)
removed_imputed = imputed.transform(
Domain([], housing.domain.class_var))

r = removed_imputed.transform(imputed.domain)

no_class = removed_imputed.transform(Domain(removed_imputed.domain.attributes, None))
impute_model_prediction_for_unknowns = ivar.compute_value.model(no_class[0])

np.testing.assert_equal(r.X, impute_model_prediction_for_unknowns)


class TestRandom(unittest.TestCase):
def test_replacement(self):
Expand Down

0 comments on commit 53429a2

Please sign in to comment.