File: test_svm.py

package info (click to toggle)
orange3 3.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,912 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (77 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring

import unittest
import warnings

import numpy as np
from sklearn.exceptions import ConvergenceWarning

from Orange.classification import SVMLearner, LinearSVMLearner, NuSVMLearner
from Orange.data import Table
from Orange.evaluation import CrossValidation, CA, RMSE
from Orange.regression import SVRLearner, NuSVRLearner
from Orange.tests import test_filename


class TestSVMLearner(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.data = Table(test_filename('datasets/ionosphere.tab'))
        with cls.data.unlocked():
            cls.data.shuffle()

    def test_SVM(self):
        learn = SVMLearner()
        cv = CrossValidation(k=2)
        res = cv(self.data, [learn])
        self.assertGreater(CA(res)[0], 0.9)

    def test_LinearSVM(self):
        # This warning is irrelevant here
        warnings.filterwarnings("ignore", ".*", ConvergenceWarning)
        learn = LinearSVMLearner()
        cv = CrossValidation(k=2)
        res = cv(self.data, [learn])
        self.assertGreater(CA(res)[0], 0.8)
        self.assertLess(CA(res)[0], 0.9)

    def test_NuSVM(self):
        learn = NuSVMLearner(nu=0.01)
        cv = CrossValidation(k=2)
        res = cv(self.data, [learn])
        self.assertGreater(CA(res)[0], 0.9)

    def test_SVR(self):
        nrows, ncols = 200, 5
        X = np.random.rand(nrows, ncols)
        y = X.dot(np.random.rand(ncols))
        data = Table.from_numpy(None, X, y)
        learn = SVRLearner(kernel='rbf', gamma=0.1)
        cv = CrossValidation(k=2)
        res = cv(data, [learn])
        self.assertLess(RMSE(res)[0], 0.15)

    def test_LinearSVR(self):
        nrows, ncols = 200, 5
        X = np.random.rand(nrows, ncols)
        y = X.dot(np.random.rand(ncols))
        data = Table.from_numpy(None, X, y)
        learn = SVRLearner()
        cv = CrossValidation(k=2)
        res = cv(data, [learn])
        self.assertLess(RMSE(res)[0], 0.15)

    def test_NuSVR(self):
        nrows, ncols = 200, 5
        X = np.random.rand(nrows, ncols)
        y = X.dot(np.random.rand(ncols))
        data = Table.from_numpy(None, X, y)
        learn = NuSVRLearner(kernel='rbf', gamma=0.1)
        cv = CrossValidation(k=2)
        res = cv(data, [learn])
        self.assertLess(RMSE(res)[0], 0.1)


if __name__ == "__main__":
    unittest.main()