Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix impute.Model for derived domains #6668

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading