File: test_mean.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 (53 lines) | stat: -rw-r--r-- 1,567 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
# 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.regression import MeanLearner
from Orange.tests import test_filename


class TestMeanLearner(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.learn = MeanLearner()

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

        true_mean = np.average(y)
        x2 = np.random.randint(1, 4, (nrows, ncols))
        y2 = clf(x2)
        self.assertTrue(np.allclose(y2, true_mean))

    def test_weights(self):
        nrows = 100
        ncols = 10
        x = np.random.randint(1, 4, (nrows, ncols))
        y = np.random.randint(0, 5, (nrows, 1)) / 3.0
        heavy = 1
        w = ((y == heavy) * 123 + 1.0) / 124.0
        t = Table.from_numpy(None, x, y, W=w)
        clf = self.learn(t)

        expected_mean = np.average(y, weights=w)
        x2 = np.random.randint(1, 4, (nrows, ncols))
        y2 = clf(x2)
        self.assertTrue(np.allclose(y2, expected_mean))

    def test_empty(self):
        autompg = Table(test_filename('datasets/imports-85.tab'))
        clf = self.learn(autompg[:0])
        y = clf(autompg[0])
        self.assertEqual(y, 0)

    def test_discrete(self):
        iris = Table('iris')
        self.assertRaises(ValueError, self.learn, iris)