8000 iter · scikit-learn/scikit-learn@872ebf5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 872ebf5

Browse files
glemaitreJoan Massich
authored andcommitted
iter
1 parent a6a7645 commit 872ebf5

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

sklearn/linear_model/ridge.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def _solve_svd(X, y, alpha):
206206

207207
def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
208208
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):
210211
"""Solve the ridge equation by the method of normal equations.
211212
212213
Read more in the :ref:`User Guide <ridge_regression>`.
@@ -309,6 +310,11 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
309310
310311
.. versionadded:: 0.17
311312
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+
312318
Returns
313319
-------
314320
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',
335341

336342
_dtype = [np.float64, np.float32]
337343

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")
347348
check_consistent_length(X, y)
348349

349350
n_samples, n_features = X.shape
@@ -422,11 +423,12 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
422423
# precompute max_squared_sum for all targets
423424
max_squared_sum = row_norms(X, squared=True).max()
424425

425-
coef = np.empty((y.shape[1], n_features))
426+
coef = np.empty((y.shape[1], n_features), dtype=X.dtype)
426427
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)
428429
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)}
430432
coef_, n_iter_, _ = sag_solver(
431433
X, target.ravel(), sample_weight, 'squared', alpha_i, 0,
432434
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,
479481

480482
def fit(self, X, y, sample_weight=None):
481483

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]
488486
X, y = check_X_y(X, y, ['csr', 'csc', 'coo'], dtype=_dtype,
489487
multi_output=True, y_numeric=True)
490488

@@ -502,14 +500,14 @@ def fit(self, X, y, sample_weight=None):
502500
X, y, alpha=self.alpha, sample_weight=sample_weight,
503501
max_iter=self.max_iter, tol=self.tol, solver=self.solver,
504502
random_state=self.random_state, return_n_iter=True,
505-
return_intercept=True)
503+
return_intercept=True, check_input=False)
506504
self.intercept_ += y_offset
507505
else:
508506
self.coef_, self.n_iter_ = ridge_regression(
509507
X, y, alpha=self.alpha, sample_weight=sample_weight,
510508
max_iter=self.max_iter, tol=self.tol, solver=self.solver,
511509
random_state=self.random_state, return_n_iter=True,
512-
return_intercept=False)
510+
return_intercept=False, check_input=False)
513511
self._set_intercept(X_offset, y_offset, X_scale)
514512

515513
return self
@@ -1013,10 +1011,12 @@ def fit(self, X, y, sample_weight=None):
10131011
-------
10141012
self : object
10151013
"""
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],
10171016
multi_output=True, y_numeric=True)
10181017
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)
10201020
n_samples, n_features = X.shape
10211021

10221022
X, y, X_offset, y_offset, X_scale = LinearModel._preprocess_data(

sklearn/linear_model/tests/test_ridge.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,9 @@ def test_ridge_classifier_no_support_multilabel():
879879
assert_raises(ValueError, RidgeClassifier().fit, X, y)
880880

881881

882-
def test_dtype_match():
882+
@pytest.mark.parametrize(
883+
"solver", ["svd", "sparse_cg", "cholesky", "lsqr", "sag", "saga"])
884+
def test_dtype_match(solver):
883885
rng = np.random.RandomState(0)
884886
alpha = 1.0
885887

@@ -889,25 +891,22 @@ def test_dtype_match():
889891
X_32 = X_64.astype(np.float32)
890892
y_32 = y_64.astype(np.float32)
891893

892-
solvers = ["svd", "sparse_cg", "cholesky", "lsqr"]
893-
for solver in solvers:
894-
895-
# Check type consistency 32bits
896-
ridge_32 = Ridge(alpha=alpha, solver=solver)
897-
ridge_32.fit(X_32, y_32)
898-
coef_32 = ridge_32.coef_
899-
900-
# Check type consistency 64 bits
901-
ridge_64 = Ridge(alpha=alpha, solver=solver)
902-
ridge_64.fit(X_64, y_64)
903-
coef_64 = ridge_64.coef_
904-
905-
# Do the actual checks at once for easier debug
906-
assert coef_32.dtype == X_32.dtype
907-
assert coef_64.dtype == X_64.dtype
908-
assert ridge_32.predict(X_32).dtype == X_32.dtype
909-
assert ridge_64.predict(X_64).dtype == X_64.dtype
910-
assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5)
894+
# Check type consistency 32bits
895+
ridge_32 = Ridge(alpha=alpha, solver=solver)
896+
ridge_32.fit(X_32, y_32)
897+
coef_32 = ridge_32.coef_
898+
899+
# Check type consistency 64 bits
900+
ridge_64 = Ridge(alpha=alpha, solver=solver)
901+
ridge_64.fit(X_64, y_64)
902+
coef_64 = ridge_64.coef_
903+
904+
# Do the actual checks at once for easier debug
905+
assert coef_32.dtype == X_32.dtype
906+
assert coef_64.dtype == X_64.dtype
907+
assert ridge_32.predict(X_32).dtype == X_32.dtype
908+
assert ridge_64.predict(X_64).dtype == X_64.dtype
909+
assert_almost_equal(ridge_32.coef_, ridge_64.coef_, decimal=5)
911910

912911

913912
def test_dtype_match_cholesky():

0 commit comments

Comments
 (0)
0