8000 CLN remove redundant default parameters in examples and tests (#14590) · qdeffense/scikit-learn@f13c9c0 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit f13c9c0

Browse files
qdeffenseTomDLT
authored andcommitted
CLN remove redundant default parameters in examples and tests (scikit-learn#14590)
remove redundant 'fit_intercept=True' in examples and tests along with some instances of other redundant parameters (max_iter=100, C=1 and alpha=1.0)
1 parent 36bca23 commit f13c9c0

13 files changed

+43
-57
lines changed

benchmarks/bench_sparsify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def sparsity_ratio(X):
7777
print("test data sparsity: %f" % sparsity_ratio(X_test))
7878

7979
###############################################################################
80-
clf = SGDRegressor(penalty='l1', alpha=.2, fit_intercept=True, max_iter=2000,
80+
clf = SGDRegressor(penalty='l1', alpha=.2, max_iter=2000,
8181
tol=None)
8282
clf.fit(X_train, y_train)
8383
print("model sparsity: %f" % sparsity_ratio(clf.coef_))

examples/applications/plot_prediction_latency.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ def plot_benchmark_throughput(throughputs, configuration):
278278
'estimators': [
279279
{'name': 'Linear Model',
280280
'instance': SGDRegressor(penalty='elasticnet', alpha=0.01,
281-
l1_ratio=0.25, fit_intercept=True,
282-
tol=1e-4),
281+
l1_ratio=0.25, tol=1e-4),
283282
'complexity_label': 'non-zero coefficients',
284283
341A 'complexity_computer': lambda clf: np.count_nonzero(clf.coef_)},
285284
{'name': 'RandomForest',

examples/linear_model/plot_huber_vs_ridge.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@
4545
x = np.linspace(X.min(), X.max(), 7)
4646
epsilon_values = [1.35, 1.5, 1.75, 1.9]
4747
for k, epsilon in enumerate(epsilon_values):
48-
huber = HuberRegressor(fit_intercept=True, alpha=0.0, max_iter=100,
49-
epsilon=epsilon)
48+
huber = HuberRegressor(alpha=0.0, epsilon=epsilon)
5049
huber.fit(X, y)
5150
coef_ = huber.coef_ * x + huber.intercept_
5251
plt.plot(x, coef_, colors[k], label="huber loss, %s" % epsilon)
5352

5453
# Fit a ridge regressor to compare it to huber regressor.
55-
ridge = Ridge(fit_intercept=True, alpha=0.0, random_state=0, normalize=True)
54+
ridge = Ridge(alpha=0.0, random_state=0, normalize=True)
5655
ridge.fit(X, y)
5756
coef_ridge = ridge.coef_
5857
coef_ = ridge.coef_ * x + ridge.intercept_

examples/linear_model/plot_sgd_separating_hyperplane.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)
1919

2020
# fit the model
21-
clf = SGDClassifier(loss="hinge", alpha=0.01, max_iter=200,
22-
fit_intercept=True)
21+
clf = SGDClassifier(loss="hinge", alpha=0.01, max_iter=200)
22+
2323
clf.fit(X, Y)
2424

2525
# plot the line, the points, and the nearest vectors to the plane

examples/linear_model/plot_sparse_logistic_regression_20newsgroups.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@
7474
(model_params['name'], solver, this_max_iter))
7575
lr = LogisticRegression(solver=solver,
7676
multi_class=model,
77-
C=1,
7877
penalty='l1',
79-
fit_intercept=True,
8078
max_iter=this_max_iter,
8179
random_state=42,
8280
)

sklearn/linear_model/tests/test_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ def test_fit_intercept():
123123
y = np.array([1, 1])
124124

125125
lr2_without_intercept = LinearRegression(fit_intercept=False).fit(X2, y)
126-
lr2_with_intercept = LinearRegression(fit_intercept=True).fit(X2, y)
126+
lr2_with_intercept = LinearRegression().fit(X2, y)
127127

128128
lr3_without_intercept = LinearRegression(fit_intercept=False).fit(X3, y)
129-
lr3_with_intercept = LinearRegression(fit_intercept=True).fit(X3, y)
129+
lr3_with_intercept = LinearRegression().fit(X3, y)
130130

131131
assert (lr2_with_intercept.coef_.shape ==
132132
lr2_without_intercept.coef_.shape)
@@ -179,7 +179,7 @@ def test_linear_regression_multiple_outcome(random_state=0):
179179
Y = np.vstack((y, y)).T
180180
n_features = X.shape[1]
181181

182-
reg = LinearRegression(fit_intercept=True)
182+
reg = LinearRegression()
183183
reg.fit((X), Y)
184184
assert reg.coef_.shape == (2, n_features)
185185
Y_pred = reg.predict(X)

sklearn/linear_model/tests/test_coordinate_descent.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,10 @@ def test_enet_cv_positive_constraint():
359359

360360

361361
def test_uniform_targets():
362-
enet = ElasticNetCV(fit_intercept=True, n_alphas=3)
363-
m_enet = MultiTaskElasticNetCV(fit_intercept=True, n_alphas=3)
364-
lasso = LassoCV(fit_intercept=True, n_alphas=3)
365-
m_lasso = MultiTaskLassoCV(fit_intercept=True, n_alphas=3)
362+
enet = ElasticNetCV(n_alphas=3)
363+
m_enet = MultiTaskElasticNetCV(n_alphas=3)
364+
lasso = LassoCV(n_alphas=3)
365+
m_lasso = MultiTaskLassoCV(n_alphas=3)
366366

367367
models_single_task = (enet, lasso)
368368
models_multi_task = (m_enet, m_lasso)
@@ -432,7 +432,7 @@ def test_enet_multitarget():
432432
n_targets = 3
433433
X, y, _, _ = build_dataset(n_samples=10, n_features=8,
434434
n_informative_features=10, n_targets=n_targets)
435-
estimator = ElasticNet(alpha=0.01, fit_intercept=True)
435+
estimator = ElasticNet(alpha=0.01)
436436
estimator.fit(X, y)
437437
coef, intercept, dual_gap = (estimator.coef_, estimator.intercept_,
438438
estimator.dual_gap_)
@@ -695,8 +695,7 @@ def test_enet_copy_X_False_check_input_False():
695695
def test_overrided_gram_matrix():
696696
X, y, _, _ = build_dataset(n_samples=20, n_features=10)
697697
Gram = X.T.dot(X)
698-
clf = ElasticNet(selection='cyclic', tol=1e-8, precompute=Gram,
699-
fit_intercept=True)
698+
clf = ElasticNet(selection='cyclic', tol=1e-8, precompute=Gram)
700699
assert_warns_message(UserWarning,
701700
"Gram matrix was provided but X was centered"
702701
" to fit intercept, "

sklearn/linear_model/tests/test_huber.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def make_regression_with_outliers(n_samples=50, n_features=20):
3232
def test_huber_equals_lr_for_high_epsilon():
3333
# Test that Ridge matches LinearRegression for large epsilon
3434
X, y = make_regression_with_outliers()
35-
lr = LinearRegression(fit_intercept=True)
35+
lr = LinearRegression()
3636
lr.fit(X, y)
37-
huber = HuberRegressor(fit_intercept=True, epsilon=1e3, alpha=0.0)
37+
huber = HuberRegressor(epsilon=1e3, alpha=0.0)
3838
huber.fit(X, y)
3939
assert_almost_equal(huber.coef_, lr.coef_, 3)
4040
assert_almost_equal(huber.int 57AE ercept_, lr.intercept_, 2)
@@ -74,7 +74,7 @@ def test_huber_sample_weights():
7474
# Test sample_weights implementation in HuberRegressor"""
7575

7676
X, y = make_regression_with_outliers()
77-
huber = HuberRegressor(fit_intercept=True)
77+
huber = HuberRegressor()
7878
huber.fit(X, y)
7979
huber_coef = huber.coef_
8080
huber_intercept = huber.intercept_
@@ -108,19 +108,19 @@ def test_huber_sample_weights():
108108

109109
# Test sparse implementation with sample weights.
110110
X_csr = sparse.csr_matrix(X)
111-
huber_sparse = HuberRegressor(fit_intercept=True)
111+
huber_sparse = HuberRegressor()
112112
huber_sparse.fit(X_csr, y, sample_weight=sample_weight)
113113
assert_array_almost_equal(huber_sparse.coef_ / scale,
114114
huber_coef / scale)
115115

116116

117117
def test_huber_sparse():
118118
X, y = make_regression_with_outliers()
119-
huber = HuberRegressor(fit_intercept=True, alpha=0.1)
119+
huber = HuberRegressor(alpha=0.1)
120120
huber.fit(X, y)
121121

122122
X_csr = sparse.csr_matrix(X)
123-
huber_sparse = HuberRegressor(fit_intercept=True, alpha=0.1)
123+
huber_sparse = HuberRegressor(alpha=0.1)
124124
huber_sparse.fit(X_csr, y)
125125
assert_array_almost_equal(huber_sparse.coef_, huber.coef_)
126126
assert_array_equal(huber.outliers_, huber_sparse.outliers_)
@@ -170,8 +170,8 @@ def test_huber_and_sgd_same_results():
170170
def test_huber_warm_start():
171171
X, y = make_regression_with_outliers()
172172
huber_warm = HuberRegressor(
173-
fit_intercept=True, alpha=1.0, max_iter=10000, warm_start=True,
174-
tol=1e-1)
173+
alpha=1.0, max_iter=10000, warm_start=True, tol=1e-1)
174+
175175
huber_warm.fit(X, y)
176176
huber_warm_coef = huber_warm.coef_.copy()
177177
huber_warm.fit(X, y)
@@ -186,7 +186,7 @@ def test_huber_warm_start():
186186
def test_huber_better_r2_score():
187187
# Test that huber returns a better r2 score than non-outliers"""
188188
X, y = make_regression_with_outliers()
189-
huber = HuberRegressor(fit_intercept=True, alpha=0.01, max_iter=100)
189+
huber = HuberRegressor(alpha=0.01)
190190
huber.fit(X, y)
191191
linear_loss = np.dot(X, huber.coef_) + huber.intercept_ - y
192192
mask = np.abs(linear_loss) < huber.epsilon * huber.scale_
@@ -196,7 +196,7 @@ def test_huber_better_r2_score():
196196
# The Ridge regressor should be influenced by the outliers and hence
197197
# give a worse score on the non-outliers as compared to the huber
198198
# regressor.
199-
ridge = Ridge(fit_intercept=True, alpha=0.01)
199+
ridge = Ridge(alpha=0.01)
200200
ridge.fit(X, y)
201201
ridge_score = ridge.score(X[mask], y[mask])
202202
ridge_outlier_score = ridge.score(X[~mask], y[~mask])

sklearn/linear_model/tests/test_least_angle.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ def test_lasso_lars_vs_lasso_cd_early_stopping():
302302
for alpha_min in alphas_min:
303303
alphas, _, lasso_path = linear_model.lars_path(X, y, method='lasso',
304304
alpha_min=alpha_min)
305-
lasso_cd = linear_model.Lasso(fit_intercept=True, normalize=True,
306-
tol=1e-8)
305+
lasso_cd = linear_model.Lasso(normalize=True, tol=1e-8)
307306
lasso_cd.alpha = alphas[-1]
308307
lasso_cd.fit(X, y)
309308
error = linalg.norm(lasso_path[:, -1] - lasso_cd.coef_)
@@ -688,8 +687,7 @@ def test_lasso_lars_vs_R_implementation():
688687
[0, 0, -1.569380717440311, -5.924804108067312,
689688
-7.996385265061972]])
690689

