Skip to content

Commit b398883

Browse files
committed
transformation.Lookup: Fix 'transform' (it never worked\!)
1 parent 8d17a1f commit b398883

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Orange/preprocess/transformation.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import scipy.sparse as sp
23

34
from Orange.data import Instance, Table, Domain
@@ -137,5 +138,9 @@ def __init__(self, variable, lookup_table):
137138
super().__init__(variable)
138139
self.lookup_table = lookup_table
139140

140-
def transform(self, c):
141-
return self.lookup_table[c]
141+
def transform(self, column):
142+
mask = np.isnan(column)
143+
column = column.astype(int)
144+
column[mask] = 0
145+
values = self.lookup_table[column]
146+
return np.where(mask, np.nan, values)

Orange/tests/test_transformation.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import unittest
2+
23
import numpy as np
34

45
from Orange.data import Table, Domain, DiscreteVariable, ContinuousVariable, \
56
StringVariable
6-
from Orange.preprocess.transformation import Identity, Transformation
7+
from Orange.preprocess.transformation import Identity, Transformation, Lookup
78

89

910
class TestTransformation(unittest.TestCase):
@@ -59,3 +60,12 @@ def test_identity(self):
5960
np.testing.assert_equal(D1.X, D.X)
6061
np.testing.assert_equal(D1.Y, D.Y)
6162
np.testing.assert_equal(D1.metas, D.metas)
63+
64+
65+
class LookupTest(unittest.TestCase):
66+
def test_transform(self):
67+
lookup = Lookup(None, np.array([1, 2, 0, 2]))
68+
column = np.array([1, 2, 3, 0, np.nan, 0], dtype=np.float64)
69+
np.testing.assert_array_equal(
70+
lookup.transform(column),
71+
np.array([2, 0, 2, 1, np.nan, 1], dtype=np.float64))

0 commit comments

Comments
 (0)