Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b76d5a4

Browse files
author
giorgiop
committed
tests sample_weights linearreg, ridge
1 parent 04ca448 commit b76d5a4

File tree

2 files changed

+100
-63
lines changed

2 files changed

+100
-63
lines changed

sklearn/linear_model/tests/test_base.py

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
import numpy as np
77
from scipy import sparse
8+
from scipy import linalg
89

910
from sklearn.utils.testing import assert_array_almost_equal
11+
from sklearn.utils.testing import assert_almost_equal
1012
from sklearn.utils.testing import assert_equal
1113

1214
from sklearn.linear_model.base import LinearRegression
13-
from sklearn.linear_model.base import center_data, sparse_center_data, _rescale_data
15+
from sklearn.linear_model.base import center_data
16+
from sklearn.linear_model.base import sparse_center_data
17+
from sklearn.linear_model.base import _rescale_data
1418
from sklearn.utils import check_random_state
1519
from sklearn.utils.testing import assert_greater
1620
from sklearn.datasets.samples_generator import make_sparse_uncorrelated
@@ -23,48 +27,63 @@ def test_linear_regression():
2327
X = [[1], [2]]
2428
Y = [1, 2]
2529

26-
clf = LinearRegression()
27-
clf.fit(X, Y)
30+
reg = LinearRegression()
31+
reg.fit(X, Y)
2832

29-
assert_array_almost_equal(clf.coef_, [1])
30-
assert_array_almost_equal(clf.intercept_, [0])
31-
assert_array_almost_equal(clf.predict(X), [1, 2])
33+
assert_array_almost_equal(reg.coef_, [1])
34+
assert_array_almost_equal(reg.intercept_, [0])
35+
assert_array_almost_equal(reg.predict(X), [1, 2])
3236

3337
# test it also for degenerate input
3438
X = [[1]]
3539
Y = [0]
3640

37-
clf = LinearRegression()
38-
clf.fit(X, Y)
39-
assert_array_almost_equal(clf.coef_, [0])
40-
assert_array_almost_equal(clf.intercept_, [0])
41-
assert_array_almost_equal(clf.predict(X), [0])
41+
reg = LinearRegression()
42+
reg.fit(X, Y)
43+
assert_array_almost_equal(reg.coef_, [0])
44+
assert_array_almost_equal(reg.intercept_, [0])
45+
assert_array_almost_equal(reg.predict(X), [0])
4246

4347

4448
def test_linear_regression_sample_weights():
4549
rng = np.random.RandomState(0)
4650

47-
for n_samples, n_features in ((6, 5), (5, 10)):
51+
# It would not work with under-determined systems
52+
for n_samples, n_features in ((6, 5), ):
53+
4854
y = rng.randn(n_samples)
4955
X = rng.randn(n_samples, n_features)
5056
sample_weight = 1.0 + rng.rand(n_samples)
5157

52-
clf = LinearRegression()
53-
clf.fit(X, y, sample_weight)
54-
coefs1 = clf.coef_
58+
# TODO: loop over sparse data as well
59+
for intercept in (True, False):
60+
61+
# LinearRegression with explicit sample_weight
62+
reg = LinearRegression(fit_intercept=intercept)
63+
reg.fit(X, y, sample_weight=sample_weight)
64+
coefs1 = reg.coef_
65+
inter1 = reg.intercept_
66+
67+
assert_equal(reg.coef_.shape, (X.shape[1], )) # sanity checks
68+
assert_greater(reg.score(X, y), 0.5)
5569

56-
assert_equal(clf.coef_.shape, (X.shape[1], ))
57-
assert_greater(clf.score(X, y), 0.9)
58-
assert_array_almost_equal(clf.predict(X), y)
70+
# Closed form of the weighted least square
71+
# theta = (X^T W X)^(-1) * X^T W y
72+
W = np.diag(sample_weight)
73+
if intercept is False:
74+
X_aug = X.copy()
75+
else:
76+
dummy_column = np.ones(shape=(n_samples, 1))
77+
X_aug = np.concatenate((dummy_column, X), axis=1)
5978

60-
# Sample weight can be implemented via a simple rescaling
61-
# for the square loss.
62-
scaled_y = y * np.sqrt(sample_weight)
63-
scaled_X = X * np.sqrt(sample_weight)[:, np.newaxis]
64-
clf.fit(X, y)
65-
coefs2 = clf.coef_
79+
coefs3 = linalg.pinv(X_aug.T.dot(W).dot(X_aug)
80+
).dot(X_aug.T).dot(W).dot(y)
6681

67-
assert_array_almost_equal(coefs1, coefs2)
82+
if intercept is False:
83+
assert_array_almost_equal(coefs1, coefs3)
84+
else:
85+
assert_array_almost_equal(coefs1, coefs3[1:])
86+
assert_almost_equal(inter1, coefs3[0])
6887

6988

7089
def test_raises_value_error_if_sample_weights_greater_than_1d():
@@ -82,12 +101,12 @@ def test_raises_value_error_if_sample_weights_greater_than_1d():
82101
sample_weights_OK_1 = 1.
83102
sample_weights_OK_2 = 2.
84103

85-
clf = LinearRegression()
104+
reg = LinearRegression()
86105

87106
# make sure the "OK" sample weights actually work
88-
clf.fit(X, y, sample_weights_OK)
89-
clf.fit(X, y, sample_weights_OK_1)
90-
clf.fit(X, y, sample_weights_OK_2)
107+
reg.fit(X, y, sample_weights_OK)
108+
reg.fit(X, y, sample_weights_OK_1)
109+
reg.fit(X, y, sample_weights_OK_2)
91110

