Skip to content

Commit

Permalink
Merge pull request #6668 from markotoplak/fix-impute-model
Browse files Browse the repository at this point in the history
[FIX] Fix impute.Model for derived domains
  • Loading branch information
markotoplak authored Dec 8, 2023
2 parents d5de749 + 0547537 commit ef3465d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
19 changes: 13 additions & 6 deletions Orange/preprocess/impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import scipy.sparse as sp

import Orange.data
from Orange.data.table import DomainTransformationError
from Orange.statistics import distribution, basic_stats
from Orange.util import Reprable
from .transformation import Transformation, Lookup
Expand Down Expand Up @@ -172,7 +173,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 +186,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 All @@ -203,10 +203,17 @@ def __call__(self, data):
data = data.transform(
Orange.data.Domain(domain.attributes, None, domain.metas)
)
predicted = self.model(data[mask])
column[mask] = predicted
try:
column[mask] = self.model(data[mask])
except DomainTransformationError:
# owpredictions showed error when imputing target using a Model
# based imputer (owpredictions removes the target before predicing)
pass
return column

def transform(self, c):
assert False, "abstract in Transformation, never used here"

def __eq__(self, other):
return type(self) is type(other) \
and self.variable == other.variable \
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))
model_prediction_for_unknowns = ivar.compute_value.model(no_class[0])

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


class TestRandom(unittest.TestCase):
def test_replacement(self):
Expand Down
3 changes: 3 additions & 0 deletions i18n/si.jaml
Original file line number Diff line number Diff line change
Expand Up @@ -2685,6 +2685,9 @@ preprocess/impute.py:
def `__call__`:
"'{}' has no values": false
"'{}' has an unknown distribution": false
class `ReplaceUnknownsModel`:
def `transform`:
abstract in Transformation, never used here: false
preprocess/normalize.py:
Normalizer: false
preprocess/preprocess.py:
Expand Down

0 comments on commit ef3465d

Please sign in to comment.