17
17
from itertools import chain , combinations
18
18
from collections import Iterable
19
19
from math import ceil , floor
20
- import numbers
21
20
from abc import ABCMeta , abstractmethod
22
21
23
22
import numpy as np
24
23
25
24
from ..utils import indexable , check_random_state , safe_indexing
26
25
from ..utils .validation import _num_samples , column_or_1d
27
26
from ..utils .validation import check_array
27
+ from ..utils .validation import integer_types , floating_types
28
28
from ..utils .multiclass import type_of_target
29
29
from ..externals .six import with_metaclass
30
30
from ..externals .six .moves import zip
@@ -271,7 +271,7 @@ class _BaseKFold(with_metaclass(ABCMeta, BaseCrossValidator)):
271
271
272
272
@abstractmethod
273
273
def __init__ (self , n_splits , shuffle , random_state ):
274
- if not isinstance (n_splits , ( numbers . Integral , np . integer ) ):
274
+ if not isinstance (n_splits , integer_types ):
275
275
raise ValueError ('The number of folds must be of Integral type. '
276
276
'%s of type %s was passed.'
277
277
% (n_splits , type (n_splits )))
@@ -989,7 +989,7 @@ class _RepeatedSplits(with_metaclass(ABCMeta)):
989
989
and shuffle.
990
990
"""
991
991
def __init__ (self , cv , n_repeats = 10 , random_state = None , ** cvargs ):
992
- if not isinstance (n_repeats , ( np . integer , numbers . Integral ) ):
992
+ if not isinstance (n_repeats , integer_types ):
993
993
raise ValueError ("Number of repetitions must be of Integral type." )
994
994
995
995
if n_repeats <= 0 :
@@ -1643,27 +1643,27 @@ def _validate_shuffle_split_init(test_size, train_size):
1643
1643
raise ValueError ('test_size and train_size can not both be None' )
1644
1644
1645
1645
if test_size is not None :
1646
- if isinstance (test_size , ( float , np . floating ) ):
1646
+ if isinstance (test_size , floating_types ):
1647
1647
if test_size >= 1. :
1648
1648
raise ValueError (
1649
1649
'test_size=%f should be smaller '
1650
1650
'than 1.0 or be an integer' % test_size )
1651
- elif not isinstance (test_size , ( numbers . Integral , np . integer ) ):
1651
+ elif not isinstance (test_size , integer_types ):
1652
1652
# int values are checked during split based on the input
1653
1653
raise ValueError ("Invalid value for test_size: %r" % test_size )
1654
1654
1655
1655
if train_size is not None :
1656
- if isinstance (train_size , ( float , np . floating ) ):
1656
+ if isinstance (train_size , floating_types ):
1657
1657
if train_size >= 1. :
1658
1658
raise ValueError ("train_size=%f should be smaller "
1659
1659
"than 1.0 or be an integer" % train_size )
1660
- elif (isinstance (test_size , ( float , np . floating ) ) and
1660
+ elif (isinstance (test_size , floating_types ) and
1661
1661
(train_size + test_size ) > 1. ):
1662
1662
raise ValueError ('The sum of test_size and train_size = %f, '
1663
1663
'should be smaller than 1.0. Reduce '
1664
1664
'test_size and/or train_size.' %
1665
1665
(train_size + test_size ))
1666
- elif not isinstance (train_size , ( numbers . Integral , np . integer ) ):
1666
+ elif not isinstance (train_size , integer_types ):
1667
1667
# int values are checked during split based on the input
1668
1668
raise ValueError ("Invalid value for train_size: %r" % train_size )
1669
1669
@@ -1676,28 +1676,28 @@ def _validate_shuffle_split(n_samples, test_size, train_size):
1676
1676
test_size, defaults to 0.1
1677
1677
"""
1678
1678
if (test_size is not None and
1679
- isinstance (test_size , ( numbers . Integral , np . integer ) ) and
1679
+ isinstance (test_size , integer_types ) and
1680
1680
test_size >= n_samples ):
1681
1681
raise ValueError ('test_size=%d should be smaller than the number of '
1682
1682
'samples %d' % (test_size , n_samples ))
1683
1683
1684
1684
if (train_size is not None and
1685
- isinstance (train_size , ( numbers . Integral , np . integer ) ) and
1685
+ isinstance (train_size , integer_types ) and
1686
1686
train_size >= n_samples ):
1687
1687
raise ValueError ("train_size=%d should be smaller than the number of"
1688
1688
" samples %d" % (train_size , n_samples ))
1689
1689
1690
1690
if test_size == "default" :
1691
1691
test_size = 0.1
1692
1692
1693
- if isinstance (test_size , ( float , np . floating ) ):
1693
+ if isinstance (test_size , floating_types ):
1694
1694
n_test = ceil (test_size * n_samples )
1695
- elif isinstance (test_size , ( numbers . Integral , np . integer ) ):
1695
+ elif isinstance (test_size , integer_types ):
1696
1696
n_test = float (test_size )
1697
1697
1698
1698
if train_size is None :
1699
1699
n_train = n_samples - n_test
1700
- elif isinstance (train_size , ( float , np . floating ) ):
1700
+ elif isinstance (train_size , floating_types ):
1701
1701
n_train = floor (train_size * n_samples )
1702
1702
else :
1703
1703
n_train = float (train_size )
@@ -1902,7 +1902,7 @@ def check_cv(cv=3, y=None, classifier=False):
1902
1902
if cv is None :
1903
1903
cv = 3
1904
1904
1905
- if isinstance (cv , ( numbers . Integral , np . integer ) ):
1905
+ if isinstance (cv , integer_types ):
1906
1906
if (classifier and (y is not None ) and
1907
1907
(type_of_target (y ) in ('binary' , 'multiclass' ))):
1908
1908
return StratifiedKFold (cv )
0 commit comments