def test_target_in_attributes(self): """ If target in attributes precision is 1.0. """ self.problem.attributes = [self.target] self.this = self.classifier(self.corpus, self.problem) prec = evaluation.precision(self.this, self.test_set) self.assertEqual(prec, 1.0)
def test_better_than_majority(self): d = defaultdict(int) for example in self.corpus: d[self.target(example)] += 1 majority = max(d, key=d.get) class MockClassifier(object): def classify(self, example): return majority, 1.0 mock = MockClassifier() mock_prec = evaluation.precision(mock, self.target, self.test_set) this_prec = evaluation.precision(self.this, self.target, self.test_set) try: self.assertGreaterEqual(this_prec, mock_prec) except: print self.corpus
def test_tolerates_empty_attributes(self): self.problem.attributes = [] self.this = self.classifier(self.corpus, self.problem) evaluation.precision(self.this, self.test_set)
def test_bad_testset(self): test = [] with self.assertRaises(ValueError): precision(self.c, test)
def test_is_0(self): test = [(1, 3, 3), (1, 2, 2)] p = precision(self.c, test) self.assertEqual(p, 0.0)
def test_is_1(self): test = [(0, 3, 3), (0, 2, 2)] p = precision(self.c, test) self.assertEqual(p, 1.0)