691-
model_lasso_lars2 = linear_model.LassoLars(alpha=0, fit_intercept=True,
692-
normalize=True)
690+
model_lasso_lars2 = linear_model.LassoLars(alpha=0, normalize=True)
693691
model_lasso_lars2.fit(X, y)
694692
skl_betas2 = model_lasso_lars2.coef_path_
695693

sklearn/linear_model/tests/test_logistic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ def test_consistency_path():
373373
for solver in ('lbfgs', 'newton-cg', 'liblinear', 'sag', 'saga'):
374374
Cs = [1e3]
375375
coefs, Cs, _ = f(_logistic_regression_path)(
376-
X, y, Cs=Cs, fit_intercept=True, tol=1e-6, solver=solver,
376+
X, y, Cs=Cs, tol=1e-6, solver=solver,
377377
intercept_scaling=10000., random_state=0, multi_class='ovr')
378-
lr = LogisticRegression(C=Cs[0], fit_intercept=True, tol=1e-4,
378+
lr = LogisticRegression(C=Cs[0], tol=1e-4,
379379
intercept_scaling=10000., random_state=0,
380380
multi_class='ovr', solver=solver)
381381
lr.fit(X, y)
@@ -596,9 +596,9 @@ def test_logistic_cv_sparse():
596596
X[X < 1.0] = 0.0
597597
csr = sp.csr_matrix(X)
598598

599-
clf = LogisticRegressionCV(fit_intercept=True)
599+
clf = LogisticRegressionCV()
600600
clf.fit(X, y)
601-
clfs = LogisticRegressionCV(fit_intercept=True)
601+
clfs = LogisticRegressionCV()
602602
clfs.fit(csr, y)
603603
assert_array_almost_equal(clfs.coef_, clf.coef_)
604604
assert_array_almost_equal(clfs.intercept_, clf.intercept_)

0 commit comments

Comments
 (0)
0