-
Notifications
You must be signed in to change notification settings - Fork 856
/
test_linear_regression.py
139 lines (111 loc) · 4.32 KB
/
test_linear_regression.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Sebastian Raschka 2014-2018
# mlxtend Machine Learning Library Extensions
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
from mlxtend.regressor import LinearRegression
from mlxtend.data import boston_housing_data
import numpy as np
from numpy.testing import assert_almost_equal
from sklearn.base import clone
X, y = boston_housing_data()
X_rm = X[:, 5][:, np.newaxis]
X_rm_lstat = X[:, [5, -1]]
# standardized variables
X_rm_std = (X_rm - X_rm.mean(axis=0)) / X_rm.std(axis=0)
X_rm_lstat_std = ((X_rm_lstat - X_rm_lstat.mean(axis=0)) /
X_rm_lstat.std(axis=0))
y_std = (y - y.mean()) / y.std()
def test_univariate_normal_equation():
w_exp = np.array([[9.1]])
b_exp = np.array([-34.7])
ne_lr = LinearRegression(minibatches=None)
ne_lr.fit(X_rm, y)
assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
def test_univariate_normal_equation_std():
w_exp = np.array([[0.7]])
b_exp = np.array([0.0])
ne_lr = LinearRegression(minibatches=None)
ne_lr.fit(X_rm_std, y_std)
assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
def test_univariate_gradient_descent():
w_exp = np.array([[0.7]])
b_exp = np.array([0.0])
gd_lr = LinearRegression(minibatches=1,
eta=0.001,
epochs=500,
random_seed=0)
gd_lr.fit(X_rm_std, y_std)
assert_almost_equal(gd_lr.w_, w_exp, decimal=1)
assert_almost_equal(gd_lr.b_, b_exp, decimal=1)
def test_progress_1():
gd_lr = LinearRegression(minibatches=1,
eta=0.001,
epochs=1,
print_progress=1,
random_seed=0)
gd_lr.fit(X_rm_std, y_std)
def test_progress_2():
gd_lr = LinearRegression(minibatches=1,
eta=0.001,
epochs=1,
print_progress=2,
random_seed=0)
gd_lr.fit(X_rm_std, y_std)
def test_progress_3():
gd_lr = LinearRegression(minibatches=1,
eta=0.001,
epochs=1,
print_progress=2,
random_seed=0)
gd_lr.fit(X_rm_std, y_std)
def test_univariate_stochastic_gradient_descent():
w_exp = np.array([[0.7]])
b_exp = np.array([0.0])
sgd_lr = LinearRegression(minibatches=len(y),
eta=0.0001,
epochs=150,
random_seed=0)
sgd_lr.fit(X_rm_std, y_std)
assert_almost_equal(sgd_lr.w_, w_exp, decimal=1)
assert_almost_equal(sgd_lr.b_, b_exp, decimal=1)
def test_multivariate_normal_equation():
w_exp = np.array([[5.1], [-0.6]])
b_exp = np.array([-1.5])
ne_lr = LinearRegression(minibatches=None)
ne_lr.fit(X_rm_lstat, y)
assert_almost_equal(ne_lr.w_, w_exp, decimal=1)
assert_almost_equal(ne_lr.b_, b_exp, decimal=1)
def test_multivariate_gradient_descent():
w_exp = np.array([[0.4], [-0.5]])
b_exp = np.array([0.0])
gd_lr = LinearRegression(eta=0.001,
epochs=500,
minibatches=1,
random_seed=0)
gd_lr.fit(X_rm_lstat_std, y_std)
assert_almost_equal(gd_lr.w_, w_exp, decimal=1)
assert_almost_equal(gd_lr.b_, b_exp, decimal=1)
def test_multivariate_stochastic_gradient_descent():
w_exp = np.array([[0.4], [-0.5]])
b_exp = np.array([0.0])
sgd_lr = LinearRegression(eta=0.0001,
epochs=500,
minibatches=len(y),
random_seed=0)
sgd_lr.fit(X_rm_lstat_std, y_std)
assert_almost_equal(sgd_lr.w_, w_exp, decimal=1)
assert_almost_equal(sgd_lr.b_, b_exp, decimal=1)
def test_ary_persistency_in_shuffling():
orig = X_rm_lstat_std.copy()
sgd_lr = LinearRegression(eta=0.0001,
epochs=500,
minibatches=len(y),
random_seed=0)
sgd_lr.fit(X_rm_lstat_std, y_std)
np.testing.assert_almost_equal(orig, X_rm_lstat_std, 6)
def test_clone():
regr = LinearRegression()
clone(regr)