MCPcopy
hub / github.com/ddbourgin/numpy-ml / test_knn_regression

Function test_knn_regression

numpy_ml/tests/test_nonparametric.py:12–47  ·  view source on GitHub ↗
(N=15)

Source from the content-addressed store, hash-verified

10
11
12def test_knn_regression(N=15):
13 np.random.seed(12345)
14
15 i = 0
16 while i < N:
17 N = np.random.randint(2, 100)
18 M = np.random.randint(2, 100)
19 k = np.random.randint(1, N)
20 ls = np.min([np.random.randint(1, 10), N - 1])
21 weights = np.random.choice(["uniform", "distance"])
22
23 X = np.random.rand(N, M)
24 X_test = np.random.rand(N, M)
25 y = np.random.rand(N)
26
27 knn = KNN(
28 k=k, leaf_size=ls, metric=euclidean, classifier=False, weights=weights
29 )
30 knn.fit(X, y)
31 preds = knn.predict(X_test)
32
33 gold = KNeighborsRegressor(
34 p=2,
35 leaf_size=ls,
36 n_neighbors=k,
37 weights=weights,
38 metric="minkowski",
39 algorithm="ball_tree",
40 )
41 gold.fit(X, y)
42 gold_preds = gold.predict(X_test)
43
44 for mine, theirs in zip(preds, gold_preds):
45 np.testing.assert_almost_equal(mine, theirs)
46 print("PASSED")
47 i += 1
48
49
50def test_knn_clf(N=15):

Callers

nothing calls this directly

Calls 5

fitMethod · 0.95
predictMethod · 0.95
KNNClass · 0.90
fitMethod · 0.45
predictMethod · 0.45

Tested by

no test coverage detected