Skip to content

Commit 2fc3a85

Browse files
rththomasjpfan
authored andcommitted
MAINT Use isinstance(x, numbers.Integral) to check for integer dtype (scikit-learn#14004)
1 parent 05b12cf commit 2fc3a85

File tree

15 files changed

+29
-31
lines changed

15 files changed

+29
-31
lines changed

benchmarks/bench_plot_nmf.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from sklearn.decomposition.nmf import NMF
2222
from sklearn.decomposition.nmf import _initialize_nmf
2323
from sklearn.decomposition.nmf import _beta_divergence
24-
from sklearn.decomposition.nmf import INTEGER_TYPES, _check_init
24+
from sklearn.decomposition.nmf import _check_init
2525
from sklearn.exceptions import ConvergenceWarning
2626
from sklearn.utils.extmath import safe_sparse_dot, squared_norm
2727
from sklearn.utils import check_array
@@ -236,11 +236,12 @@ def _fit_transform(self, X, y=None, W=None, H=None, update_H=True):
236236
if n_components is None:
237237
n_components = n_features
238238

239-
if (not isinstance(n_components, INTEGER_TYPES) or
239+
if (not isinstance(n_components, numbers.Integral) or
240240
n_components <= 0):
241241
raise ValueError("Number of components must be a positive integer;"
242242
" got (n_components=%r)" % n_components)
243-
if not isinstance(self.max_iter, INTEGER_TYPES) or self.max_iter < 0:
243+
if (not isinstance(self.max_iter, numbers.Integral) or
244+
self.max_iter < 0):
244245
raise ValueError("Maximum number of iterations must be a positive "
245246
"integer; got (max_iter=%r)" % self.max_iter)
246247
if not isinstance(self.tol, numbers.Number) or self.tol < 0:

sklearn/decomposition/nmf.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
EPSILON = np.finfo(np.float32).eps
2626

27-
INTEGER_TYPES = (numbers.Integral, np.integer)
28-
2927

3028
def norm(x):
3129
"""Dot product-based Euclidean norm implementation
@@ -1011,10 +1009,10 @@ def non_negative_factorization(X, W=None, H=None, n_components=None,
10111009
if n_components is None:
10121010
n_components = n_features
10131011

1014-
if not isinstance(n_components, INTEGER_TYPES) or n_components <= 0:
1012+
if not isinstance(n_components, numbers.Integral) or n_components <= 0:
10151013
raise ValueError("Number of components must be a positive integer;"
10161014
" got (n_components=%r)" % n_components)
1017-
if not isinstance(max_iter, INTEGER_TYPES) or max_iter < 0:
1015+
if not isinstance(max_iter, numbers.Integral) or max_iter < 0:
10181016
raise ValueError("Maximum number of iterations must be a positive "
10191017
"integer; got (max_iter=%r)" % max_iter)
10201018
if not isinstance(tol, numbers.Number) or tol < 0:

sklearn/decomposition/pca.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def _fit_full(self, X, n_components):
422422
"svd_solver='full'"
423423
% (n_components, min(n_samples, n_features)))
424424
elif n_components >= 1:
425-
if not isinstance(n_components, (numbers.Integral, np.integer)):
425+
if not isinstance(n_components, numbers.Integral):
426426
raise ValueError("n_components=%r must be of type int "
427427
"when greater than or equal to 1, "
428428
"was of type=%r"
@@ -487,7 +487,7 @@ def _fit_truncated(self, X, n_components, svd_solver):
487487
"svd_solver='%s'"
488488
% (n_components, min(n_samples, n_features),
489489
svd_solver))
490-
elif not isinstance(n_components, (numbers.Integral, np.integer)):
490+
elif not isinstance(n_components, numbers.Integral):
491491
raise ValueError("n_components=%r must be of type int "
492492
"when greater than or equal to 1, was of type=%r"
493493
% (n_components, type(n_components)))

sklearn/ensemble/bagging.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _fit(self, X, y, max_samples=None, max_depth=None, sample_weight=None):
298298
# Validate max_samples
299299
if max_samples is None:
300300
max_samples = self.max_samples
301-
elif not isinstance(max_samples, (numbers.Integral, np.integer)):
301+
elif not isinstance(max_samples, numbers.Integral):
302302
max_samples = int(max_samples * X.shape[0])
303303

304304
if not (0 < max_samples <= X.shape[0]):
@@ -308,7 +308,7 @@ def _fit(self, X, y, max_samples=None, max_depth=None, sample_weight=None):
308308
self._max_samples = max_samples
309309

310310
# Validate max_features
311-
if isinstance(self.max_features, (numbers.Integral, np.integer)):
311+
if isinstance(self.max_features, numbers.Integral):
312312
max_features = self.max_features
313313
elif isinstance(self.max_features, np.float):
314314
max_features = self.max_features * self.n_features_

sklearn/ensemble/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(self, base_estimator, n_estimators=10,
101101
def _validate_estimator(self, default=None):
102102
"""Check the estimator and the n_estimator attribute, set the
103103
`base_estimator_` attribute."""
104-
if not isinstance(self.n_estimators, (numbers.Integral, np.integer)):
104+
if not isinstance(self.n_estimators, numbers.Integral):
105105
raise ValueError("n_estimators must be an integer, "
106106
"got {0}.".format(type(self.n_estimators)))
107107

sklearn/ensemble/gradient_boosting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ def _check_params(self):
13141314
"or 'log2'." % self.max_features)
13151315
elif self.max_features is None:
13161316
max_features = self.n_features_
1317-
elif isinstance(self.max_features, (numbers.Integral, np.integer)):
1317+
elif isinstance(self.max_features, numbers.Integral):
13181318
max_features = self.max_features
13191319
else: # float
13201320
if 0. < self.max_features <= 1.:
@@ -1326,7 +1326,7 @@ def _check_params(self):
13261326
self.max_features_ = max_features
13271327

13281328
if not isinstance(self.n_iter_no_change,
1329-
(numbers.Integral, np.integer, type(None))):
1329+
(numbers.Integral, type(None))):
13301330
raise ValueError("n_iter_no_change should either be None or an "
13311331
"integer. %r was passed"
13321332
% self.n_iter_no_change)

sklearn/ensemble/iforest.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
__all__ = ["IsolationForest"]
2525

26-
INTEGER_TYPES = (numbers.Integral, np.integer)
27-
2826

2927
class IsolationForest(BaseBagging, OutlierMixin):
3028
"""Isolation Forest Algorithm
@@ -256,7 +254,7 @@ def fit(self, X, y=None, sample_weight=None):
256254
'Valid choices are: "auto", int or'
257255
'float' % self.max_samples)
258256

259-
elif isinstance(self.max_samples, INTEGER_TYPES):
257+
elif isinstance(self.max_samples, numbers.Integral):
260258
if self.max_samples > n_samples:
261259
warn("max_samples (%s) is greater than the "
262260
"total number of samples (%s). max_samples "

sklearn/feature_extraction/hashing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(self, n_features=(2 ** 20), input_type="dict",
9696
def _validate_params(n_features, input_type):
9797
# strangely, np.int16 instances are not instances of Integral,
9898
# while np.int64 instances are...
99-
if not isinstance(n_features, (numbers.Integral, np.integer)):
99+
if not isinstance(n_features, numbers.Integral):
100100
raise TypeError("n_features must be integral, got %r (%s)."
101101
% (n_features, type(n_features)))
102102
elif n_features < 1 or n_features >= 2 ** 31:

sklearn/model_selection/_search.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from collections.abc import Mapping, Sequence, Iterable
1616
from functools import partial, reduce
1717
from itertools import product
18+
import numbers
1819
import operator
1920
import time
2021
import warnings
@@ -693,7 +694,7 @@ def evaluate_candidates(candidate_params):
693694
# parameter set.
694695
if callable(self.refit):
695696
self.best_index_ = self.refit(results)
696-
if not isinstance(self.best_index_, (int, np.integer)):
697+
if not isinstance(self.best_index_, numbers.Integral):
697698
raise TypeError('best_index_ returned is not an integer')
698699
if (self.best_index_ < 0 or
699700
self.best_index_ >= len(results["params"])):

sklearn/model_selection/_split.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ class _RepeatedSplits(metaclass=ABCMeta):
10751075
and shuffle.
10761076
"""
10771077
def __init__(self, cv, n_repeats=10, random_state=None, **cvargs):
1078-
if not isinstance(n_repeats, (np.integer, numbers.Integral)):
1078+
if not isinstance(n_repeats, numbers.Integral):
10791079
raise ValueError("Number of repetitions must be of Integral type.")
10801080

10811081
if n_repeats <= 0:

sklearn/neighbors/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import warnings
1313
from abc import ABCMeta, abstractmethod
14+
import numbers
1415

1516
import numpy as np
1617
from scipy.sparse import csr_matrix, issparse
@@ -268,7 +269,7 @@ def _fit(self, X):
268269
self.n_neighbors
269270
)
270271
else:
271-
if not np.issubdtype(type(self.n_neighbors), np.integer):
272+
if not isinstance(self.n_neighbors, numbers.Integral):
272273
raise TypeError(
273274
"n_neighbors does not take %s value, "
274275
"enter integer value" %
@@ -391,7 +392,7 @@ class from an array representing our data set and ask who's
391392
n_neighbors
392393
)
393394
else:
394-
if not np.issubdtype(type(n_neighbors), np.integer):
395+
if not isinstance(n_neighbors, numbers.Integral):
395396
raise TypeError(
396397
"n_neighbors does not take %s value, "
397398
"enter integer value" %

sklearn/preprocessing/_discretization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def _validate_n_bins(self, n_features):
211211
"""
212212
orig_bins = self.n_bins
213213
if isinstance(orig_bins, numbers.Number):
214-
if not isinstance(orig_bins, (numbers.Integral, np.integer)):
214+
if not isinstance(orig_bins, numbers.Integral):
215215
raise ValueError("{} received an invalid n_bins type. "
216216
"Received {}, expected int."
217217
.format(KBinsDiscretizer.__name__,

sklearn/preprocessing/data.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
from itertools import chain, combinations
12+
import numbers
1213
import warnings
1314
from itertools import combinations_with_replacement as combinations_w_r
1415

@@ -670,7 +671,7 @@ def partial_fit(self, X, y=None):
670671
# transform it to a NumPy array of shape (n_features,) required by
671672
# incr_mean_variance_axis and _incremental_variance_axis
672673
if (hasattr(self, 'n_samples_seen_') and
673-
isinstance(self.n_samples_seen_, (int, np.integer))):
674+
isinstance(self.n_samples_seen_, numbers.Integral)):
674675
self.n_samples_seen_ = np.repeat(
675676
self.n_samples_seen_, X.shape[1]).astype(np.int64, copy=False)
676677

sklearn/tree/tree.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#
1515
# License: BSD 3 clause
1616

17-
18-
1917
import numbers
2018
import warnings
2119
from abc import ABCMeta
@@ -187,7 +185,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
187185
max_leaf_nodes = (-1 if self.max_leaf_nodes is None
188186
else self.max_leaf_nodes)
189187

190-
if isinstance(self.min_samples_leaf, (numbers.Integral, np.integer)):
188+
if isinstance(self.min_samples_leaf, numbers.Integral):
191189
if not 1 <= self.min_samples_leaf:
192190
raise ValueError("min_samples_leaf must be at least 1 "
193191
"or in (0, 0.5], got %s"
@@ -200,7 +198,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
200198
% self.min_samples_leaf)
201199
min_samples_leaf = int(ceil(self.min_samples_leaf * n_samples))
202200

203-
if isinstance(self.min_samples_split, (numbers.Integral, np.integer)):
201+
if isinstance(self.min_samples_split, numbers.Integral):
204202
if not 2 <= self.min_samples_split:
205203
raise ValueError("min_samples_split must be an integer "
206204
"greater than 1 or a float in (0.0, 1.0]; "
@@ -234,7 +232,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
234232
'values are "auto", "sqrt" or "log2".')
235233
elif self.max_features is None:
236234
max_features = self.n_features_
237-
elif isinstance(self.max_features, (numbers.Integral, np.integer)):
235+
elif isinstance(self.max_features, numbers.Integral):
238236
max_features = self.max_features
239237
else: # float
240238
if self.max_features > 0.0:
@@ -254,7 +252,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
254252
raise ValueError("max_depth must be greater than zero. ")
255253
if not (0 < max_features <= self.n_features_):
256254
raise ValueError("max_features must be in (0, n_features]")
257-
if not isinstance(max_leaf_nodes, (numbers.Integral, np.integer)):
255+
if not isinstance(max_leaf_nodes, numbers.Integral):
258256
raise ValueError("max_leaf_nodes must be integral number but was "
259257
"%r" % max_leaf_nodes)
260258
if -1 < max_leaf_nodes < 2:

sklearn/utils/validation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def check_random_state(seed):
773773
"""
774774
if seed is None or seed is np.random:
775775
return np.random.mtrand._rand
776-
if isinstance(seed, (numbers.Integral, np.integer)):
776+
if isinstance(seed, numbers.Integral):
777777
return np.random.RandomState(seed)
778778
if isinstance(seed, np.random.RandomState):
779779
return seed

0 commit comments

Comments
 (0)