Skip to content

Commit 41e6a32

Browse files
committed
fix tests for linear models
1 parent cba1b8a commit 41e6a32

File tree

6 files changed

+90
-24
lines changed

6 files changed

+90
-24
lines changed

numpy_ml/linear_models/linear_regression.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ def update(self, X, y, weights=None):
122122
X, y = np.atleast_2d(X), np.atleast_2d(y)
123123

124124
X1, Y1 = X.shape[0], y.shape[0]
125-
weights = np.ones(X1) if weights is None else np.squeeze(np.atleast_1d(weights))
125+
weights = np.ones(X1) if weights is None else np.atleast_1d(weights)
126+
weights = np.squeeze(weights) if weights.size > 1 else weights
126127

127128
err_str = f"weights must have shape ({X1},) but got {weights.shape}"
128129
assert weights.shape == (X1,), err_str
@@ -187,7 +188,8 @@ def fit(self, X, y, weights=None):
187188
"""
188189
N = X.shape[0]
189190

190-
weights = np.ones(N) if weights is None else np.squeeze(np.atleast_1d(weights))
191+
weights = np.ones(N) if weights is None else np.atleast_1d(weights)
192+
weights = np.squeeze(weights) if weights.size > 1 else weights
191193
err_str = f"weights must have shape ({N},) but got {weights.shape}"
192194
assert weights.shape == (N,), err_str
193195

@@ -200,7 +202,7 @@ def fit(self, X, y, weights=None):
200202
X = np.c_[np.sqrt(weights), X]
201203

202204
self.sigma_inv = np.linalg.pinv(X.T @ X)
203-
self.beta = np.atleast_2d(self.sigma_inv @ X.T @ y).T
205+
self.beta = np.atleast_2d(self.sigma_inv @ X.T @ y)
204206

205207
self._is_fit = True
206208
return self
@@ -223,4 +225,5 @@ def predict(self, X):
223225
# convert X to a design matrix if we're fitting an intercept
224226
if self.fit_intercept:
225227
X = np.c_[np.ones(X.shape[0]), X]
226-
return np.dot(X, self.beta)
228+
return X @ self.beta
229+
# return np.dot(X, self.beta)

numpy_ml/tests/test_linear_regression.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# flake8: noqa
22
import numpy as np
33

4-
import statsmodels.api as sm
54
from sklearn.linear_model import LinearRegression as LinearRegressionGold
65

76
from numpy_ml.linear_models import LinearRegression

numpy_ml/tests/test_naive_bayes.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# flake8: noqa
12
import numpy as np
23
from sklearn import datasets
34
from sklearn.model_selection import train_test_split
@@ -13,6 +14,7 @@ def test_GaussianNB(N=10):
1314
N = np.inf if N is None else N
1415

1516
i = 1
17+
eps = np.finfo(float).eps
1618
while i < N + 1:
1719
n_ex = np.random.randint(1, 300)
1820
n_feats = np.random.randint(1, 100)
@@ -33,29 +35,29 @@ def test_GaussianNB(N=10):
3335

3436
sk_preds = sklearn_NB.predict(X_test)
3537

36-
for i in range(len(NB.labels)):
38+
for j in range(len(NB.labels)):
3739
P = NB.parameters
38-
jointi = np.log(sklearn_NB.class_prior_[i])
39-
jointi_mine = np.log(P["prior"][i])
40+
jointi = np.log(sklearn_NB.class_prior_[j])
41+
jointi_mine = np.log(P["prior"][j])
4042

4143
np.testing.assert_almost_equal(jointi, jointi_mine)
4244

43-
n_ij = -0.5 * np.sum(np.log(2.0 * np.pi * sklearn_NB.sigma_[i, :]))
44-
n_ij_mine = -0.5 * np.sum(np.log(2.0 * np.pi * P["sigma"][i]))
45+
n_jk = -0.5 * np.sum(np.log(2.0 * np.pi * sklearn_NB.sigma_[j, :] + eps))
46+
n_jk_mine = -0.5 * np.sum(np.log(2.0 * np.pi * P["sigma"][j] + eps))
4547

46-
np.testing.assert_almost_equal(n_ij_mine, n_ij)
48+
np.testing.assert_almost_equal(n_jk_mine, n_jk)
4749

48-
n_ij2 = n_ij - 0.5 * np.sum(
49-
((X_test - sklearn_NB.theta_[i, :]) ** 2) / (sklearn_NB.sigma_[i, :]), 1
50+
n_jk2 = n_jk - 0.5 * np.sum(
51+
((X_test - sklearn_NB.theta_[j, :]) ** 2) / (sklearn_NB.sigma_[j, :]), 1
5052
)
5153

52-
n_ij2_mine = n_ij_mine - 0.5 * np.sum(
53-
((X_test - P["mean"][i]) ** 2) / (P["sigma"][i]), 1
54+
n_jk2_mine = n_jk_mine - 0.5 * np.sum(
55+
((X_test - P["mean"][j]) ** 2) / (P["sigma"][j]), 1
5456
)
55-
np.testing.assert_almost_equal(n_ij2_mine, n_ij2, decimal=4)
57+
np.testing.assert_almost_equal(n_jk2_mine, n_jk2, decimal=4)
5658

57-
llh = jointi + n_ij2
58-
llh_mine = jointi_mine + n_ij2_mine
59+
llh = jointi + n_jk2
60+
llh_mine = jointi_mine + n_jk2_mine
5961

6062
np.testing.assert_almost_equal(llh_mine, llh, decimal=4)
6163

numpy_ml/tests/test_nn.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import torch.nn as nn
1515
import torch.nn.functional as F
1616

17-
import tensorflow.keras.datasets.mnist as mnist
18-
1917
from numpy_ml.neural_nets.utils import (
2018
calc_pad_dims_2D,
2119
conv2D_naive,
@@ -2308,6 +2306,7 @@ def test_conv(N=15):
23082306

23092307
def fit_VAE():
23102308
# for testing
2309+
import tensorflow.keras.datasets.mnist as mnist
23112310
from numpy_ml.neural_nets.models.vae import BernoulliVAE
23122311

23132312
np.random.seed(12345)

numpy_ml/tests/test_preprocessing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_mel_filterbank(N=15):
248248
n_fft=window_len,
249249
n_mels=n_filters,
250250
htk=True,
251-
norm=norm if norm == 1 else None,
251+
norm="slaney" if norm == 1 else None,
252252
)
253253

254254
np.testing.assert_almost_equal(mine, theirs)

numpy_ml/tests/test_utils.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@
1010
from sklearn.metrics.pairwise import polynomial_kernel as sk_poly
1111

1212

13-
from numpy_ml.utils.distance_metrics import euclidean
13+
from numpy_ml.utils.distance_metrics import (
14+
hamming,
15+
euclidean,
16+
chebyshev,
17+
manhattan,
18+
minkowski,
19+
)
1420
from numpy_ml.utils.kernels import LinearKernel, PolynomialKernel, RBFKernel
1521
from numpy_ml.utils.data_structures import BallTree
1622
from numpy_ml.utils.graphs import (
23+
Edge,
1724
DiGraph,
1825
UndirectedGraph,
19-
Edge,
20-
random_unweighted_graph,
2126
random_DAG,
27+
random_unweighted_graph,
2228
)
2329

2430
#######################################################################
@@ -110,6 +116,63 @@ def test_euclidean(N=1):
110116
i += 1
111117

112118

119+
def test_hamming(N=1):
120+
np.random.seed(12345)
121+
i = 0
122+
while i < N:
123+
N = np.random.randint(1, 100)
124+
x = (np.random.rand(N) * 100).round().astype(int)
125+
y = (np.random.rand(N) * 100).round().astype(int)
126+
mine = hamming(x, y)
127+
theirs = scipy.spatial.distance.hamming(x, y)
128+
np.testing.assert_almost_equal(mine, theirs)
129+
print("PASSED")
130+
i += 1
131+
132+
133+
def test_minkowski(N=1):
134+
np.random.seed(12345)
135+
i = 0
136+
while i < N:
137+
N = np.random.randint(1, 100)
138+
p = 1 + np.random.rand() * 10
139+
x = np.random.rand(N)
140+
y = np.random.rand(N)
141+
mine = minkowski(x, y, p)
142+
theirs = scipy.spatial.distance.minkowski(x, y, p)
143+
np.testing.assert_almost_equal(mine, theirs)
144+
print("PASSED")
145+
i += 1
146+
147+
148+
def test_chebyshev(N=1):
149+
np.random.seed(12345)
150+
i = 0
151+
while i < N:
152+
N = np.random.randint(1, 100)
153+
x = np.random.rand(N)
154+
y = np.random.rand(N)
155+
mine = chebyshev(x, y)
156+
theirs = scipy.spatial.distance.chebyshev(x, y)
157+
np.testing.assert_almost_equal(mine, theirs)
158+
print("PASSED")
159+
i += 1
160+
161+
162+
def test_manhattan(N=1):
163+
np.random.seed(12345)
164+
i = 0
165+
while i < N:
166+
N = np.random.randint(1, 100)
167+
x = np.random.rand(N)
168+
y = np.random.rand(N)
169+
mine = manhattan(x, y)
170+
theirs = scipy.spatial.distance.cityblock(x, y)
171+
np.testing.assert_almost_equal(mine, theirs)
172+
print("PASSED")
173+
i += 1
174+
175+
113176
#######################################################################
114177
# Data Structures #
115178
#######################################################################

0 commit comments

Comments
 (0)