Skip to content

Commit 709afd0

Browse files
committed
style fixes
1 parent 3617030 commit 709afd0

2 files changed

Lines changed: 63 additions & 87 deletions

File tree

sklearn/linear_model/least_angle.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ..externals.six.moves import xrange
2828
from ..externals.six import string_types
2929

30-
solve_triangular_args = {'check_finite': False}
30+
SOLVE_TRIANGULAR_ARGS = {'check_finite': False}
3131

3232

3333
def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
@@ -285,7 +285,7 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
285285
L[n_active, :n_active],
286286
trans=0, lower=1,
287287
overwrite_b=True,
288-
**solve_triangular_args)
288+
**SOLVE_TRIANGULAR_ARGS)
289289

290290
v = np.dot(L[n_active, :n_active], L[n_active, :n_active])
291291
diag = max(np.sqrt(np.abs(c - v)), eps)
@@ -338,9 +338,9 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
338338
break
339339

340340
# least squares solution
341-
least_squares, info = solve_cholesky(L[:n_active, :n_active],
342-
sign_active[:n_active],
343-
lower=True)
341+
least_squares, _ = solve_cholesky(L[:n_active, :n_active],
342+
sign_active[:n_active],
343+
lower=True)
344344

345345
if least_squares.size == 1 and least_squares == 0:
346346
# This happens because sign_active[:n_active] = 0
@@ -356,7 +356,7 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
356356
L_ = L[:n_active, :n_active].copy()
357357
while not np.isfinite(AA):
358358
L_.flat[::n_active + 1] += (2 ** i) * eps
359-
least_squares, info = solve_cholesky(
359+
least_squares, _ = solve_cholesky(
360360
L_, sign_active[:n_active], lower=True)
361361
tmp = max(np.sum(least_squares * sign_active[:n_active]),
362362
eps)
@@ -427,8 +427,8 @@ def lars_path(X, y, Xy=None, Gram=None, max_iter=500,
427427
if drop and method == 'lasso':
428428

429429
# handle the case when idx is not length of 1
430-
[arrayfuncs.cholesky_delete(L[:n_active, :n_active], ii) for ii in
431-
idx]
430+
for ii in idx:
431+
arrayfuncs.cholesky_delete(L[:n_active, :n_active], ii)
432432

433433
n_active -= 1
434434
m, n = idx, n_active
@@ -606,7 +606,8 @@ def __init__(self, fit_intercept=True, verbose=False, normalize=True,
606606
self.copy_X = copy_X
607607
self.fit_path = fit_path
608608

609-
def _get_gram(self, precompute, X, y):
609+
@staticmethod
610+
def _get_gram(precompute, X, y):
610611
if (not hasattr(precompute, '__array__')) and (
611612
(precompute is True) or
612613
(precompute == 'auto' and X.shape[0] > X.shape[1]) or
@@ -1135,7 +1136,7 @@ def fit(self, X, y):
11351136
all_alphas = all_alphas[::stride]
11361137

11371138
mse_path = np.empty((len(all_alphas), len(cv_paths)))
1138-
for index, (alphas, active, coefs, residues) in enumerate(cv_paths):
1139+
for index, (alphas, _, _, residues) in enumerate(cv_paths):
11391140
alphas = alphas[::-1]
11401141
residues = residues[::-1]
11411142
if alphas[0] != 0:
@@ -1481,7 +1482,7 @@ def fit(self, X, y, copy_X=True):
14811482

14821483
Gram = self.precompute
14831484

1484-
alphas_, active_, coef_path_, self.n_iter_ = lars_path(
1485+
alphas_, _, coef_path_, self.n_iter_ = lars_path(
14851486
X, y, Gram=Gram, copy_X=copy_X, copy_Gram=True, alpha_min=0.0,
14861487
method='lasso', verbose=self.verbose, max_iter=max_iter,
14871488
eps=self.eps, return_n_iter=True, positive=self.positive)

sklearn/linear_model/tests/test_least_angle.py

Lines changed: 51 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
from distutils.version import LooseVersion
44

55
import numpy as np
6-
from scipy import linalg
7-
86
import pytest
7+
from scipy import linalg
98

109
from sklearn.model_selection import train_test_split
1110
from sklearn.utils.testing import assert_equal
@@ -22,10 +21,12 @@
2221
from sklearn import linear_model, datasets
2322
from sklearn.linear_model.least_angle import _lars_path_residues
2423

24+
# TODO: use another dataset that has multiple drops
2525
diabetes = datasets.load_diabetes()
2626
X, y = diabetes.data, diabetes.target
27-
28-
# TODO: use another dataset that has multiple drops
27+
G = np.dot(X.T, X)
28+
Xy = np.dot(X.T, y)
29+
n_samples = y.size
2930

3031

3132
def test_simple():
@@ -38,12 +39,12 @@ def test_simple():
3839
try:
3940
sys.stdout = StringIO()
4041

41-
alphas_, active, coef_path_ = linear_model.lars_path(
42-
diabetes.data, diabetes.target, method="lar", verbose=10)
42+
_, _, coef_path_ = linear_model.lars_path(
43+
X, y, method='lar', verbose=10)
4344

4445
sys.stdout = old_stdout
4546

46-
for (i, coef_) in enumerate(coef_path_.T):
47+
for i, coef_ in enumerate(coef_path_.T):
4748
res = y - np.dot(X, coef_)
4849
cov = np.dot(X.T, res)
4950
C = np.max(abs(cov))
@@ -61,9 +62,8 @@ def test_simple():
6162
def test_simple_precomputed():
6263
# The same, with precomputed Gram matrix
6364

64-
G = np.dot(diabetes.data.T, diabetes.data)
65-
alphas_, active, coef_path_ = linear_model.lars_path(
66-
diabetes.data, diabetes.target, Gram=G, method="lar")
65+
_, _, coef_path_ = linear_model.lars_path(
66+
X, y, Gram=G, method='lar')
6767

6868
for i, coef_ in enumerate(coef_path_.T):
6969
res = y - np.dot(X, coef_)
@@ -80,10 +80,7 @@ def test_simple_precomputed():
8080

8181
def test_all_precomputed():
8282
# Test that lars_path with precomputed Gram and Xy gives the right answer
83-
X, y = diabetes.data, diabetes.target
84-
G = np.dot(X.T, X)
85-
Xy = np.dot(X.T, y)
86-
for method in 'lar', 'lasso':
83+
for method in ('lar', 'lasso'):
8784
output = linear_model.lars_path(X, y, method=method)
8885
output_pre = linear_model.lars_path(X, y, Gram=G, Xy=Xy, method=method)
8986
for expected, got in zip(output, output_pre):
@@ -95,7 +92,7 @@ def test_all_precomputed():
9592
def test_lars_lstsq():
9693
# Test that Lars gives least square solution at the end
9794
# of the path
98-
X1 = 3 * diabetes.data # use un-normalized dataset
95+
X1 = 3 * X # use un-normalized dataset
9996
clf = linear_model.LassoLars(alpha=0.)
10097
clf.fit(X1, y)
10198
# Avoid FutureWarning about default value change when numpy >= 1.14
@@ -109,7 +106,7 @@ def test_lars_lstsq():
109106
def test_lasso_gives_lstsq_solution():
110107
# Test that Lars Lasso gives least square solution at the end
111108
# of the path
112-
alphas_, active, coef_path_ = linear_model.lars_path(X, y, method="lasso")
109+
_, _, coef_path_ = linear_model.lars_path(X, y, method='lasso')
113110
coef_lstsq = np.linalg.lstsq(X, y)[0]
114111
assert_array_almost_equal(coef_lstsq, coef_path_[:, -1])
115112

@@ -122,8 +119,8 @@ def test_collinearity():
122119
y = np.array([1., 0., 0])
123120
rng = np.random.RandomState(0)
124121

125-
f = ignore_warnings
126-
_, _, coef_path_ = f(linear_model.lars_path)(X, y, alpha_min=0.01)
122+
_, _, coef_path_ = ignore_warnings(linear_model.lars_path)(
123+
X, y, alpha_min=0.01)
127124
assert_true(not np.isnan(coef_path_).any())
128125
residual = np.dot(X, coef_path_[:, -1]) - y
129126
assert_less((residual ** 2).sum(), 1.) # just make sure it's bounded
@@ -140,26 +137,21 @@ def test_collinearity():
140137

141138
def test_no_path():
142139
# Test that the ``return_path=False`` option returns the correct output
143-
144-
alphas_, active_, coef_path_ = linear_model.lars_path(
145-
diabetes.data, diabetes.target, method="lar")
146-
alpha_, active, coef = linear_model.lars_path(
147-
diabetes.data, diabetes.target, method="lar", return_path=False)
140+
alphas_, _, coef_path_ = linear_model.lars_path(
141+
X, y, method='lar')
142+
alpha_, _, coef = linear_model.lars_path(
143+
X, y, method='lar', return_path=False)
148144

149145
assert_array_almost_equal(coef, coef_path_[:, -1])
150146
assert_true(alpha_ == alphas_[-1])
151147

152148

153149
def test_no_path_precomputed():
154150
# Test that the ``return_path=False`` option with Gram remains correct
155-
156-
G = np.dot(diabetes.data.T, diabetes.data)
157-
158-
alphas_, active_, coef_path_ = linear_model.lars_path(
159-
diabetes.data, diabetes.target, method="lar", Gram=G)
160-
alpha_, active, coef = linear_model.lars_path(
161-
diabetes.data, diabetes.target, method="lar", Gram=G,
162-
return_path=False)
151+
alphas_, _, coef_path_ = linear_model.lars_path(
152+
X, y, method='lar', Gram=G)
153+
alpha_, _, coef = linear_model.lars_path(
154+
X, y, method='lar', Gram=G, return_path=False)
163155

164156
assert_array_almost_equal(coef, coef_path_[:, -1])
165157
assert_true(alpha_ == alphas_[-1])
@@ -172,25 +164,20 @@ def test_no_path_all_precomputed():
172164
G = np.dot(X.T, X)
173165
Xy = np.dot(X.T, y)
174166

175-
alphas_, active_, coef_path_ = linear_model.lars_path(
176-
X, y, method="lasso", Gram=G, Xy=Xy, alpha_min=0.9)
177-
print("---")
178-
alpha_, active, coef = linear_model.lars_path(
179-
X, y, method="lasso", Gram=G, Xy=Xy, alpha_min=0.9, return_path=False)
167+
alphas_, _, coef_path_ = linear_model.lars_path(
168+
X, y, method='lasso', Xy=Xy, Gram=G, alpha_min=0.9)
169+
alpha_, _, coef = linear_model.lars_path(
170+
X, y, method='lasso', Gram=G, Xy=Xy, alpha_min=0.9, return_path=False)
180171

181172
assert_array_almost_equal(coef, coef_path_[:, -1])
182173
assert_true(alpha_ == alphas_[-1])
183174

184175

185176
@pytest.mark.filterwarnings('ignore: You should specify a value') # 0.22
186-
@pytest.mark.parametrize(
187-
'classifier',
188-
[linear_model.Lars, linear_model.LarsCV, linear_model.LassoLarsIC])
177+
@pytest.mark.parametrize('classifier', [linear_model.Lars, linear_model.LarsCV,
178+
linear_model.LassoLarsIC])
189179
def test_lars_precompute(classifier):
190180
# Check for different values of precompute
191-
X, y = diabetes.data, diabetes.target
192-
G = np.dot(X.T, X)
193-
194181
clf = classifier(precompute=G)
195182
output_1 = ignore_warnings(clf.fit)(X, y).coef_
196183
for precompute in [True, False, 'auto', None]:
@@ -203,7 +190,7 @@ def test_singular_matrix():
203190
# Test when input is a singular matrix
204191
X1 = np.array([[1, 1.], [1., 1.]])
205192
y1 = np.array([1, 1])
206-
alphas, active, coef_path = linear_model.lars_path(X1, y1)
193+
_, _, coef_path = linear_model.lars_path(X1, y1)
207194
assert_array_almost_equal(coef_path.T, [[0, 0], [1, 0]])
208195

209196

@@ -212,14 +199,14 @@ def test_rank_deficient_design():
212199
# deficient input data (with n_features < rank) in the same way
213200
# as coordinate descent Lasso
214201
y = [5, 0, 5]
215-
for X in ([[5, 0],
202+
for X in (
203+
[[5, 0],
216204
[0, 5],
217205
[10, 10]],
218-
219206
[[10, 10, 0],
220207
[1e-32, 0, 0],
221-
[0, 0, 1]],
222-
):
208+
[0, 0, 1]]
209+
):
223210
# To be able to use the coefs to compute the objective function,
224211
# we need to turn off normalization
225212
lars = linear_model.LassoLars(.1, normalize=False)
@@ -234,7 +221,7 @@ def test_rank_deficient_design():
234221
assert_less(obj_lars, obj_cd * (1. + 1e-8))
235222

236223

237-
def test_lasso_lars_vs_lasso_cd(verbose=False):
224+
def test_lasso_lars_vs_lasso_cd():
238225
# Test that LassoLars and Lasso using coordinate descent give the
239226
# same results.
240227
X = 3 * diabetes.data
@@ -271,7 +258,7 @@ def test_lasso_lars_vs_lasso_cd(verbose=False):
271258
assert_less(error, 0.01)
272259

273260

274-
def test_lasso_lars_vs_lasso_cd_early_stopping(verbose=False):
261+
def test_lasso_lars_vs_lasso_cd_early_stopping():
275262
# Test that LassoLars and Lasso using coordinate descent give the
276263
# same results when early stopping is used.
277264
# (test : before, in the middle, and in the last part of the path)
@@ -393,8 +380,7 @@ def test_lars_n_nonzero_coefs(verbose=False):
393380
@ignore_warnings
394381
def test_multitarget():
395382
# Assure that estimators receiving multidimensional y do the right thing
396-
X = diabetes.data
397-
Y = np.vstack([diabetes.target, diabetes.target ** 2]).T
383+
Y = np.vstack([y, y ** 2]).T
398384
n_targets = Y.shape[1]
399385
estimators = [
400386
linear_model.LassoLars(),
@@ -439,10 +425,9 @@ def test_lars_cv():
439425
@pytest.mark.filterwarnings('ignore::FutureWarning')
440426
def test_lars_cv_max_iter():
441427
with warnings.catch_warnings(record=True) as w:
442-
X = diabetes.data
443-
y = diabetes.target
444428
rng = np.random.RandomState(42)
445429
x = rng.randn(len(y))
430+
X = diabetes.data
446431
X = np.c_[X, x, x] # add correlated features
447432
lars_cv = linear_model.LassoLarsCV(max_iter=5)
448433
lars_cv.fit(X, y)
@@ -458,7 +443,6 @@ def test_lasso_lars_ic():
458443
lars_aic = linear_model.LassoLarsIC('aic')
459444
rng = np.random.RandomState(42)
460445
X = diabetes.data
461-
y = diabetes.target
462446
X = np.c_[X, rng.randn(X.shape[0], 5)] # add 5 bad features
463447
lars_bic.fit(X, y)
464448
lars_aic.fit(X, y)
@@ -498,52 +482,43 @@ def test_lars_path_positive_constraint():
498482
# Once deprecation of LAR + positive option is done use these:
499483
# assert_raises(ValueError, linear_model.lars_path, diabetes['data'],
500484
# diabetes['target'], method='lar', positive=True)
501-
502-
with pytest.warns(DeprecationWarning, match="broken"):
485+
with pytest.warns(DeprecationWarning, match='broken'):
503486
linear_model.lars_path(diabetes['data'], diabetes['target'],
504487
return_path=True, method='lar',
505488
positive=True)
506-
507489
method = 'lasso'
508-
alpha, active, coefs = \
509-
linear_model.lars_path(diabetes['data'], diabetes['target'],
510-
return_path=True, method=method,
490+
_, _, coefs = \
491+
linear_model.lars_path(X, y, return_path=True, method=method,
511492
positive=False)
512493
assert_true(coefs.min() < 0)
513494

514-
alpha, active, coefs = \
515-
linear_model.lars_path(diabetes['data'], diabetes['target'],
516-
return_path=True, method=method,
495+
_, _, coefs = \
496+
linear_model.lars_path(X, y, return_path=True, method=method,
517497
positive=True)
518498
assert_true(coefs.min() >= 0)
519499

520500

521-
# now we gonna test the positive option for all estimator classes
522-
523-
default_parameter = {'fit_intercept': False}
524-
525-
estimator_parameter_map = {'LassoLars': {'alpha': 0.1},
526-
'LassoLarsCV': {},
527-
'LassoLarsIC': {}}
528-
529-
530501
@pytest.mark.filterwarnings('ignore: You should specify a value') # 0.22
531502
def test_estimatorclasses_positive_constraint():
532503
# testing the transmissibility for the positive option of all estimator
533504
# classes in this same function here
505+
default_parameter = {'fit_intercept': False}
534506

507+
estimator_parameter_map = {'LassoLars': {'alpha': 0.1},
508+
'LassoLarsCV': {},
509+
'LassoLarsIC': {}}
535510
for estname in estimator_parameter_map:
536511
params = default_parameter.copy()
537512
params.update(estimator_parameter_map[estname])
538513
estimator = getattr(linear_model, estname)(positive=False, **params)
539-
estimator.fit(diabetes['data'], diabetes['target'])
514+
estimator.fit(X, y)
540515
assert_true(estimator.coef_.min() < 0)
541516
estimator = getattr(linear_model, estname)(positive=True, **params)
542-
estimator.fit(diabetes['data'], diabetes['target'])
517+
estimator.fit(X, y)
543518
assert_true(min(estimator.coef_) >= 0)
544519

545520

546-
def test_lasso_lars_vs_lasso_cd_positive(verbose=False):
521+
def test_lasso_lars_vs_lasso_cd_positive():
547522
# Test that LassoLars and Lasso using coordinate descent give the
548523
# same results when using the positive option
549524

@@ -634,7 +609,7 @@ def test_lasso_lars_vs_R_implementation():
634609
0.025219751009936],
635610
[0, -3.577397088285891, -4.702795355871871,
636611
-7.016748621359461, -7.614898471899412, -0.336938391359179,
637-
0, 0, 0.001213370600853, 0.048162321585148],
612+
0, 0, 0.001213370600853, 0.048162321585148],
638613
[0, 0, 0, 2.231558436628169, 2.723267514525966,
639614
2.811549786389614, 2.813766976061531, 2.817462468949557,
640615
2.817368178703816, 2.816221090636795],

0 commit comments

Comments
 (0)