92111

93112
def test_fit_intercept():
@@ -135,12 +154,12 @@ def test_linear_regression_multiple_outcome(random_state=0):
135154
Y = np.vstack((y, y)).T
136155
n_features = X.shape[1]
137156

138-
clf = LinearRegression(fit_intercept=True)
139-
clf.fit((X), Y)
140-
assert_equal(clf.coef_.shape, (2, n_features))
141-
Y_pred = clf.predict(X)
142-
clf.fit(X, y)
143-
y_pred = clf.predict(X)
157+
reg = LinearRegression(fit_intercept=True)
158+
reg.fit((X), Y)
159+
assert_equal(reg.coef_.shape, (2, n_features))
160+
Y_pred = reg.predict(X)
161+
reg.fit(X, y)
162+
y_pred = reg.predict(X)
144163
assert_array_almost_equal(np.vstack((y_pred, y_pred)).T, Y_pred, decimal=3)
145164

146165

sklearn/linear_model/tests/test_ridge.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import scipy.sparse as sp
33
from scipy import linalg
4+
from itertools import product
45

56
from sklearn.utils.testing import assert_true
67
from sklearn.utils.testing import assert_almost_equal
@@ -112,20 +113,21 @@ def test_ridge_singular():
112113
assert_greater(ridge.score(X, y), 0.9)
113114

114115

115-
def test_ridge_sample_weights():
116+
def test_ridge_regression_sample_weights():
116117
rng = np.random.RandomState(0)
117118

118119
for solver in ("cholesky", ):
119120
for n_samples, n_features in ((6, 5), (5, 10)):
120121
for alpha in (1.0, 1e-2):
121122
y = rng.randn(n_samples)
122123
X = rng.randn(n_samples, n_features)
123-
sample_weight = 1 + rng.rand(n_samples)
124+
sample_weight = 1.0 + rng.rand(n_samples)
124125

125126
coefs = ridge_regression(X, y,
126127
alpha=alpha,
127128
sample_weight=sample_weight,
128129
solver=solver)
130+
129131
# Sample weight can be implemented via a simple rescaling
130132
# for the square loss.
131133
coefs2 = ridge_regression(
@@ -134,32 +136,48 @@ def test_ridge_sample_weights():
134136
alpha=alpha, solver=solver)
135137
assert_array_almost_equal(coefs, coefs2)
136138

137-
# Test for fit_intercept = True
138-
est = Ridge(alpha=alpha, solver=solver)
139-
est.fit(X, y, sample_weight=sample_weight)
140-
141-
# Check using Newton's Method
142-
# Quadratic function should be solved in a single step.
143-
# Initialize
144-
sample_weight = np.sqrt(sample_weight)
145-
X_weighted = sample_weight[:, np.newaxis] * (
146-
np.column_stack((np.ones(n_samples), X)))
147-
y_weighted = y * sample_weight
148-
149-
# Gradient is (X*coef-y)*X + alpha*coef_[1:]
150-
# Remove coef since it is initialized to zero.
151-
grad = -np.dot(y_weighted, X_weighted)
152-
153-
# Hessian is (X.T*X) + alpha*I except that the first
154-
# diagonal element should be zero, since there is no
155-
# penalization of intercept.
156-
diag = alpha * np.ones(n_features + 1)
157-
diag[0] = 0.
158-
hess = np.dot(X_weighted.T, X_weighted)
159-
hess.flat[::n_features + 2] += diag
160-
coef_ = - np.dot(linalg.inv(hess), grad)
161-
assert_almost_equal(coef_[0], est.intercept_)
162-
assert_array_almost_equal(coef_[1:], est.coef_)
139+
140+
def test_ridge_sample_weights():
141+
rng = np.random.RandomState(0)
142+
param_grid = product((1.0, 1e-2), (True, False),
143+
('svd', 'cholesky', 'lsqr', 'sparse_cg'))
144+
145+
for n_samples, n_features in ((6, 5), (5, 10)):
146+
147+
y = rng.randn(n_samples)
148+
X = rng.randn(n_samples, n_features)
149+
sample_weight = 1 + rng.rand(n_samples)
150+
151+
# TODO: loop over sparse data as well
152+
for (alpha, intercept, solver) in param_grid:
153+
print(solver)
154+
155+
# Ridge with explicit sample_weight
156+
est = Ridge(alpha=alpha, fit_intercept=intercept, solver=solver)
157+
est.fit(X, y, sample_weight=sample_weight)
158+
coefs = est.coef_
159+
inter = est.intercept_
160+
161+
# Closed form of the weighted regularized least square
162+
# theta = (X^T W X + alpha I)^(-1) * X^T W y
163+
W = np.diag(sample_weight)
164+
if intercept is False:
165+
X_aug = X.copy()
166+
I = np.eye(n_features)
167+
else:
168+
dummy_column = np.ones(shape=(n_samples, 1))
169+
X_aug = np.concatenate((dummy_column, X), axis=1)
170+
I = np.eye(n_features + 1)
171+
I[0, 0] = 0
172+
173+
cf_coefs = linalg.inv(X_aug.T.dot(W).dot(X_aug) +
174+
alpha * I).dot(X_aug.T).dot(W).dot(y)
175+
176+
if intercept is False:
177+
assert_array_almost_equal(coefs, cf_coefs)
178+
else:
179+
assert_array_almost_equal(coefs, cf_coefs[1:])
180+
assert_almost_equal(inter, cf_coefs[0])
163181

164182

165183
def test_ridge_shapes():

0 commit comments

Comments
 (0)