File: test_majority.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (91 lines) | stat: -rw-r--r-- 2,736 bytes parent folder | download | duplicates (2)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring

import unittest

import numpy as np

from Orange.data import Table
from Orange.classification import MajorityLearner
from Orange.tests import test_filename


class TestMajorityLearner(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.iris = Table('iris')
        cls.learn = MajorityLearner()

    def test_majority(self):
        nrows = 1000
        ncols = 10
        x = np.random.randint(1, 4, (nrows, ncols))
        y = np.random.randint(1, 4, (nrows, 1)) // 2
        t = Table.from_numpy(None, x, y)
        clf = self.learn(t)

        x2 = np.random.randint(1, 4, (nrows, ncols))
        y2 = clf(x2)
        self.assertEqual(y2.all(), 1)

    def test_weights(self):
        nrows = 100
        ncols = 10
        x = np.random.randint(1, 5, (nrows, ncols))
        y = np.array(70*[0] + 30*[1]).reshape((nrows, 1))
        heavy_class = 1
        w = (y == heavy_class) * 2 + 1
        t = Table.from_numpy(None, x, y, W=w)
        clf = self.learn(t)

        y2 = clf(x)
        self.assertEqual(y2.all(), heavy_class)

    def test_empty(self):
        clf = self.learn(self.iris[:0])
        y = clf(self.iris[0], clf.Probs)
        self.assertTrue(np.allclose(y, y.sum() / y.size))

    def test_missing(self):
        iris = Table('iris')
        learn = MajorityLearner()
        sub_table = iris[: len(iris) // 2: 2].copy()
        with sub_table.unlocked():
            for e in sub_table:
                e.set_class("?")
        clf = learn(iris)
        y = clf(iris)
        self.assertTrue((y == 2).all())

        with iris.unlocked():
            for e in iris:
                e.set_class("?")
        clf = learn(iris)
        y = clf(iris)
        self.assertEqual(y.all(), 1)

    def test_continuous(self):
        autompg = Table(test_filename('datasets/imports-85.tab'))
        learn = MajorityLearner()
        self.assertRaises(ValueError, learn, autompg)

    def test_returns_random_class(self):
        iris = self.iris
        train = np.ones((150,), dtype='bool')
        train[0] = False
        majority = MajorityLearner()(iris[train])
        pred1 = majority(iris[0])
        self.assertIn(pred1, [1, 2])

        for i in range(1, 50):
            train[i] = train[50 + i] = train[100 + i] = False
            majority = MajorityLearner()(iris[train])
            pred2 = majority(iris[0])
            self.assertIn(pred2, [1, 2])
            if pred1 != pred2:
                break
        else:
            self.fail("Majority always returns the same value.")

    def test_supports_weights(self):
        self.assertFalse(MajorityLearner().supports_weights)