8000 MNT Completes position arg deprecation (#17272) · sstalley/scikit-learn@fd23727 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd23727

Browse files
thomasjpfanadrinjalali
authored andcommitted
MNT Completes position arg deprecation (scikit-learn#17272)
* ENH Adds public functions with position args warning * BUG Circlar import * STY * BUG Add keywords * BUG Fixes warnings * ENH Adds other public functions * CLN Suggestion * CLN Suggestion * REV Revert pairwise * ENH Positoinal args for compute_class_weight
1 parent 467d95f commit fd23727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+198
-138
lines changed

examples/linear_model/plot_lasso_coordinate_descent_path.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
eps = 5e-3 # the smaller it is the longer is the path
3232

3333
print("Computing regularization path using the lasso...")
34-
alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps, fit_intercept=False)
34+
alphas_lasso, coefs_lasso, _ = lasso_path(X, y, eps=eps, fit_intercept=False)
3535

3636
print("Computing regularization path using the positive lasso...")
3737
alphas_positive_lasso, coefs_positive_lasso, _ = lasso_path(
38-
X, y, eps, positive=True, fit_intercept=False)
38+
X, y, eps=eps, positive=True, fit_intercept=False)
3939
print("Computing regularization path using the elastic net...")
4040
alphas_enet, coefs_enet, _ = enet_path(
4141
X, y, eps=eps, l1_ratio=0.8, fit_intercept=False)

sklearn/calibration.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ def predict(self, T):
506506
return expit(-(self.a_ * T + self.b_))
507507

508508

