8000 Apply ruff rule RUF007 · scikit-learn/scikit-learn@e992b1e · GitHub
[go: up one dir, main page]

Skip to content

Commit e992b1e

Browse files
Apply ruff rule RUF007
RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs
1 parent d30de5c commit e992b1e

File tree

8 files changed

+21
-15
lines changed

8 files changed

+21
-15
lines changed

sklearn/decomposition/tests/test_incremental_pca.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for Incremental PCA."""
22

3+
import itertools
34
import warnings
45

56
import numpy as np
@@ -228,7 +229,7 @@ def test_incremental_pca_batch_signs():
228229
ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X)
229230
all_components.append(ipca.components_)
230231

231-
for i, j in zip(all_components[:-1], all_components[1:]):
232+
for i, j in itertools.pairwise(all_components):
232233
assert_almost_equal(np.sign(i), np.sign(j), decimal=6)
233234

234235

@@ -265,7 +266,7 @@ def test_incremental_pca_batch_values():
265266
ipca = IncrementalPCA(n_components=None, batch_size=batch_size).fit(X)
266267
all_components.append(ipca.components_)
267268

268-
for i, j in zip(all_components[:-1], all_components[1:]):
269+
for i, j in itertools.pairwise(all_components):
269270
assert_almost_equal(i, j, decimal=1)
270271

271272

@@ -281,7 +282,7 @@ def test_incremental_pca_batch_rank():
281282
ipca = IncrementalPCA(n_components=20, batch_size=batch_size).fit(X)
282283
all_components.append(ipca.components_)
283284

284-
for components_i, components_j in zip(all_components[:-1], all_components[1:]):
285+
for components_i, components_j in itertools.pairwise(all_components):
285286
assert_allclose_dense_sparse(components_i, components_j)
286287

287288

@@ -300,7 +301,7 @@ def test_incremental_pca_partial_fit():
300301
pipca = IncrementalPCA(n_components=2, batch_size=batch_size)
301302
# Add one to make sure endpoint is included
302303
batch_itr = np.arange(0, n + 1, batch_size)
303-
for i, j in zip(batch_itr[:-1], batch_itr[1:]):
304+
for i, j in itertools.pairwise(batch_itr):
304305
pipca.partial_fit(X[i:j, :])
305306
assert_almost_equal(ipca.components_, pipca.components_, decimal=3)
306307

sklearn/ensemble/tests/test_forest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def test_parallel_train():
926926

927927
X_test = rng.randn(n_samples, n_features)
928928
probas = [clf.predict_proba(X_test) for clf in clfs]
929-
for proba1, proba2 in zip(probas, probas[1:]):
929+
for proba1, proba2 in itertools.pairwise(probas):
930930
assert_array_almost_equal(proba1, proba2)
931931

932932

sklearn/manifold/tests/test_spectral_embedding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
from unittest.mock import Mock
23

34
import numpy as np
@@ -71,7 +72,7 @@ def test_sparse_graph_connected_component(coo_container):
7172
p = rng.permutation(n_samples)
7273
connections = []
7374

74-
for start, stop in zip(boundaries[:-1], boundaries[1:]):
75+
for start, stop in itertools.pairwise(boundaries):
7576
group = p[start:stop]
7677
# Connect all elements within the group at least once via an
7778
# arbitrary path that spans the group.
@@ -91,7 +92,7 @@ def test_sparse_graph_connected_component(coo_container):
9192
affinity = coo_container((data, (row_idx, column_idx)))
9293
affinity = 0.5 * (affinity + affinity.T)
9394

94-
for start, stop in zip(boundaries[:-1], boundaries[1:]):
95+
for start, stop in itertools.pairwise(boundaries):
9596
component_1 = _graph_connected_component(affinity, p[start])
9697
component_size = stop - start
9798
assert component_1.sum() == component_size

sklearn/neural_network/_multilayer_perceptron.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import warnings
77
from abc import ABC, abstractmethod
8-
from itertools import chain
8+
from itertools import chain, pairwise
99
from numbers import Integral, Real
1010

1111
import numpy as np
@@ -491,7 +491,7 @@ def _fit(self, X, y, sample_weight=None, incremental=False):
491491

492492
coef_grads = [
493493
np.empty((n_fan_in_, n_fan_out_), dtype=X.dtype)
494-
for n_fan_in_, n_fan_out_ in zip(layer_units[:-1], layer_units[1:])
494+
for n_fan_in_, n_fan_out_ in pairwise(layer_units)
495495
]
496496

497497
intercept_grads = [

sklearn/tree/tests/test_tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pickle
99
import re
1010
import struct
11-
from itertools import chain, product
11+
from itertools import chain, pairwise, product
1212

1313
import joblib
1414
import numpy as np
@@ -1865,7 +1865,7 @@ def assert_pruning_creates_subtree(estimator_cls, X, y, pruning_path):
18651865

18661866
# A pruned tree must be a subtree of the previous tree (which had a
18671867
# smaller ccp_alpha)
1868-
for prev_est, next_est in zip(estimators, estimators[1:]):
1868+
for prev_est, next_est in pairwise(estimators):
18691869
assert_is_subtree(prev_est.tree_, next_est.tree_)
18701870

18711871

sklearn/utils/_tags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import warnings
44
from collections import OrderedDict
55
from dataclasses import dataclass, field
6-
from itertools import chain
6+
from itertools import chain, pairwise
77

88
from .fixes import _dataclass_args
99

@@ -437,7 +437,7 @@ def get_tags(estimator) -> Tags:
437437
# inheritance
438438
sklearn_tags_diff = {}
439439
items = list(sklearn_tags_provider.items())
440-
for current_item, next_item in zip(items[:-1], items[1:]):
440+
for current_item, next_item in pairwise(items):
441441
current_name, current_tags = current_item
442442
next_name, next_tags = next_item
443443
current_tags = _to_old_tags(current_tags)

sklearn/utils/sparsefuncs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Authors: The scikit-learn developers
44
# SPDX-License-Identifier: BSD-3-Clause
55

6+
import itertools
7+
68
import numpy as np
79
import scipy.sparse as sp
810
from scipy.sparse.linalg import LinearOperator
@@ -704,7 +706,7 @@ def csc_median_axis_0(X):
704706
n_samples, n_features = X.shape
705707
median = np.zeros(n_features)
706708

707-
for f_ind, (start, end) in enumerate(zip(indptr[:-1], indptr[1:])):
709+
for f_ind, (start, end) in enumerate(itertools.pairwise(indptr)):
708710
# Prevent modifying X in place
709711
data = np.copy(X.data[start:end])
710712
nz = n_samples - data.size

sklearn/utils/tests/test_extmath.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Authors: The scikit-learn developers
22
# SPDX-License-Identifier: BSD-3-Clause
33

4+
import itertools
5+
46
import numpy as np
57
import pytest
68
from scipy import linalg, sparse
@@ -905,7 +907,7 @@ def test_incremental_variance_ddof():
905907
if steps[-1] != X.shape[0]:
906908
steps = np.hstack([steps, n_samples])
907909

908-
for i, j in zip(steps[:-1], steps[1:]):
910+
for i, j in itertools.pairwise(steps):
909911
batch = X[i:j, :]
910912
if i == 0:
911913
incremental_means = batch.mean(axis=0)

0 commit comments

Comments
 (0)
0