1
1
import numpy as np
2
2
import scipy .sparse as sp
3
3
from scipy import linalg
4
+ from itertools import product
4
5
5
6
from sklearn .utils .testing import assert_true
6
7
from sklearn .utils .testing import assert_almost_equal
@@ -111,7 +112,7 @@ def test_ridge_singular():
111
112
assert_greater (ridge .score (X , y ), 0.9 )
112
113
113
114
114
- def test_ridge_sample_weights ():
115
+ def test_ridge_regression_sample_weights ():
115
116
rng = np .random .RandomState (0 )
116
117
117
118
for solver in ("cholesky" , ):
@@ -125,6 +126,7 @@ def test_ridge_sample_weights():
125
126
alpha = alpha ,
126
127
sample_weight = sample_weight ,
127
128
solver = solver )
129
+
128
130
# Sample weight can be implemented via a simple rescaling
129
131
# for the square loss.
130
132
coefs2 = ridge_regression (
@@ -133,32 +135,46 @@ def test_ridge_sample_weights():
133
135
alpha = alpha , solver = solver )
134
136
assert_array_almost_equal (coefs , coefs2 )
135
137
136
- # Test for fit_intercept = True
137
- est = Ridge (alpha = alpha , solver = solver )
138
- est .fit (X , y , sample_weight = sample_weight )
139
-
140
- # Check using Newton's Method
141
- # Quadratic function should be solved in a single step.
142
- # Initialize
143
- sample_weight = np .sqrt (sample_weight )
144
- X_weighted = sample_weight [:, np .newaxis ] * (
145
- np .column_stack ((np .ones (n_samples ), X )))
146
- y_weighted = y * sample_weight
147
-
148
- # Gradient is (X*coef-y)*X + alpha*coef_[1:]
149
- # Remove coef since it is initialized to zero.
150
- grad = - np .dot (y_weighted , X_weighted )
151
-
152
- # Hessian is (X.T*X) + alpha*I except that the first
153
- # diagonal element should be zero, since there is no
154
- # penalization of intercept.
155
- diag = alpha * np .ones (n_features + 1 )
156
- diag [0 ] = 0.
157
- hess = np .dot (X_weighted .T , X_weighted )
158
- hess .flat [::n_features + 2 ] += diag
159
- coef_ = - np .dot (linalg .inv (hess ), grad )
160
- assert_almost_equal (coef_ [0 ], est .intercept_ )
161
- assert_array_almost_equal (coef_ [1 :], est .coef_ )
138
+
139
+ def test_ridge_sample_weights ():
140
+ rng = np .random .RandomState (0 )
141
+ param_grid = product ((1.0 , 1e-2 ), (True , False ),
142
+ ('svd' , 'cholesky' , 'lsqr' , 'sparse_cg' ))
143
+
144
+ for n_samples , n_features in ((6 , 5 ), (5 , 10 )):
145
+
146
+ y = rng .randn (n_samples )
147
+ X = rng .randn (n_samples , n_features )
148
+ sample_weight = 1 + rng .rand (n_samples )
149
+
150
+ for (alpha , intercept , solver ) in param_grid :
151
+ print (solver )
152
+
153
+ # Ridge with explicit sample_weight
154
+ est = Ridge (alpha = alpha , fit_intercept = intercept , solver = solver )
155
+ est .fit (X , y , sample_weight = sample_weight )
156
+ coefs = est .coef_
157
+ inter = est .intercept_
158
+
159
+ # Closed form of the weighted regularized least square
160
+ # theta = (X^T W X + alpha I)^(-1) * X^T W y
161
+ W = np .diag (sample_weight )
162
+ if intercept is False :
163
+ X_aug = X .copy ()
164
+ I = np .eye (n_features )
165
+ else :
166
+ X_aug = np .column_stack ((np .ones (n_samples ), X ))
167
+ I = np .eye (n_features + 1 )
168
+ I [0 , 0 ] = 0
169
+
170
+ cf_coefs = linalg .inv (X_aug .T .dot (W ).dot (X_aug ) +
171
+ alpha * I ).dot (X_aug .T ).dot (W ).dot (y )
172
+
173
+ if intercept is False :
174
+ assert_array_almost_equal (coefs , cf_coefs )
175
+ else :
176
+ assert_array_almost_equal (coefs , cf_coefs [1 :])
177
+ assert_almost_equal (inter , cf_coefs [0 ])
162
178
163
179
164
180
def test_ridge_shapes ():
0 commit comments