File: test_lda.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 (49 lines) | stat: -rw-r--r-- 1,523 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
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring

import unittest

import numpy as np

from Orange.preprocess import Continuize, Randomize
from Orange.projection import LDA
from Orange.data import Table


class TestLDA(unittest.TestCase):
    def test_lda(self):
        iris = Table('iris')
        n_components = 2
        lda = LDA(solver="eigen", n_components=n_components)
        model = lda(iris)
        transformed = model(iris)
        self.assertEqual(transformed.X.shape, (len(iris), n_components))
        self.assertEqual(transformed.Y.shape, (len(iris),))

    def test_transform_changed_domain(self):
        """
        1. Open data, apply some preprocessor, splits the data into two parts,
        use LDA on the first part, and then transform the second part.

        2. Open data, split into two parts, apply the same preprocessor and
        LDA only on the first part, and then transform the second part.

        The transformed second part in (1) and (2) has to be the same.
        """
        data = Table("iris")
        data = Randomize()(data)
        preprocessor = Continuize()
        lda = LDA()

        # normalize all
        ndata = preprocessor(data)

        model = lda(ndata[:75])
        result_1 = model(ndata[75:])

        # normalize only the "training" part
        ndata = preprocessor(data[:75])
        model = lda(ndata)
        result_2 = model(data[75:])

        np.testing.assert_almost_equal(result_1.X, result_2.X)