Skip to content

isinstance(x, (numbers.Integral, np.integer)) vs isinstance(x, six.integer_types) #5786

Closed

Description

@betatim

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.

Activity

  1. aakashjain commented on Nov 11, 2015

    @aakashjain

    I'd like to do this.

  2. aakashjain commented on Nov 11, 2015

    @aakashjain

    Did this, and 1 test failed.

    ======================================================================
    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...
        if not isinstance(n_features, (numbers.Integral, np.integer)):    #changed this
            raise TypeError("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.

    So maybe this isn't such a good idea?

  3. betatim commented on Nov 11, 2015

    @betatim
    MemberAuthor

    ping @agramfort, any thoughts about this?

  4. agramfort commented on Nov 11, 2015

    @agramfort
    Member
  5. aakashjain commented on Nov 11, 2015

    @aakashjain

    @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?

  6. aakashjain commented on Nov 12, 2015

    @aakashjain

    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.

  7. betatim commented on Nov 12, 2015

    @betatim
    MemberAuthor

    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.

  8. aakashjain commented on Nov 14, 2015

    @aakashjain

    Any suggestions?

  9. betatim commented on Nov 14, 2015

    @betatim
    MemberAuthor

    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.

  10. agramfort commented on Nov 14, 2015

    @agramfort
    Member

    I agree with @glouppe . I think six.integer_types or np.integer should do the trick depending on context.

  11. NeilGirdhar commented on Feb 9, 2016

    @NeilGirdhar

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions