@@ -313,11 +313,8 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
313
313
raise ValueError ("Sample weights must be 1D array or scalar" )
314
314
315
315
if solver != 'sag' :
316
- # Sample weight can be implemented via a simple rescaling.
317
- # FIX: In the sparse case, with fit_intercept=True, we cannot
318
- # center the data, so a rescaling is incorrect! See #4710.
319
- # Sag solver can actually handle sample weights without rescaling,
320
- # and can be used as a reference to solve #4710.
316
+ # SAG supports sample_weight directly. For other solvers,
317
+ # we implement sample_weight via a simple rescaling.
321
318
X , y = _rescale_data (X , y , sample_weight )
322
319
323
320
# There should be either 1 or n_targets penalties
@@ -405,6 +402,13 @@ def fit(self, X, y, sample_weight=None):
405
402
X , y , self .fit_intercept , self .normalize , self .copy_X ,
406
403
sample_weight = sample_weight )
407
404
405
+ if sparse .issparse (X ) and self .fit_intercept :
406
+ if self .solver == 'auto' :
407
+ self .solver = 'sag'
408
+ elif self .solver != 'sag' :
409
+ raise ValueError ("Only sag solver can handle sparse data with"
410
+ " fit_intercept=True (temp fix for #4710)" )
411
+
408
412
self .coef_ = ridge_regression (X , y ,
409
413
alpha = self .alpha ,
410
414
sample_weight = sample_weight ,
0 commit comments