You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
isinstance(x, (numbers.Integral, np.integer)) vs isinstance(x, six.integer_types) #5786
During #5678 it came up that one should prefer isinstance(x, six.integer_types) to detect that a variable contains an integral number over isinstance(x, (numbers.Integral, np.integer)). There are a few occurrences of the latter spread around the code base according to a quick grep. Switching those would make a nice first contribution.
======================================================================
ERROR: sklearn.feature_extraction.tests.test_feature_hasher.test_hasher_invalid_input
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/aakash/code/scikit-learn/sklearn/feature_extraction/tests/test_feature_hasher.py", line 72, in test_hasher_invalid_input
h = FeatureHasher(n_features=np.uint16(2 ** 6))
File "/home/aakash/code/scikit-learn/sklearn/feature_extraction/hashing.py", line 81, in __init__
self._validate_params(n_features, input_type)
File "/home/aakash/code/scikit-learn/sklearn/feature_extraction/hashing.py", line 94, in _validate_params
% (n_features, type(n_features)))
TypeError: n_features must be integral, got 64 (<type 'numpy.uint16'>).
----------------------------------------------------------------------
This occurred due to changing the following function in feature_extraction/hashing.py, which already had a relevant comment:
def_validate_params(n_features, input_type):
# strangely, np.int16 instances are not instances of Integral,# while np.int64 instances are...ifnotisinstance(n_features, (numbers.Integral, np.integer)): #changed thisraiseTypeError("n_features must be integral, got %r (%s)."% (n_features, type(n_features)))
[...]
I found that, for the numpy int types, isinstance(x, six.integer_types) == True only for numpy.int, numpy.int0, numpy.int64, numpy.intp. All other numpy.int* and all numpy.uint* types give False.
@agramfort, to clarify, you mean to leave the occurrence in hashing.py and change all other occurrences to six.integer_types?
If so, should there be some sort of guideline for the future to specify when (numbers.Integral, np.integer) should be used instead?
I don't think this should be done at all unless we don't need to support numpy ints.
Please have a look at these ipython notebooks: Python2 Python3
This shows that six.integer_types doesn't work for numpy ints.
Also, instead of (numbers.Integral, np.integer), simply numbers.Integral is sufficient because numpy registers its number types with the appropriate numbers classes.
I agree that some recommendation/guidance would be good. When I created the issue I had assumed that six.integer_types was the recommendation, maybe that isn't true after all. In which case it would be good to understand when to use it and when not.
From a brief discussion with @glouppe I think the answer is "it depends". If the thing you are checking the type of is a hyper-parameter six.integer_types is probably the right thing, however if it is related to numpy arrays using np.integer and/or numbers.Integral is better. For the moment I'd just be patient until we get some feedback.
I'm looking into this for other code, and my impression is to stick with numbers.Integral. You shouldn't need np.integer for the reason that @aakashjain mentions. six is great, but of course they could not pull in numpy as a dependency and so they don't do the right thing if you are supposed to handle numpy integers.
Activity
aakashjain commentedon Nov 11, 2015
I'd like to do this.
aakashjain commentedon Nov 11, 2015
Did this, and 1 test failed.
This occurred due to changing the following function in feature_extraction/hashing.py, which already had a relevant comment:
I found that, for the numpy int types,
isinstance(x, six.integer_types) == Trueonly fornumpy.int, numpy.int0, numpy.int64, numpy.intp. All othernumpy.int*and allnumpy.uint*types giveFalse.So maybe this isn't such a good idea?
betatim commentedon Nov 11, 2015
ping @agramfort, any thoughts about this?
agramfort commentedon Nov 11, 2015
aakashjain commentedon Nov 11, 2015
@agramfort, to clarify, you mean to leave the occurrence in hashing.py and change all other occurrences to
six.integer_types?If so, should there be some sort of guideline for the future to specify when
(numbers.Integral, np.integer)should be used instead?aakashjain commentedon Nov 12, 2015
I don't think this should be done at all unless we don't need to support numpy ints.
Please have a look at these ipython notebooks:
Python2
Python3
This shows that
six.integer_typesdoesn't work for numpy ints.Also, instead of
(numbers.Integral, np.integer), simplynumbers.Integralis sufficient because numpy registers its number types with the appropriatenumbersclasses.betatim commentedon Nov 12, 2015
I agree that some recommendation/guidance would be good. When I created the issue I had assumed that
six.integer_typeswas the recommendation, maybe that isn't true after all. In which case it would be good to understand when to use it and when not.aakashjain commentedon Nov 14, 2015
Any suggestions?
betatim commentedon Nov 14, 2015
From a brief discussion with @glouppe I think the answer is "it depends". If the thing you are checking the type of is a hyper-parameter
six.integer_typesis probably the right thing, however if it is related to numpy arrays usingnp.integerand/ornumbers.Integralis better. For the moment I'd just be patient until we get some feedback.agramfort commentedon Nov 14, 2015
I agree with @glouppe . I think six.integer_types or np.integer should do the trick depending on context.
NeilGirdhar commentedon Feb 9, 2016
I'm looking into this for other code, and my impression is to stick with
numbers.Integral. You shouldn't neednp.integerfor the reason that @aakashjain mentions.sixis great, but of course they could not pull innumpyas a dependency and so they don't do the right thing if you are supposed to handle numpy integers.