Skip to content

Commit

Permalink
Contingency add a property for getting all values including unknowns
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Dec 22, 2019
1 parent ccee167 commit c1b46b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Orange/statistics/contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@ def from_data(self, data, col_variable, row_variable=None):
"Fallback method for computation of contingencies is not implemented yet"
)

@property
def array_with_unknowns(self):
"""
This function returns the list of all items returned by __getitem__
with adding a row of row_unknowns together with values.
"""
other_rows = [x for x in self]
ind = self.row_unknowns > 0
unknown_rows = np.vstack((self.values[ind], self.row_unknowns[ind]))
return other_rows + [unknown_rows]

def __eq__(self, other):
return (
np.array_equal(self.values, other.values) and
Expand Down
26 changes: 25 additions & 1 deletion Orange/tests/test_contingency.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ def test_continuous_missing(self):
3., 4., 2., 1., 1., 1., 1.])
self.assertEqual(cont.unknowns, 1)

def test_continuous_array_with_unknowns(self):
"""
Test array_with_unknowns function
"""
d = data.Table("iris")
d.Y[:50] = np.zeros(50) * float("nan")
cont = contingency.Continuous(d, "sepal width")
correct_row_unknowns = [0., 0., 1., 0., 0., 0., 0., 0., 1., 6., 5., 5.,
2., 9., 6., 2., 3., 4., 2., 1., 1., 1., 1.]
correct_row_unknowns_no_zero = [
c for c in correct_row_unknowns if c > 0]
correct_values_no_zero = [
v for v, c in zip(cont.values, correct_row_unknowns) if c > 0]

np.testing.assert_almost_equal(cont.row_unknowns, correct_row_unknowns)
arr_unknowns = cont.array_with_unknowns
np.testing.assert_almost_equal(
arr_unknowns[-1][1], correct_row_unknowns_no_zero)
np.testing.assert_almost_equal(
arr_unknowns[-1][0], correct_values_no_zero)

# check if other match to what we get with __getitem__
for v1, v2 in zip(arr_unknowns[:-1], cont):
np.testing.assert_almost_equal(v1, v2)

def test_mixedtype_metas(self):
import Orange
zoo = Orange.data.Table("zoo")
Expand Down Expand Up @@ -212,7 +237,6 @@ def _construct_sparse():
Y = np.array([[1, 2, 1, 0, 0]]).T
return data.Table.from_numpy(domain, X, Y)


def test_sparse(self):
d = self._construct_sparse()
cont = contingency.Discrete(d, 5)
Expand Down

0 comments on commit c1b46b9

Please sign in to comment.