@@ -206,7 +206,8 @@ def _solve_svd(X, y, alpha):
206
206
207
207
def ridge_regression (X , y , alpha , sample_weight = None , solver = 'auto' ,
208
208
max_iter = None , tol = 1e-3 , verbose = 0 , random_state = None ,
209
- return_n_iter = False , return_intercept = False ):
209
+ return_n_iter = False , return_intercept = False ,
210
+ check_input = True ):
210
211
"""Solve the ridge equation by the method of normal equations.
211
212
212
213
Read more in the :ref:`User Guide <ridge_regression>`.
@@ -309,6 +310,11 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
309
310
310
311
.. versionadded:: 0.17
311
312
313
+ check_input : boolean, default True
314
+ If False, the input arrays X and y will not be checked.
315
+
316
+ .. versionadded:: 0.20
317
+
312
318
Returns
313
319
-------
314
320
coef : array, shape = [n_features] or [n_targets, n_features]
@@ -335,15 +341,10 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
335
341
336
342
_dtype = [np .float64 , np .float32 ]
337
343
338
- # SAG needs X and y columns to be C-contiguous and np.float64
339
- if solver in ['sag' , 'saga' ]:
340
- X = check_array (X , accept_sparse = ['csr' ],
341
- dtype = np .float64 , order = 'C' )
342
- y = check_array (y , dtype = np .float64 , ensure_2d = False , order = 'F' )
343
- else :
344
- X = check_array (X , accept_sparse = ['csr' , 'csc' , 'coo' ],
345
- dtype = _dtype )
346
- y = check_array (y , dtype = X .dtype , ensure_2d = False )
344
+ if check_input :
345
+ X = check_array (X , accept_sparse = ['csr' , 'csc' , 'coo' ], dtype = _dtype ,
346
+ order = "C" )
347
+ y = check_array (y , dtype = X .dtype , ensure_2d = False , order = "C" )
347
348
check_consistent_length (X , y )
348
349
349
350
n_samples , n_features = X .shape
@@ -422,11 +423,12 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
422
423
# precompute max_squared_sum for all targets
423
424
max_squared_sum = row_norms (X , squared = True ).max ()
424
425
425
- coef = np .empty ((y .shape [1 ], n_features ))
426
+ coef = np .empty ((y .shape [1 ], n_features ), dtype = X . dtype )
426
427
n_iter = np .empty (y .shape [1 ], dtype = np .int32 )
427
- intercept = np .zeros ((y .shape [1 ], ))
428
+ intercept = np .zeros ((y .shape [1 ], ), dtype = X . dtype )
428
429
for i , (alpha_i , target ) in enumerate (zip (alpha , y .T )):
429
- init = {'coef' : np .zeros ((n_features + int (return_intercept ), 1 ))}
430
+ init = {'coef' : np .zeros ((n_features + int (return_intercept ), 1 ),
431
+ dtype = X .dtype )}
430
432
coef_ , n_iter_ , _ = sag_solver (
431
433
X , target .ravel (), sample_weight , 'squared' , alpha_i , 0 ,
432
434
max_iter , tol , verbose , random_state , False , max_squared_sum ,
@@ -479,12 +481,8 @@ def __init__(self, alpha=1.0, fit_intercept=True, normalize=False,
479
481
480
482
def fit (self , X , y , sample_weight = None ):
481
483
482
- if self .solver in ('sag' , 'saga' ):
483
- _dtype = np .float64
484
- else :
485
- # all other solvers work at both float precision levels
486
- _dtype = [np .float64 , np .float32 ]
487
-
484
+ # all other solvers work at both float precision levels
485
+ _dtype = [np .float64 , np .float32 ]
488
486
X , y = check_X_y (X , y , ['csr' , 'csc' , 'coo' ], dtype = _dtype ,
489
487
multi_output = True , y_numeric = True )
490
488
@@ -502,14 +500,14 @@ def fit(self, X, y, sample_weight=None):
502
500
X , y , alpha = self .alpha , sample_weight = sample_weight ,
503
501
max_iter = self .max_iter , tol = self .tol , solver = self .solver ,
504
502
random_state = self .random_state , return_n_iter = True ,
505
- return_intercept = True )
503
+ return_intercept = True , check_input = False )
506
504
self .intercept_ += y_offset
507
505
else :
508
506
self .coef_ , self .n_iter_ = ridge_regression (
509
507
X , y , alpha = self .alpha , sample_weight = sample_weight ,
510
508
max_iter = self .max_iter , tol = self .tol , solver = self .solver ,
511
509
random_state = self .random_state , return_n_iter = True ,
512
- return_intercept = False )
510
+ return_intercept = False , check_input = False )
513
511
self ._set_intercept (X_offset , y_offset , X_scale )
514
512
515
513
return self
@@ -1013,10 +1011,12 @@ def fit(self, X, y, sample_weight=None):
1013
1011
-------
1014
1012
self : object
1015
1013
"""
1016
- X , y = check_X_y (X , y , ['csr' , 'csc' , 'coo' ], dtype = np .float64 ,
1014
+ X , y = check_X_y (X , y , ['csr' , 'csc' , 'coo' ],
1015
+ dtype = [np .float64 , np .float32 ],
1017
1016
multi_output = True , y_numeric = True )
1018
1017
if sample_weight is not None and not isinstance (sample_weight , float ):
1019
- sample_weight = check_array (sample_weight , ensure_2d = False )
1018
+ sample_weight = check_array (sample_weight , ensure_2d = False ,
1019
+ dtype = X .dtype )
1020
1020
n_samples , n_features = X .shape
1021
1021
1022
1022
X , y , X_offset , y_offset , X_scale = LinearModel ._preprocess_data (
0 commit comments