Changeset 10641:787ea01b59dc in orange
 Timestamp:
 03/26/12 14:21:46 (2 years ago)
 Branch:
 default
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

Orange/testing/unit/tests/test_svm.py
r10617 r10641 1 try: 2 import unittest2 as unittest 3 except: 4 import unittest 5 1 6 import Orange 2 7 from Orange.classification.svm import SVMLearner, SVMLearnerSparse, \ … … 4 9 get_linear_svm_weights, \ 5 10 example_weighted_sum 6 11 from Orange.classification import svm 7 12 from Orange.classification.svm.kernels import BagOfWords, RBFKernelWrapper 8 13 from Orange.misc import testing … … 15 20 import numpy as np 16 21 17 def multiclass_from1 sv1(dec_values, class_var):22 def multiclass_from1vs1(dec_values, class_var): 18 23 n_class = len(class_var.values) 19 24 votes = [0] * n_class … … 60 65 prediction_1 = classifier_no_prob(inst) 61 66 d_val = classifier_no_prob.get_decision_values(inst) 62 prediciton_2 = multiclass_from1 sv1(d_val, classifier_no_prob.class_var)67 prediciton_2 = multiclass_from1vs1(d_val, classifier_no_prob.class_var) 63 68 self.assertEqual(prediction_1, prediciton_2) 64 69 … … 75 80 76 81 77 @test_on_datasets(datasets=testing.CLASSIFICATION_DATASETS + ["zoo"]) 78 def test_linear_weights_on(self, dataset): 82 # Don't test on "monks" the coefs are really large and 83 @test_on_datasets(datasets=["iris", "brownselected", "lenses", "zoo"]) 84 def test_linear_classifier_weights_on(self, dataset): 79 85 # Test get_linear_svm_weights 80 86 classifier = self.LEARNER(dataset) … … 91 97 92 98 l_map = classifier._get_libsvm_labels_map() 93 # Would need to map the rho values 94 if l_map == sorted(l_map): 95 for inst in dataset[:20]: 96 dec_values = classifier.get_decision_values(inst) 97 98 for dec_v, weight, rho, pair in zip(dec_values, weights, 99 classifier.rho, class_pairs(n_class)): 100 t_inst = Orange.data.Instance(classifier.domain, inst) 101 dec_v1 = example_weighted_sum(t_inst, weight)  rho 102 self.assertAlmostEqual(dec_v, dec_v1, 4) 103 104 99 100 for inst in dataset[:20]: 101 dec_values = classifier.get_decision_values(inst) 102 103 for dec_v, weight, rho, pair in zip(dec_values, weights, 104 classifier.rho, class_pairs(n_class)): 105 t_inst = Orange.data.Instance(classifier.domain, inst) 106 dec_v1 = example_weighted_sum(t_inst, weight)  rho 107 self.assertAlmostEqual(dec_v, dec_v1, 4) 108 109 @test_on_datasets(datasets=testing.REGRESSION_DATASETS) 110 def test_linear_regression_weights_on(self, dataset): 111 predictor = self.LEARNER(dataset) 112 weights = get_linear_svm_weights(predictor) 113 114 for inst in dataset[:20]: 115 t_inst = Orange.data.Instance(predictor.domain, inst) 116 prediction = predictor(inst) 117 w_sum = example_weighted_sum(t_inst, weights) 118 self.assertAlmostEqual(float(prediction), 119 w_sum  predictor.rho[0], 120 places=4) 121 122 105 123 @datasets_driven(datasets=datasets) 106 124 class PolySVMTestCase(testing.LearnerTestCase): … … 199 217 copy = cPickle.loads(cPickle.dumps(rfe)) 200 218 219 201 220 if __name__ == "__main__": 202 try:203 import unittest2 as unittest204 except:205 import unittest206 221 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.