Skip to content

Commit

Permalink
Merge pull request #4217 from VesnaT/norm_attrs
Browse files Browse the repository at this point in the history
[FIX] Normalizer: Retain attributes of attributes
  • Loading branch information
janezd authored Nov 22, 2019
2 parents 747beb7 + c0f8a67 commit 6722bb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
20 changes: 5 additions & 15 deletions Orange/preprocess/normalize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from Orange.data import ContinuousVariable, Domain
from Orange.data import Domain
from Orange.statistics import distribution
from Orange.util import Reprable
from .preprocess import Normalize
Expand Down Expand Up @@ -51,25 +51,15 @@ def normalize_by_sd(self, dist, var):
compute_val = Norm(var, avg, 1 / sd)
else:
compute_val = Norm(var, 0, 1 / sd)

return ContinuousVariable(
var.name,
compute_value=compute_val,
sparse=var.sparse,
)
return var.copy(compute_value=compute_val)

def normalize_by_span(self, dist, var):
dma, dmi = (dist.max(), dist.min()) if dist.shape[1] else (np.nan, np.nan)
diff = dma - dmi
if diff < 1e-15:
diff = 1
if self.zero_based:
return ContinuousVariable(
var.name,
compute_value=Norm(var, dmi, 1 / diff),
sparse=var.sparse)
compute_val = Norm(var, dmi, 1 / diff)
else:
return ContinuousVariable(
var.name,
compute_value=Norm(var, (dma + dmi) / 2, 2 / diff),
sparse=var.sparse)
compute_val = Norm(var, (dma + dmi) / 2, 2 / diff)
return var.copy(compute_value=compute_val)
15 changes: 15 additions & 0 deletions Orange/tests/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ def test_datetime_normalization(self):
[0., '2003-07-23', 'a', 'b', -1., '?', 0., 'b', '?', 'b', 0],
[0., '1967-03-12', 'a', 'b', 1., 'b', -1.225, 'c', '?', 'c', 1]]
self.compare_tables(data_norm, solution)

def test_retain_vars_attributes(self):
data = Table("iris")
attributes = {"foo": "foo", "baz": 1}
data.domain.attributes[0].attributes = attributes
self.assertDictEqual(
Normalize(norm_type=Normalize.NormalizeBySD)(
data).domain.attributes[0].attributes, attributes)
self.assertDictEqual(
Normalize(norm_type=Normalize.NormalizeBySpan)(
data).domain.attributes[0].attributes, attributes)


if __name__ == "__main__":
unittest.main()

0 comments on commit 6722bb8

Please sign in to comment.