509-
def calibration_curve(y_true, y_prob, normalize=False, n_bins=5,
509+
@_deprecate_positional_args
510+
def calibration_curve(y_true, y_prob, *, normalize=False, n_bins=5,
510511
strategy='uniform'):
511512
"""Compute true and predicted probabilities for a calibration curve.
512513

sklearn/cluster/_affinity_propagation.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def all_equal_similarities():
3030
return all_equal_preferences() and all_equal_similarities()
3131

3232

33-
def affinity_propagation(S, preference=None, convergence_iter=15, max_iter=200,
34-
damping=0.5, copy=True, verbose=False,
33+
@_deprecate_positional_args
34+
def affinity_propagation(S, *, preference=None, convergence_iter=15,
35+
max_iter=200, damping=0.5, copy=True, verbose=False,
3536
return_n_iter=False, random_state='warn'):
3637
"""Perform Affinity Propagation Clustering of data
3738
@@ -411,7 +412,8 @@ def fit(self, X, y=None):
411412

412413
self.cluster_centers_indices_, self.labels_, self.n_iter_ = \
413414
affinity_propagation(
414-
self.affinity_matrix_, self.preference, max_iter=self.max_iter,
415+
self.affinity_matrix_, preference=self.preference,
416+
max_iter=self.max_iter,
415417
convergence_iter=self.convergence_iter, damping=self.damping,
416418
copy=self.copy, verbose=self.verbose, return_n_iter=True,
417419
random_state=self.random_state)

sklearn/cluster/_agglomerative.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def _single_linkage_tree(connectivity, n_samples, n_nodes, n_clusters,
134134
###############################################################################
135135
# Hierarchical tree building functions
136136

137-
def ward_tree(X, connectivity=None, n_clusters=None, return_distance=False):
137+
@_deprecate_positional_args
138+
def ward_tree(X, *, connectivity=None, n_clusters=None, return_distance=False):
138139
"""Ward clustering based on a Feature matrix.
139140
140141
Recursively merges the pair of clusters that minimally increases
@@ -875,7 +876,7 @@ def fit(self, X, y=None):
875876
distance_threshold = self.distance_threshold
876877

877878
return_distance = distance_threshold is not None
878-
out = memory.cache(tree_builder)(X, connectivity,
879+
out = memory.cache(tree_builder)(X, connectivity=connectivity,
879880
n_clusters=n_clusters,
880881
return_distance=return_distance,
881882
**kwargs)

sklearn/cluster/_dbscan.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
from ._dbscan_inner import dbscan_inner
2121

2222

23-
def dbscan(X, eps=0.5, min_samples=5, metric='minkowski', metric_params=None,
24-
algorithm='auto', leaf_size=30, p=2, sample_weight=None,
25-
n_jobs=None):
23+
@_deprecate_positional_args
24+
def dbscan(X, eps=0.5, *, min_samples=5, metric='minkowski',
25+
metric_params=None, algorithm='auto', leaf_size=30, p=2,
26+
sample_weight=None, n_jobs=None):
2627
"""Perform DBSCAN clustering from vector array or distance matrix.
2728
2829
Read more in the :ref:`User Guide <dbscan>`.

sklearn/cluster/_kmeans.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def _check_normalize_sample_weight(sample_weight, X):
182182
return sample_weight
183183

184184

185-
def k_means(X, n_clusters, sample_weight=None, init='k-means++',
185+
@_deprecate_positional_args
186+
def k_means(X, n_clusters, *, sample_weight=None, init='k-means++',
186187
precompute_distances='deprecated', n_init=10, max_iter=300,
187188
verbose=False, tol=1e-4, random_state=None, copy_x=True,
188189
n_jobs='deprecated', algorithm="auto", return_n_iter=False):

sklearn/cluster/_mean_shift.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from ..metrics.pairwise import pairwise_distances_argmin
2727

2828

29-
def estimate_bandwidth(X, quantile=0.3, n_samples=None, random_state=0,
29+
@_deprecate_positional_args
30+
def estimate_bandwidth(X, *, quantile=0.3, n_samples=None, random_state=0,
3031
n_jobs=None):
3132
"""Estimate the bandwidth to use with the mean-shift algorithm.
3233
@@ -106,7 +107,8 @@ def _mean_shift_single_seed(my_mean, X, nbrs, max_iter):
106107
return tuple(my_mean), len(points_within), completed_iterations
107108

108109

109-
def mean_shift(X, bandwidth=None, seeds=None, bin_seeding=False,
110+
@_deprecate_positional_args
111+
def mean_shift(X, *, bandwidth=None, seeds=None, bin_seeding=False,
110112
min_bin_freq=1, cluster_all=True, max_iter=300,
111113
n_jobs=None):
112114
"""Perform mean shift clustering of data using a flat kernel.

sklearn/cluster/_optics.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ def fit(self, X, y=None):
261261
# Extract clusters from the calculated orders and reachability
262262
if self.cluster_method == 'xi':
263263
labels_, clusters_ = cluster_optics_xi(
264-
self.reachability_,
265-
self.predecessor_,
266-
self.ordering_,
267-
self.min_samples,
268-
self.min_cluster_size,
269-
self.xi,
270-
self.predecessor_correction)
264+
reachability=self.reachability_,
265+
predecessor=self.predecessor_,
266+
ordering=self.ordering_,
267+
min_samples=self.min_samples,
268+
min_cluster_size=self.min_cluster_size,
269+
xi=self.xi,
270+
predecessor_correction=self.predecessor_correction)
271271
self.cluster_hierarchy_ = clusters_
272272
elif self.cluster_method == 'dbscan':
273273
if self.eps is None:
@@ -279,10 +279,10 @@ def fit(self, X, y=None):
279279
raise ValueError('Specify an epsilon smaller than %s. Got %s.'
280280
% (self.max_eps, eps))
281281

282-
labels_ = cluster_optics_dbscan(self.reachability_,
283-
self.core_distances_,
284-
self.ordering_,
285-
eps)
282+
labels_ = cluster_optics_dbscan(
283+
reachability=self.reachability_,
284+
core_distances=self.core_distances_,
285+
ordering=self.ordering_, eps=eps)
286286

287287
self.labels_ = labels_
288288
return self
@@ -339,7 +339,8 @@ def _compute_core_distances_(X, neighbors, min_samples, working_memory):
339339
return core_distances
340340

341341

342-
def compute_optics_graph(X, min_samples, max_eps, metric, p, metric_params,
342+
@_deprecate_positional_args
343+
def compute_optics_graph(X, *, min_samples, max_eps, metric, p, metric_params,
343344
algorithm, leaf_size, n_jobs):
344345
"""Computes the OPTICS reachability graph.
345346
@@ -538,7 +539,8 @@ def _set_reach_dist(core_distances_, reachability_, predecessor_,
538539
predecessor_[unproc[improved]] = point_index
539540

540541

541-
def cluster_optics_dbscan(reachability, core_distances, ordering, eps):
542+
@_deprecate_positional_args
543+
def cluster_optics_dbscan(*, reachability, core_distances, ordering, eps):
542544
"""Performs DBSCAN extraction for an arbitrary epsilon.
543545
544546
Extracting the clusters runs in linear time. Note that this results in
@@ -577,7 +579,7 @@ def cluster_optics_dbscan(reachability, core_distances, ordering, eps):
577579
return labels
578580

579581

580-
def cluster_optics_xi(reachability, predecessor, ordering, min_samples,
582+
def cluster_optics_xi(*, reachability, predecessor, ordering, min_samples,
581583
min_cluster_size=None, xi=0.05,
582584
predecessor_correction=True):
583585
"""Automatically extract clusters according to the Xi-steep method.

sklearn/cluster/_spectral.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
from ._kmeans import k_means
1919

2020

21-
def discretize(vectors, copy=True, max_svd_restarts=30, n_iter_max=20,
21+
@_deprecate_positional_args
22+
def discretize(vectors, *, copy=True, max_svd_restarts=30, n_iter_max=20,
2223
random_state=None):
2324
"""Search for a partition matrix (clustering) which is closest to the
2425
eigenvector embedding.
@@ -156,7 +157,8 @@ def discretize(vectors, copy=True, max_svd_restarts=30, n_iter_max=20,
156157
return labels
157158

158159

159-
def spectral_clustering(affinity, n_clusters=8, n_components=None,
160+
@_deprecate_positional_args
161+
def spectral_clustering(affinity, *, n_clusters=8, n_components=None,
160162
eigen_solver=None, random_state=None, n_init=10,
161163
eigen_tol=0.0, assign_labels='kmeans'):
162164
"""Apply clustering to a projection of the normalized Laplacian.

sklearn/cluster/tests/test_hierarchical.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ def test_structured_linkage_tree():
7474
connectivity = grid_to_graph(*mask.shape)
7575
for tree_builder in _TREE_BUILDERS.values():
7676
children, n_components, n_leaves, parent = \
77-
tree_builder(X.T, connectivity)
77+
tree_builder(X.T, connectivity=connectivity)
7878
n_nodes = 2 * X.shape[1] - 1
7979
assert len(children) + n_leaves == n_nodes
8080
# Check that ward_tree raises a ValueError with a connectivity matrix
8181
# of the wrong shape
8282
with pytest.raises(ValueError):
83-
tree_builder(X.T, np.ones((4, 4)))
83+
tree_builder(X.T, connectivity=np.ones((4, 4)))
8484
# Check that fitting with no samples raises an error
8585
with pytest.raises(ValueError):
86-
tree_builder(X.T[:0], connectivity)
86+
tree_builder(X.T[:0], connectivity=connectivity)
8787

8888

8989
def test_unstructured_linkage_tree():
@@ -116,7 +116,8 @@ def test_height_linkage_tree():
116116
X = rng.randn(50, 100)
117117
connectivity = grid_to_graph(*mask.shape)
118118
for linkage_func in _TREE_BUILDERS.values():
119-
children, n_nodes, n_leaves, parent = linkage_func(X.T, connectivity)
119+
children, n_nodes, n_leaves, parent = linkage_func(
120+
X.T, connectivity=connectivity)
120121
n_nodes = 2 * X.shape[1] - 1
121122
assert len(children) + n_leaves == n_nodes
122123

@@ -298,7 +299,8 @@ def test_sparse_scikit_vs_scipy():
298299
out = hierarchy.linkage(X, method=linkage)
299300

300301
children_ = out[:, :2].astype(np.int, copy=False)
301-
children, _, n_leaves, _ = _TREE_BUILDERS[linkage](X, connectivity)
302+
children, _, n_leaves, _ = _TREE_BUILDERS[linkage](
303+
X, connectivity=connectivity)
302304

303305
# Sort the order of child nodes per row for consistency
304306
children.sort(axis=1)

sklearn/cluster/tests/test_mean_shift.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_estimate_bandwidth_with_sparse_matrix():
7171
# Test estimate_bandwidth with sparse matrix
7272
X = sparse.lil_matrix((1000, 1000))
7373
msg = "A sparse matrix was passed, but dense data is required."
74-
assert_raise_message(TypeError, msg, estimate_bandwidth, X, 200)
74+
assert_raise_message(TypeError, msg, estimate_bandwidth, X)
7575

7676

7777
def test_parallel():

sklearn/cluster/tests/test_spectral.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_discretize(n_samples):
187187
y_true_noisy = (y_indicator.toarray()
188188
+ 0.1 * random_state.randn(n_samples,
189189
n_class + 1))
190-
y_pred = discretize(y_true_noisy, random_state)
190+
y_pred = discretize(y_true_noisy, random_state=random_state)
191191
assert adjusted_rand_score(y_true, y_pred) > 0.8
192192

193193

sklearn/covariance/_empirical_covariance.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def log_likelihood(emp_cov, precision):
4848
return log_likelihood_
4949

5050

51-
def empirical_covariance(X, assume_centered=False):
51+
@_deprecate_positional_args
52+
def empirical_covariance(X, *, assume_centered=False):
5253
"""Computes the Maximum likelihood covariance estimator
5354
5455

sklearn/covariance/_graph_lasso.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def alpha_max(emp_cov):
7575

7676

7777
# The g-lasso algorithm
78-
79-
def graphical_lasso(emp_cov, alpha, cov_init=None, mode='cd', tol=1e-4,
78+
@_deprecate_positional_args
79+
def graphical_lasso(emp_cov, alpha, *, cov_init=None, mode='cd', tol=1e-4,
8080
enet_tol=1e-4, max_iter=100, verbose=False,
8181
return_costs=False, eps=np.finfo(np.float64).eps,
8282
return_n_iter=False):

sklearn/covariance/_shrunk_covariance.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ def ledoit_wolf_shrinkage(X, assume_centered=False, block_size=1000):
253253
return shrinkage
254254

255255

256-
def ledoit_wolf(X, assume_centered=False, block_size=1000):
256+
@_deprecate_positional_args
257+
def ledoit_wolf(X, *, assume_centered=False, block_size=1000):
257258
"""Estimates the shrunk Ledoit-Wolf covariance matrix.
258259
259260
Read more in the :ref:`User Guide <shrunk_covariance>`.
@@ -430,8 +431,8 @@ def fit(self, X, y=None):
430431

431432

432433
# OAS estimator
433-
434-
def oas(X, assume_centered=False):
434+
@_deprecate_positional_args
435+
def oas(X, *, assume_centered=False):
435436
"""Estimate covariance with the Oracle Approximating Shrinkage algorithm.
436437
437438
Parameters

sklearn/decomposition/_dict_learning.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ def _sparse_encode(X, dictionary, gram, cov=None, algorithm='lasso_lars',
185185

186186

187187
# XXX : could be moved to the linear_model module
188-
def sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
189-
n_nonzero_coefs=None, alpha=None, copy_cov=True, init=None,
190-
max_iter=1000, n_jobs=None, check_input=True, verbose=0,
191-
positive=False):
188+
@_deprecate_positional_args
189+
def sparse_encode(X, dictionary, *, gram=None, cov=None,
190+
algorithm='lasso_lars', n_nonzero_coefs=None, alpha=None,
191+
copy_cov=True, init=None, max_iter=1000, n_jobs=None,
192+
check_input=True, verbose=0, positive=False):
192193
"""Sparse coding
193194
194195
Each row of the result is the solution to a sparse coding problem.
@@ -421,7 +422,8 @@ def _update_dict(dictionary, Y, code, verbose=False, return_r2=False,
421422
return dictionary
422423

423424

424-
def dict_learning(X, n_components, alpha, max_iter=100, tol=1e-8,
425+
@_deprecate_positional_args
426+
def dict_learning(X, n_components, *, alpha, max_iter=100, tol=1e-8,
425427
method='lars', n_jobs=None, dict_init=None, code_init=None,
426428
callback=None, verbose=False, random_state=None,
427429
return_n_iter=False, positive_dict=False,
@@ -615,7 +617,8 @@ def dict_learning(X, n_components, alpha, max_iter=100, tol=1e-8,
615617
return code, dictionary, errors
616618

617619

618-
def dict_learning_online(X, n_components=2, alpha=1, n_iter=100,
620+
@_deprecate_positional_args
621+
def dict_learning_online(X, n_components=2, *, alpha=1, n_iter=100,
619622
return_code=True, dict_init=None, callback=None,
620623
batch_size=3, verbose=False, shuffle=True,
621624
n_jobs=None, method='lars', iter_offset=0,
@@ -1230,7 +1233,7 @@ def fit(self, X, y=None):
12301233
n_components = self.n_components
12311234

12321235
V, U, E, self.n_iter_ = dict_learning(
1233-
X, n_components, self.alpha,
1236+
X, n_components, alpha=self.alpha,
12341237
tol=self.tol, max_iter=self.max_iter,
12351238
method=self.fit_algorithm,
12361239
method_max_iter=self.transform_max_iter,
@@ -1434,7 +1437,7 @@ def fit(self, X, y=None):
14341437
X = self._validate_data(X)
14351438

14361439
U, (A, B), self.n_iter_ = dict_learning_online(
1437-
X, self.n_components, self.alpha,
1440+
X, self.n_components, alpha=self.alpha,
14381441
n_iter=self.n_iter, return_code=False,
14391442
method=self.fit_algorithm,
14401443
method_max_iter=self.transform_max_iter,
@@ -1486,7 +1489,7 @@ def partial_fit(self, X, y=None, iter_offset=None):
14861489
if iter_offset is None:
14871490
iter_offset = getattr(self, 'iter_offset_', 0)
14881491
U, (A, B) = dict_learning_online(
1489-
X, self.n_components, self.alpha,
1492+
X, self.n_components, alpha=self.alpha,
14901493
n_iter=self.n_iter, method=self.fit_algorithm,
14911494
method_max_iter=self.transform_max_iter,
14921495
n_jobs=self.n_jobs, dict_init=dict_init,

sklearn/decomposition/_fastica.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ def _cube(x, fun_args):
147147
return x ** 3, (3 * x ** 2).mean(axis=-1)
148148

149149

150-
def fastica(X, n_components=None, algorithm="parallel", whiten=True,
150+
@_deprecate_positional_args
151+
def fastica(X, n_components=None, *, algorithm="parallel", whiten=True,
151152
fun="logcosh", fun_args=None, max_iter=200, tol=1e-04, w_init=None,
152153
random_state=None, return_X_mean=False, compute_sources=True,
153154
return_n_iter=False):

sklearn/decomposition/_nmf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ def _fit_multiplicative_update(X, W, H, beta_loss='frobenius',
841841
return W, H, n_iter
842842

843843

844-
def non_negative_factorization(X, W=None, H=None, n_components=None,
844+
@_deprecate_positional_args
845+
def non_negative_factorization(X, W=None, H=None, n_components=None, *,
845846
init=None, update_H=True, solver='cd',
846847
beta_loss='frobenius', tol=1e-4,
847848
max_iter=200, alpha=0., l1_ratio=0.,

sklearn/decomposition/_sparse_pca.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def fit(self, X, y=None):
187187
n_components = self.n_components
188188
code_init = self.V_init.T if self.V_init is not None else None
189189
dict_init = self.U_init.T if self.U_init is not None else None
190-
Vt, _, E, self.n_iter_ = dict_learning(X.T, n_components, self.alpha,
190+
Vt, _, E, self.n_iter_ = dict_learning(X.T, n_components,
191+
alpha=self.alpha,
191192
tol=self.tol,
192193
max_iter=self.max_iter,
193194
method=self.method,

0 commit comments

Comments
 (0)
0