File: test_kernel_regression.py

package info (click to toggle)
astroml 1.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 932 kB
  • sloc: python: 5,731; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,460 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
import numpy as np
from numpy.testing import assert_allclose
import pytest
from astroML.linear_model import NadarayaWatson


def test_NW_simple():
    X = np.arange(11.)
    y = X + 1
    dy = 1

    # by symmetry, NW regression should get these exactly correct
    Xfit = np.array([4, 5, 6])[:, None]
    y_true = np.ravel(Xfit + 1)

    clf = NadarayaWatson(h=0.5).fit(X[:, None], y, dy)
    y_fit = clf.predict(Xfit)

    assert_allclose(y_fit, y_true)


def test_NW_simple_laplacian_kernel():
    X = np.arange(11.)
    y = X + 1
    dy = 1

    # by symmetry, NW regression should get these exactly correct
    Xfit = np.array([4, 5, 6])[:, None]
    y_true = np.ravel(Xfit + 1)

    kwargs = {'gamma': 10.}
    clf = NadarayaWatson(kernel='laplacian', **kwargs).fit(X[:, None], y, dy)
    y_fit = clf.predict(Xfit)

    assert_allclose(y_fit, y_true)


def test_X_invalid_shape_exception():
    X = np.arange(11.)
    y = X + 1
    dy = 1

    clf = NadarayaWatson(h=0.5).fit(X[:, None], y, dy)

    # not valid Xfit.shape[1], should raise an exception
    Xfit = np.array([[4, 5, 6], [1, 2, 3]])

    with pytest.raises(Exception) as e:
        clf.predict(Xfit)

    assert str(e.value) == "dimensions of X do not match training dimension"

    # not valid Xfit.shape[1], should raise an exception
    Xfit = np.array([4, 5, 6])

    with pytest.raises(Exception) as e:
        clf.predict(Xfit)

    assert str(e.value) == "X must be two-dimensional"