diff --git a/Orange/statistics/contingency.py b/Orange/statistics/contingency.py index 9905a52eb43..47fd7e3ec38 100644 --- a/Orange/statistics/contingency.py +++ b/Orange/statistics/contingency.py @@ -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 diff --git a/Orange/tests/test_contingency.py b/Orange/tests/test_contingency.py index 6f7ccfca2f3..2f64bbed698 100644 --- a/Orange/tests/test_contingency.py +++ b/Orange/tests/test_contingency.py @@ -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") @@ -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)