Skip to content

Commit c1b46b9

Browse files
committed
Contingency add a property for getting all values including unknowns
1 parent ccee167 commit c1b46b9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Orange/statistics/contingency.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ def from_data(self, data, col_variable, row_variable=None):
225225
"Fallback method for computation of contingencies is not implemented yet"
226226
)
227227

228+
@property
229+
def array_with_unknowns(self):
230+
"""
231+
This function returns the list of all items returned by __getitem__
232+
with adding a row of row_unknowns together with values.
233+
"""
234+
other_rows = [x for x in self]
235+
ind = self.row_unknowns > 0
236+
unknown_rows = np.vstack((self.values[ind], self.row_unknowns[ind]))
237+
return other_rows + [unknown_rows]
238+
228239
def __eq__(self, other):
229240
return (
230241
np.array_equal(self.values, other.values) and

Orange/tests/test_contingency.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ def test_continuous_missing(self):
165165
3., 4., 2., 1., 1., 1., 1.])
166166
self.assertEqual(cont.unknowns, 1)
167167

168+
def test_continuous_array_with_unknowns(self):
169+
"""
170+
Test array_with_unknowns function
171+
"""
172+
d = data.Table("iris")
173+
d.Y[:50] = np.zeros(50) * float("nan")
174+
cont = contingency.Continuous(d, "sepal width")
175+
correct_row_unknowns = [0., 0., 1., 0., 0., 0., 0., 0., 1., 6., 5., 5.,
176+
2., 9., 6., 2., 3., 4., 2., 1., 1., 1., 1.]
177+
correct_row_unknowns_no_zero = [
178+
c for c in correct_row_unknowns if c > 0]
179+
correct_values_no_zero = [
180+
v for v, c in zip(cont.values, correct_row_unknowns) if c > 0]
181+
182+
np.testing.assert_almost_equal(cont.row_unknowns, correct_row_unknowns)
183+
arr_unknowns = cont.array_with_unknowns
184+
np.testing.assert_almost_equal(
185+
arr_unknowns[-1][1], correct_row_unknowns_no_zero)
186+
np.testing.assert_almost_equal(
187+
arr_unknowns[-1][0], correct_values_no_zero)
188+
189+
# check if other match to what we get with __getitem__
190+
for v1, v2 in zip(arr_unknowns[:-1], cont):
191+
np.testing.assert_almost_equal(v1, v2)
192+
168193
def test_mixedtype_metas(self):
169194
import Orange
170195
zoo = Orange.data.Table("zoo")
@@ -212,7 +237,6 @@ def _construct_sparse():
212237
Y = np.array([[1, 2, 1, 0, 0]]).T
213238
return data.Table.from_numpy(domain, X, Y)
214239

215-
216240
def test_sparse(self):
217241
d = self._construct_sparse()
218242
cont = contingency.Discrete(d, 5)

0 commit comments

Comments
 (0)