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

Function test_linear_regression

numpy_ml/tests/test_linear_regression.py:10–81  ·  view source on GitHub ↗
(N=10)

Source from the content-addressed store, hash-verified

8
9
10def test_linear_regression(N=10):
11 np.random.seed(12345)
12 N = np.inf if N is None else N
13
14 i = 1
15 while i < N + 1:
16 train_samples = np.random.randint(2, 30)
17 update_samples = np.random.randint(1, 30)
18 n_samples = train_samples + update_samples
19
20 # ensure n_feats < train_samples, otherwise multiple solutions are
21 # possible
22 n_feats = np.random.randint(1, train_samples)
23 target_dim = np.random.randint(1, 10)
24
25 fit_intercept = np.random.choice([True, False])
26
27 X = random_tensor((n_samples, n_feats), standardize=True)
28 y = random_tensor((n_samples, target_dim), standardize=True)
29
30 weighted = np.random.choice([True, False])
31 weights = np.random.rand(n_samples) if weighted else np.ones(n_samples)
32
33 X_train, X_update = X[:train_samples], X[train_samples:]
34 y_train, y_update = y[:train_samples], y[train_samples:]
35 w_train, w_update = weights[:train_samples], weights[train_samples:]
36
37 print(f"Weights: {weighted}")
38 print(f"Fit intercept: {fit_intercept}")
39
40 # Fit gold standard model on the entire dataset
41 lr_gold = LinearRegressionGold(fit_intercept=fit_intercept, normalize=False)
42 lr_gold.fit(X, y, sample_weight=weights)
43
44 lr_mine = LinearRegression(fit_intercept=fit_intercept)
45 lr_mine.fit(X, y, weights=weights)
46
47 # check that model predictions match
48 np.testing.assert_almost_equal(
49 lr_mine.predict(X), lr_gold.predict(X), decimal=5
50 )
51 print("\t1. Overall model predictions match")
52
53 # check that model coefficients match
54 beta = lr_mine.beta.T[:, 1:] if fit_intercept else lr_mine.beta.T
55 np.testing.assert_almost_equal(beta, lr_gold.coef_, decimal=6)
56 print("\t2. Overall model coefficients match")
57
58 # Fit our model on just (X_train, y_train)...
59 lr = LinearRegression(fit_intercept=fit_intercept)
60 lr.fit(X_train, y_train, weights=w_train)
61
62 do_single_sample_update = np.random.choice([True, False])
63
64 # ...then update our model on the examples (X_update, y_update)
65 if do_single_sample_update:
66 for x_new, y_new, w_new in zip(X_update, y_update, w_update):
67 lr.update(x_new, y_new, w_new)

Callers

nothing calls this directly

Calls 7

fitMethod · 0.95
predictMethod · 0.95
updateMethod · 0.95
random_tensorFunction · 0.90
LinearRegressionClass · 0.90
fitMethod · 0.45
predictMethod · 0.45

Tested by

no test coverage detected