Skip to content

Commit

Permalink
Merge pull request #4806 from thocevar/continuize
Browse files Browse the repository at this point in the history
[FIX] OWContinuize: Fix treatment of continuous features.
  • Loading branch information
janezd authored May 22, 2020
2 parents 69e95f2 + d5ac697 commit a0381fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Orange/widgets/data/owcontinuize.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,23 @@ def one_hot_coding(var):

def continuize_domain(data,
multinomial_treatment=Continuize.Indicators,
continuous_treatment=Continuize.Leave,
continuous_treatment=OWContinuize.Normalize.Leave,
class_treatment=Continuize.Leave):
domain = data.domain
def needs_dist(var, mtreat, ctreat):
"Does the `var` need a distribution given specified flags"
if var.is_discrete:
return mtreat == Continuize.FrequentAsBase
elif var.is_continuous:
return ctreat != Continuize.Leave
return ctreat != OWContinuize.Normalize.Leave
else:
raise ValueError

# Compute the column indices which need a distribution.
attr_needs_dist = [needs_dist(var, multinomial_treatment,
continuous_treatment)
for var in domain.attributes]
cls_needs_dist = [needs_dist(var, class_treatment, Continuize.Leave)
cls_needs_dist = [needs_dist(var, class_treatment, OWContinuize.Normalize.Leave)
for var in domain.class_vars]

columns = [i for i, needs in enumerate(attr_needs_dist + cls_needs_dist)
Expand All @@ -242,7 +242,7 @@ def needs_dist(var, mtreat, ctreat):
for var, needs_dist in zip(domain.attributes, attr_needs_dist)]
newclass = [continuize_var(var,
next(dist_iter) if needs_dist else None,
class_treatment, Continuize.Remove)
class_treatment, OWContinuize.Normalize.Leave)
for var, needs_dist in zip(domain.class_vars, cls_needs_dist)]

newattrs = reduce(list.__iadd__, newattrs, [])
Expand All @@ -253,13 +253,13 @@ def needs_dist(var, mtreat, ctreat):
def continuize_var(var,
data_or_dist=None,
multinomial_treatment=Continuize.Indicators,
continuous_treatment=Continuize.Leave):
continuous_treatment=OWContinuize.Normalize.Leave):
def continuize_continuous():
dist = _ensure_dist(var, data_or_dist)
dist = _ensure_dist(var, data_or_dist) if continuous_treatment != OWContinuize.Normalize.Leave else None
treatments = [lambda var, _: var,
normalize_by_sd, center_to_mean, divide_by_sd,
normalize_to_11, normalize_to_01]
if dist.shape[1] == 0:
if dist is not None and dist.shape[1] == 0:
return [var]
new_var = treatments[continuous_treatment](var, dist)
return [new_var]
Expand Down Expand Up @@ -365,7 +365,7 @@ def normalize_by_span(var, dist, zero_based=True):
class DomainContinuizer(Reprable):
def __init__(self,
multinomial_treatment=Continuize.Indicators,
continuous_treatment=Continuize.Leave,
continuous_treatment=OWContinuize.Normalize.Leave,
class_treatment=Continuize.Leave):
self.multinomial_treatment = multinomial_treatment
self.continuous_treatment = continuous_treatment
Expand Down
5 changes: 5 additions & 0 deletions Orange/widgets/data/tests/test_owcontinuize.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def test_summary(self):
output_sum.assert_called_once()
self.assertEqual(output_sum.call_args[0][0].brief, "")

def test_continuous(self):
table = Table("housing")
self.send_signal(self.widget.Inputs.data, table)
self.widget.unconditional_commit()

def test_one_column_equal_values(self):
"""
No crash on a column with equal values and with selected option
Expand Down

0 comments on commit a0381fb

Please sign in to comment.