def test_loo(self): train, test = get_instance(Table('iris'), 0) loocp = LOOClassifier(InverseProbability(LogisticRegressionLearner()), train) pred = loocp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa']) train, test = get_instance(Table('housing'), 0) loocr = LOORegressor(AbsError(LinearRegressionLearner()), train) lo, hi = loocr(test.x, 0.1) self.assertLess(hi - lo, 20)
def test_inverse_probability(self): tab = Table('iris') train, test = get_instance(tab, 0) tcp = TransductiveClassifier(InverseProbability(NaiveBayesLearner()), train) pred = tcp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa']) train, test = get_instance(tab, 0) tcp = TransductiveClassifier(InverseProbability(NaiveBayesLearner())) tcp.fit(train) pred = tcp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa'])
def test_validate_transductive(self): tab = Table('iris') eps = 0.1 correct, num, all = 0, 0, len(tab) for i in range(all): train, test = get_instance(tab, i) tcp = TransductiveClassifier( InverseProbability(LogisticRegressionLearner()), train) pred = tcp(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_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_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_SVM(self): iris = Table('iris') tab = Table(iris.X[50:], iris.Y[50:] - 1) # versicolor, virginica # clear cases train, test = get_instance(tab, 30) train, calibrate = next(RandomSampler(train, 2, 1)) icp = InductiveClassifier(SVMDistance(skl_svm.SVC()), train, calibrate) pred = icp(test.x, 0.1) self.assertEqual(pred, ['v1']) train, test = get_instance(tab, 85) train, calibrate = next(RandomSampler(train, 2, 1)) icp = InductiveClassifier(SVMDistance(skl_svm.SVC()), train, calibrate) pred = icp(test.x, 0.1) self.assertEqual(pred, ['v2']) # border case train, test = get_instance(tab, 27) train, calibrate = next(RandomSampler(train, 2, 1)) icp = InductiveClassifier(SVMDistance(skl_svm.SVC()), train, calibrate) pred = icp(test.x, 0.2) self.assertEqual(pred, []) pred = icp(test.x, 0.01) self.assertEqual(pred, ['v1', 'v2'])
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 test_nearest_neighbours(self): tab = Table('iris') train, test = get_instance(tab, 0) tcp = TransductiveClassifier(KNNDistance(Euclidean), train) pred = tcp(test.x, 0.1) self.assertEqual(pred, ['Iris-setosa'])
def setUp(self): self.train, self.test = get_instance(Table('housing'), 0) self.train, self.calibrate = split_data(shuffle_data(self.train), 2, 1)