66# License: Simplified BSD
77
88import numpy as np
9- import scipy .sparse as sp
10- from scipy import linalg
11- from scipy .sparse import linalg as sp_linalg
129
1310from .base import LinearModel
1411from ..utils .extmath import safe_sparse_dot
@@ -85,6 +82,7 @@ def fit(self, X, y, sample_weight=1.0, solver="default", **params):
8582 X , y , Xmean , ymean = \
8683 LinearModel ._center_data (X , y , self .fit_intercept )
8784
85+ import scipy .sparse as sp
8886 if sp .issparse (X ):
8987 self ._solve_sparse (X , y , sample_weight )
9088 else :
@@ -116,6 +114,7 @@ def _solve_dense(self, X, y, sample_weight):
116114 def _solve_sparse (self , X , y , sample_weight ):
117115 n_samples , n_features = X .shape
118116
117+ import scipy .sparse as sp
119118 if n_features > n_samples or \
120119 isinstance (sample_weight , np .ndarray ) or \
121120 sample_weight != 1.0 :
@@ -132,14 +131,17 @@ def _solve_sparse(self, X, y, sample_weight):
132131 def _solve (self , A , b ):
133132 if self .solver == "cg" :
134133 # this solver cannot handle a 2-d b.
134+ from scipy .sparse import linalg as sp_linalg
135135 sol , error = sp_linalg .cg (A , b )
136136 if error :
137137 raise ValueError ("Failed with error code %d" % error )
138138 return sol
139139 else :
140+ import scipy .sparse as sp
140141 # we are working with dense symmetric positive A
141142 if sp .issparse (A ):
142143 A = A .todense ()
144+ from scipy import linalg
143145 return linalg .solve (A , b , sym_pos = True , overwrite_a = True )
144146
145147
@@ -255,6 +257,7 @@ def __init__(self, alphas=[0.1, 1.0, 10.0], fit_intercept=True,
255257 def _pre_compute (self , X , y ):
256258 # even if X is very sparse, K is usually very dense
257259 K = safe_sparse_dot (X , X .T , dense_output = True )
260+ from scipy import linalg
258261 v , Q = linalg .eigh (K )
259262 return K , v , Q
260263
0 commit comments