|
29 | 29 | from ..metrics.scorer import check_scoring
|
30 | 30 |
|
31 | 31 |
|
32 |
| -def _solve_sparse_cg(X, y, alpha, max_iter=None, tol=1e-3): |
| 32 | +def _solve_sparse_cg(X, y, alpha, max_iter=None, tol=1e-3, verbose=0): |
33 | 33 | n_samples, n_features = X.shape
|
34 | 34 | X1 = sp_linalg.aslinearoperator(X)
|
8000
35 | 35 | coefs = np.empty((y.shape[1], n_features))
|
@@ -64,9 +64,13 @@ def _mv(x):
|
64 | 64 | (n_features, n_features), matvec=mv, dtype=X.dtype)
|
65 | 65 | coefs[i], info = sp_linalg.cg(C, y_column, maxiter=max_iter,
|
66 | 66 | tol=tol)
|
67 |
| - if info != 0: |
| 67 | + if info < 0: |
68 | 68 | raise ValueError("Failed with error code %d" % info)
|
69 | 69 |
|
| 70 | + if max_iter is None and info > 0 and verbose: |
| 71 | + warnings.warn("sparse_cg did not converge after %d iterations." % |
| 72 | + info) |
| 73 | + |
70 | 74 | return coefs
|
71 | 75 |
|
72 | 76 |
|
@@ -187,7 +191,7 @@ def _deprecate_dense_cholesky(solver):
|
187 | 191 |
|
188 | 192 |
|
189 | 193 | def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
|
190 |
| - max_iter=None, tol=1e-3): |
| 194 | + max_iter=None, tol=1e-3, verbose=0): |
191 | 195 | """Solve the ridge equation by the method of normal equations.
|
192 | 196 |
|
193 | 197 | Parameters
|
@@ -239,6 +243,10 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
|
239 | 243 | tol: float
|
240 | 244 | Precision of the solution.
|
241 | 245 |
|
| 246 | + verbose: int |
| 247 | + Verbosity level. Setting verbose > 0 will display additional information |
| 248 | + depending on the solver used. |
| 249 | +
|
242 | 250 | Returns
|
243 | 251 | -------
|
244 | 252 | coef: array, shape = [n_features] or [n_targets, n_features]
|
@@ -306,7 +314,7 @@ def ridge_regression(X, y, alpha, sample_weight=None, solver='auto',
|
306 | 314 | raise ValueError('Solver %s not understood' % solver)
|
307 | 315 |
|
308 | 316 | if solver == 'sparse_cg':
|
309 |
| - coef = _solve_sparse_cg(X, y, alpha, max_iter, tol) |
| 317 | + coef = _solve_sparse_cg(X, y, alpha, max_iter, tol, verbose) |
310 | 318 |
|
311 | 319 | elif solver == "lsqr":
|
312 | 320 | coef = _solve_lsqr(X, y, alpha, max_iter, tol)
|
|
0 commit comments