def test_validate_cross_regression(self): tab = shuffle_data(Table('housing')) eps = 0.1 correct, num, all = 0, 0, len(tab) for i in range(all): train, test = get_instance(tab, i) ccr = CrossRegressor(AbsError(LinearRegressionLearner()), 5, shuffle_data(train)) y_min, y_max = ccr(test.x, eps) if y_min <= test.y <= y_max: correct += 1 num += y_max - y_min self.assertAlmostEqual(correct / all, 1.0 - eps, delta=0.02)
def test_validate_AbsErrorKNN(self): eps = 0.1 correct, num, all = 0, 0, 0 for it in range(10): train, test = split_data(shuffle_data(Table('housing')), 4, 1) train, calibrate = split_data(shuffle_data(train), 3, 1) icr = InductiveRegressor( AbsErrorKNN(Euclidean, 10, average=True, variance=True), train, calibrate) for i, inst in enumerate(test): y_min, y_max = icr(inst.x, eps) if y_min <= inst.y <= y_max: correct += 1 num += y_max - y_min all += 1 print(correct / all, num / all) self.assertAlmostEqual(correct / all, 1.0 - eps, delta=0.03)
def test_run_train_test(self): tab = shuffle_data(Table('iris')) cp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 4) r = run_train_test(cp, 0.1, tab[:100], tab[100:]) cp = InductiveClassifier( InverseProbability(LogisticRegressionLearner())) r = run_train_test(cp, 0.1, tab[:50], tab[100:], tab[50:100])
def test_validate_cross_classification(self): tab = shuffle_data(Table('iris')) eps = 0.1 correct, num, all = 0, 0, len(tab) for i in range(all): train, test = get_instance(tab, i) ccp = CrossClassifier(InverseProbability(NaiveBayesLearner()), 5, train) pred = ccp(test.x, eps) if test.get_class() in pred: correct += 1 num += len(pred) self.assertAlmostEqual(correct / all, 1.0 - eps, delta=0.02)
def test_validate_regression(self): tab = Table('housing') eps = 0.1 correct, num, all = 0, 0, len(tab) for i in range(all): train, test = get_instance(tab, i) train, calibrate = split_data(shuffle_data(train), 2, 1) icr = InductiveRegressor(AbsError(LinearRegressionLearner()), train, calibrate) y_min, y_max = icr(test.x, eps) if y_min <= test.y <= y_max: correct += 1 num += y_max - y_min self.assertAlmostEqual(correct / all, 1.0 - eps, delta=0.02)
def test_validate_inductive(self): eps = 0.1 correct, num, all = 0, 0, len(self.tab) for i in range(all): train, test = get_instance(self.tab, i) train, calibrate = split_data(shuffle_data(train), 2, 1) icp = InductiveClassifier( InverseProbability(LogisticRegressionLearner()), train, calibrate) pred = icp(test.x, eps) if test.get_class() in pred: correct += 1 num += len(pred) self.assertAlmostEqual(correct / all, 1.0 - eps, delta=0.01)
def test_individual_classification(self): train, test = get_instance(Table('iris'), 123) # borderline case train = shuffle_data(train) ccp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 3, train) pred = ccp.predict(test.x) cred, conf = pred.credibility(), pred.confidence() self.assertLess(cred, 0.5) d = 1e-6 self.assertEqual(len(ccp(test.x, 1 - (conf - d))), 1) self.assertGreater(len(ccp(test.x, 1 - (conf + d))), 1) self.assertEqual(len(ccp(test.x, (cred + d))), 0) self.assertGreater(len(ccp(test.x, (cred - d))), 0)
def test_cross_classification(self): tab = Table('iris') train, test = get_instance(tab, 0) train = shuffle_data(train) ccp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 3, train) pred = ccp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa']) ccp = CrossClassifier(InverseProbability(LogisticRegressionLearner()), 3) ccp.fit(train) pred = ccp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa'])
def setUp(self): self.tab = Table('iris') train, self.test = get_instance(self.tab, 0) self.train, self.calibrate = split_data(shuffle_data(train), 2, 1)
def setUp(self): self.train, self.test = get_instance(Table('housing'), 0) self.train, self.calibrate = split_data(shuffle_data(self.train), 2, 1)