def test_sparse_basket(self): current_dir = os.path.dirname(__file__) dataset = os.path.join(current_dir, "iris_basket.basket") table = BasketReader().read_file(dataset) test = Orange.data.Table.from_table_rows(table, range(0, len(table), 2)) train = Orange.data.Table.from_table_rows(table, range(1, len(table), 2)) learn = DummyMulticlassLearner() clf = learn(train) p = clf(test) self.assertEqual(p.shape, test.Y.shape) p = clf(test.X) self.assertEqual(p.shape, test.Y.shape)
def test_probs_from_value(self): nrows = 100 ncols = 5 x = np.random.randint(0, 2, (nrows, ncols)) # single class variable y = np.random.randint(0, 2, (nrows, 1)) d = Domain( [ DiscreteVariable("v" + str(i), values=[str(v) for v in np.unique(x[:, i])]) for i in range(ncols) ], DiscreteVariable("c", values="12"), ) t = Table(d, x, y) learn = DummyLearner() clf = learn(t) clf.ret = Model.Value y2 = clf(x, ret=Model.Probs) self.assertEqual(y2.shape, (nrows, 2)) y2, probs = clf(x, ret=Model.ValueProbs) self.assertEqual(y2.shape, (nrows, )) self.assertEqual(probs.shape, (nrows, 2)) # multitarget y = np.random.randint(1, 6, (nrows, 2)) y[:, 0] = y[:, 0] // 3 # majority = 1 y[:, 1] = (y[:, 1] + 4) // 3 - 1 # majority = 1 domain = Domain( [ContinuousVariable("i" + str(i)) for i in range(ncols)], [ DiscreteVariable("c" + str(i), values="0123") for i in range(y.shape[1]) ], ) t = Table(domain, x, y) learn = DummyMulticlassLearner() clf = learn(t) clf.ret = Model.Value probs = clf(x, ret=Model.Probs) self.assertEqual(probs.shape, (nrows, 2, 4)) y2, probs = clf(x, ret=Model.ValueProbs) self.assertEqual(y2.shape, y.shape) self.assertEqual(probs.shape, (nrows, 2, 4))
def test_probs_from_value(self): nrows = 100 ncols = 5 x = np.random.random_integers(0, 1, (nrows, ncols)) # single class variable y = np.random.random_integers(0, 1, (nrows, 1)) t = Table( Domain([ DiscreteVariable('v' + str(i), values=np.unique(x[:, i])) for i in range(ncols) ], DiscreteVariable('c', values=[1, 2])), x, y) learn = DummyLearner() clf = learn(t) clf.ret = Model.Value y2 = clf(x, ret=Model.Probs) self.assertEqual(y2.shape, (nrows, 2)) y2, probs = clf(x, ret=Model.ValueProbs) self.assertEqual(y2.shape, (nrows, )) self.assertEqual(probs.shape, (nrows, 2)) # multitarget y = np.random.random_integers(1, 5, (nrows, 2)) y[:, 0] = y[:, 0] // 3 # majority = 1 y[:, 1] = (y[:, 1] + 4) // 3 - 1 # majority = 1 domain = Domain( [ContinuousVariable('i' + str(i)) for i in range(ncols)], [ DiscreteVariable('c' + str(i), values=range(4)) for i in range(y.shape[1]) ]) t = Table(domain, x, y) learn = DummyMulticlassLearner() clf = learn(t) clf.ret = Model.Value probs = clf(x, ret=Model.Probs) self.assertEqual(probs.shape, (nrows, 2, 4)) y2, probs = clf(x, ret=Model.ValueProbs) self.assertEqual(y2.shape, y.shape) self.assertEqual(probs.shape, (nrows, 2, 4))