def test_value_from_probs(self): nrows = 100 ncols = 5 x = np.random.randint(0, 2, (nrows, ncols)) # single class variable y = np.random.randint(1, 4, (nrows, 1)) // 2 # majority = 1 t = Table(x, y) learn = DummyLearner() clf = learn(t) clf.ret = Model.Probs y2 = clf(x, ret=Model.Value) self.assertEqual(y2.shape, (nrows, )) 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 # majority = 2 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.Probs y2 = clf(x, ret=Model.Value) self.assertEqual(y2.shape, y.shape) 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(1, 2, (nrows, 1)) t = data.Table(x, y) learn = DummyLearner() clf = learn(t) clf.ret = Orange.classification.Model.Value y2 = clf(x, ret=Orange.classification.Model.Probs) self.assertTrue(y2.shape == (nrows, 3)) y2, probs = clf(x, ret=Orange.classification.Model.ValueProbs) self.assertTrue(y2.shape == (nrows, )) self.assertTrue(probs.shape == (nrows, 3)) # multitarget y = np.random.random_integers(1, 5, (nrows, 2)) y[:, 0] = y[:, 0] // 3 # majority = 1 y[:, 1] = (y[:, 1] + 4) // 3 # majority = 2 t = data.Table(x, y) learn = DummyMulticlassLearner() clf = learn(t) clf.ret = Orange.classification.Model.Value probs = clf(x, ret=Orange.classification.Model.Probs) self.assertEqual(probs.shape, (nrows, 2, 4)) y2, probs = clf(x, ret=Orange.classification.Model.ValueProbs) self.assertEqual(y2.shape, y.shape) self.assertEqual(probs.shape, (nrows, 2, 4))
def test_unsupported(self): nrows = 20 ncols = 10 x = np.random.random_integers(1, 3, (nrows, ncols)) # multiple class variables y = np.random.random_integers(10, 11, (nrows, 2)) t = Table(x, y) learn = DummyLearner() with self.assertRaises(TypeError): clf = learn(t) # single class variable y = np.random.random_integers(10, 11, (nrows, 1)) t = Table(x, y) learn = DummyLearner() clf = learn(t) z = clf(x) self.assertEqual(z.ndim, 1)
def test_unsupported(self): nrows = 20 ncols = 10 x = np.random.randint(1, 4, (nrows, ncols)) # multiple class variables y = np.random.randint(0, 2, (nrows, 2)) t = Table(x, y) learn = DummyLearner() # TODO: Errors raised from various data checks should be made consistent with self.assertRaises((ValueError, TypeError)): clf = learn(t) # single class variable y = np.random.randint(0, 2, (nrows, 1)) t = Table(x, y) learn = DummyLearner() clf = learn(t) z = clf(x) self.assertEqual(z.ndim, 1)
def test_single_class(self): rows = 10 attr = 3 vars = 1 class_var_domain = 20 self.prepareTable(rows, attr, vars, class_var_domain) y = np.random.randint(2, 6, (rows, vars)) * 2 t = Table(self.domain, self.x, y) learn = DummyLearner() clf = learn(t) z, p = clf(self.x, ret=Model.ValueProbs) self.assertEqual(p.shape, (rows, class_var_domain)) self.assertTrue(np.all(z == np.argmax(p, axis=-1)))
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))
def test_incompatible_domain(self): iris = Table("iris") titanic = Table("titanic") clf = DummyLearner()(iris) with self.assertRaises(DomainTransformationError): clf(titanic)