-
Notifications
You must be signed in to change notification settings - Fork 59
/
test_k_neighbors_regressor.py
74 lines (62 loc) · 2.8 KB
/
test_k_neighbors_regressor.py
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
import numpy as np
import warnings
from unittest import TestCase
from ezyrb import KNeighborsRegressor, Database, POD, ReducedOrderModel
class TestKNeighbors(TestCase):
def test_params(self):
reg = KNeighborsRegressor(n_neighbors=20, algorithm='kd_tree')
assert reg.regressor.get_params()['n_neighbors'] == 20
assert reg.regressor.get_params()['algorithm'] == 'kd_tree'
def test_fit_onescalarparam_scalarfunc(self):
reg = KNeighborsRegressor()
reg.fit([1], [20])
assert reg.regressor.n_samples_fit_ == 1
def test_fit_scalarparam_scalarfunc(self):
reg = KNeighborsRegressor()
reg.fit([1, 2, 5, 7, 2], [2, 5, 7, 83, 3])
assert reg.regressor.n_samples_fit_ == 5
def test_fit_biparam_scalarfunc(self):
reg = KNeighborsRegressor()
reg.fit([[1, 2], [6, 7], [8, 9]], [1, 5, 6])
assert reg.regressor.n_samples_fit_ == 3
def test_fit_biparam_bifunc(self):
reg = KNeighborsRegressor()
reg.fit([[1, 2], [6, 7], [8, 9]], [[1, 0], [20, 5], [8, 6]])
assert reg.regressor.n_samples_fit_ == 3
def test_kneighbors(self):
reg = KNeighborsRegressor(n_neighbors=2)
reg.fit([[1, 2], [6, 7], [8, 9]], [[1, 0], [20, 5], [8, 6]])
neigh_idx = reg.regressor.kneighbors([[6, 6]], return_distance=False)[0]
assert neigh_idx[0] == 1
assert neigh_idx[1] == 2
assert len(neigh_idx) == 2
def test_predict(self):
reg = KNeighborsRegressor(n_neighbors=1)
reg.fit([[1, 2], [6, 7], [8, 9]], [[1, 0], [20, 5], [8, 6]])
neigh_idx = reg.regressor.predict([[1,2], [8,9], [6,7]])
assert (neigh_idx[0] == [1,0]).all()
assert (neigh_idx[1] == [8,6]).all()
assert (neigh_idx[2] == [20,5]).all()
def test_with_db_predict(self):
reg = KNeighborsRegressor(n_neighbors=1)
pod = POD()
db = Database(np.array([1, 2, 3])[:,None], np.array([1, 5, 3])[:,None])
rom = ReducedOrderModel(db, pod, reg)
rom.fit()
assert rom.predict([1]) == 1
assert rom.predict([2]) == 5
assert rom.predict([3]) == 3
def test_wrong1(self):
# wrong number of params
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)
with self.assertRaises(Exception):
reg = KNeighborsRegressor()
reg.fit([[1, 2], [6,], [8, 9]], [[1, 0], [20, 5], [8, 6]])
def test_wrong2(self):
# wrong number of values
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning)
with self.assertRaises(Exception):
reg = KNeighborsRegressor()
reg.fit([[1, 2], [6,], [8, 9]], [[20, 5], [8, 6]])