-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[MRG] MNT Use isinstance instead of dtype.kind check for scalar validation. #7394
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
[MRG] MNT Use isinstance instead of dtype.kind check for scalar validation. #7394
Conversation
Test errors. Why is In at least some versions of numpy, I think, some variants int float types are not registered as subclasses of |
f0e0776
to
6d8dd0c
Compare
Usually scalars will be unwrapped from arrays, but not necessarily. On 12 September 2016 at 21:26, Raghav RV [email protected] wrote:
|
The current test failures answered my question ;) Is it worth doing the |
The point is, I suppose, is that it may be possible for this change to be On 12 September 2016 at 22:24, Joel Nothman [email protected] wrote:
|
It's a bit weird that this code doesn't validate that they're scalars, but On 12 September 2016 at 22:27, Raghav RV [email protected] wrote:
|
Indeed this was aimed at separating the leftover unaddressed issues at #5053 and closing it subsequently... |
sklearn/utils/validation.py
Outdated
""" | ||
Check if val is a scalar of type int (including all numpy int types) | ||
""" | ||
print val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops
I've added the |
if you want to add these helpers, I guess we should use them consistently.
to see where But I don't really see the point. import numbers
import numpy
isinstance(np.array([1])[0], numbers.Integral)
So using |
For example in iforest, the |
Exactly. Even I assumed |
Indeed I was just waiting for a confirmation... I'll proceed making them consistent now... |
+1 |
@GaelVaroquaux |
@raghavrv can you please explain the failure case? oh it's |
lol |
(for real, please use the helper everywhere and document in the helper why it is needed). |
Yes indeed sure in 10 mins... ;) |
Selfnote: at the end try switching to
Would avoid the cost of |
Is that really true? I can't seem to reproduce: In [1]: a = np.int32(2) In [2]: import numbers In [3]: isinstance(a, numbers.Integral) Out[3]: True In [4]: import sys In [5]: sys.version Out[5]: '2.7.12 (default, Jul 1 2016, 15:12:24) \n[GCC 5.4.0 20160609]' |
the failure was on 2.6.... |
Yes. 2.6... Sorry for not being clear... BTW When are we dropping support for 2.6? |
we are dropping 2.6 in 0.19 |
I can't reproduce on 2.6.9 either... but maybe with older numpy? |
ok actually it's just the old numpy, not python version dependent. It fails in numpy 1.6 |
Maybe we can just not merge this before the release, and drop what we
need to drop after the release. I don't really like merging in those
workarounds. They look like bad habits.
|
bump? |
is this urgent? |
No... :) Let's revisit after 0.18 maybe... |
Should we review and merge this now? This is really a trivial but subtle change that would decide the fate of numpy ints which are not compatible with |
For the record |
Thanks for the comment! Indeed which is why I've tried to do |
I have read the discussion but I am not sure why the idea of using helper functions was dropped. This is quite hard to get right across versions of numpy as this issue showed. Also is the idea of this PR to do the change over the whole scikit-learn codebase or only in the model_selection package? |
Yeah I said that only because when you read the discussion you think Python 2.6 is to blame until you realised the Python version has nothing to do with it. And then numpy 1.6 is blamed but what I meant to emphasize is that the issue happens with more recent numpy versions (numpy <= 1.8). |
True. Side effect of multi-tasking ;( BTW this PR handles it in |
Even if you change the code only in model_selection, I would be +1 for a helper function like is_int_like/is_float_like (or better names) somewhere in utils. I guess a check_int_like/check_float_like would work in your case but is a tad less convenient for more complicated cases (like |
@GaelVaroquaux @amueller @jnothman Are we all in the same page? Do you guys also want the |
IMO, such helpers are good if one needs to otherwise check multiple base
classes.
…On 7 December 2016 at 02:15, Raghav RV ***@***.***> wrote:
@GaelVaroquaux <https://github.com/GaelVaroquaux> @amueller
<https://github.com/amueller> @jnothman <https://github.com/jnothman> Are
we all in the same page? Do you guys also want the isint/isfloat
reinstated back into this PR? (I think I removed as I felt @GaelVaroquaux
<https://github.com/GaelVaroquaux> was not entirely happy with such
helpers...)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7394 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz63D-FmkE6rQm00gJTMe3jlRRdxt_ks5rFXwfgaJpZM4J6atB>
.
|
Or at least one should have int_types like one has string_types.
…On 7 December 2016 at 10:04, Joel Nothman ***@***.***> wrote:
IMO, such helpers are good if one needs to otherwise check multiple base
classes.
On 7 December 2016 at 02:15, Raghav RV ***@***.***> wrote:
> @GaelVaroquaux <https://github.com/GaelVaroquaux> @amueller
> <https://github.com/amueller> @jnothman <https://github.com/jnothman>
> Are we all in the same page? Do you guys also want the isint/isfloat
> reinstated back into this PR? (I think I removed as I felt @GaelVaroquaux
> <https://github.com/GaelVaroquaux> was not entirely happy with such
> helpers...)
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#7394 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAEz63D-FmkE6rQm00gJTMe3jlRRdxt_ks5rFXwfgaJpZM4J6atB>
> .
>
|
I won't have time to do this. I'm tagging this "Need contributor" and "Easy". The idea is a list of classes that correspond to int and string and use that to validate all int and strings.. |
Superseded by #10017 |
Addresses one of the issues listed in #5053
Uses
ininstance(..., (np.integer, numbers.Integral))
instead of thedtype.kind
based check.NOTE np 1.6's int does not seem to inherit from
numbers.Integral
, hence we need to check for bothnumbers.Integral
andnp.integer
...Minor PR @vene @jnothman @amueller
This change is