Skip to content

COSMIT use np.iinfo to define the max int32 #15960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sklearn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def setup_module(module):
# Check if a random seed exists in the environment, if not create one.
_random_seed = os.environ.get('SKLEARN_SEED', None)
if _random_seed is None:
_random_seed = np.random.uniform() * (2 ** 31 - 1)
_random_seed = np.random.uniform() * np.iinfo(np.int32).max
_random_seed = int(_random_seed)
print("I: Seeding RNGs with %r" % _random_seed)
np.random.seed(_random_seed)
Expand Down
4 changes: 1 addition & 3 deletions sklearn/ensemble/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from ..utils import check_random_state
from ..utils.metaestimators import _BaseComposition

MAX_RAND_SEED = np.iinfo(np.int32).max


def _parallel_fit_estimator(estimator, X, y, sample_weight=None):
"""Private function used to fit an estimator within a job."""
Expand Down Expand Up @@ -71,7 +69,7 @@ def _set_random_states(estimator, random_state=None):
to_set = {}
for key in sorted(estimator.get_params(deep=True)):
if key == 'random_state' or key.endswith('__random_state'):
to_set[key] = random_state.randint(MAX_RAND_SEED)
to_set[key] = random_state.randint(np.iinfo(np.int32).max)

if to_set:
estimator.set_params(**to_set)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_extraction/_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _validate_params(n_features, input_type):
if not isinstance(n_features, numbers.Integral):
raise TypeError("n_features must be integral, got %r (%s)."
% (n_features, type(n_features)))
elif n_features < 1 or n_features >= 2 ** 31:
elif n_features < 1 or n_features >= np.iinfo(np.int32).max + 1:
raise ValueError("Invalid number of features (%d)." % n_features)

if input_type not in ("dict", "pair", "string"):
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_extraction/_hashing_fast.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def transform(raw_X, Py_ssize_t n_features, dtype,
indices_a = np.frombuffer(indices, dtype=np.int32)
indptr_a = np.frombuffer(indptr, dtype=indices_np_dtype)

if indptr[-1] > 2147483648: # = 2**31
if indptr[-1] > np.iinfo(np.int32).max: # = 2**31 - 1
if sp_version < (0, 14):
raise ValueError(('sparse CSR array has {} non-zero '
'elements and requires 64 bit indexing, '
Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_extraction/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ def _count_vocab(self, raw_documents, fixed_vocab):
raise ValueError("empty vocabulary; perhaps the documents only"
" contain stop words")

if indptr[-1] > 2147483648: # = 2**31 - 1
if indptr[-1] > np.iinfo(np.int32).max: # = 2**31 - 1
if _IS_32BIT:
raise ValueError(('sparse CSR array has {} non-zero '
'elements and requires 64 bit indexing, '
Expand Down
2 changes: 1 addition & 1 deletion sklearn/tree/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def fit(self, X, y, sample_weight=None, check_input=True,
y = np.ascontiguousarray(y, dtype=DOUBLE)

# Check parameters
max_depth = ((2 ** 31) - 1 if self.max_depth is None
max_depth = (np.iinfo(np.int32).max if self.max_depth is None
else self.max_depth)
max_leaf_nodes = (-1 if self.max_leaf_nodes is None
else self.max_leaf_nodes)
Expand Down