Skip to content

[MRG + 1] Warn on 1D arrays, addresses #4511 - #5152

Closed
vighneshbirodkar wants to merge 3 commits into
scikit-learn:masterfrom
vighneshbirodkar:array_1d_fix
Closed

vighneshbirodkar wants to merge 3 commits into
scikit-learn:masterfrom
vighneshbirodkar:array_1d_fix

Conversation

@vighneshbirodkar

Copy link
Copy Markdown
Contributor

Just to reiterate the discussion I had with @amueller in person

I have put a check in check_array and issued a warning. I will proceed to fix the tests now.

Comment thread sklearn/utils/validation.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this is appropriate iff ensure_2d is set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is redundant if ensure_2d is set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. So what's wrong with 1d arrays if ensure_2d=False?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little confused myself now. Here is what I understand

  • Warn against 1D arrays now and raise an error in later versions.
  • If ensure_2d is set, nothing to be done now. In later versions ensure_2d parameter is deprecated ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the current ensure_2d does the wrong thing. I think we should change the behavior of ensure_2d from reshaping to warning / erroring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So should I go ahead with ensure_2d then ? It will warn now and throw an error in later versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. this is only used internally and we can rename it later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the np.atleast_2d call I get 227 errors in the tests. It's my understanding that this PR should fix all of them ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are probably many errors caused by the same underlying issues. But yes, indeed.

You should be able to just set the SingleDimensionWarning to "error" though (maybe at the top of the file if it is just for testing).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of these will have fixes in #4511.

@amueller

Copy link
Copy Markdown
Member

Mentioning #4511 for visibility (mentioning in the title will not show up there).

@vighneshbirodkar

Copy link
Copy Markdown
Contributor Author

@amueller Down to ~130 errors. I hope these little fixes are correct.

Comment thread sklearn/cluster/birch.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems odd. why does this need to be 2d? Y_norm_squared should be 1d.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many things were internally converted to 2D using the np.atleast2d call, without it, many tests are causing errors.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then this should probably be fixed in euclidean distances. the Y_norm_squared should be ok to be 1d.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I have made a note of it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can euclidian_distances check for 1d and do a conversion to 2d ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can do that.

@vighneshbirodkar

Copy link
Copy Markdown
Contributor Author

@amueller The tests are passing now. There are 9 places where the new warning thrown, I will fix that soon.

However, you also mentioned creating a common test for every estimator and ensure that a 1D input throws the new warning. But as far as I know, due to the 1D array, something most likely will fail later on.

Comment thread sklearn/utils/__init__.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly checking for sparse formats and finiteness. Since the _ensure_sparse_format is an internal function, I have kept the check_array call as it is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine.

@vighneshbirodkar

Copy link
Copy Markdown
Contributor Author

@amueller
For the common tests with 1d input, I am leaning towards making an assert_warns call and then catching any exceptions that might occur. Thoughts ?

Comment thread sklearn/utils/validation.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo on the warning message

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a better message, like "Passing 1d arrays as data is deprecated and will be removed in 0.18. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample."

Comment thread sklearn/neighbors/approximate.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't X[[i]] be easier?

@amueller amueller added this to the 0.17 milestone Aug 31, 2015
@amueller

Copy link
Copy Markdown
Member

ping @jnothman and @GaelVaroquaux who might be interested.
One question: do we also want to deprecate 1d input in the scalers? It was explicitly allowed there up till now.

@amueller

Copy link
Copy Markdown
Member

Also maybe ping @ogrisel

@giorgiop

giorgiop commented Sep 2, 2015

Copy link
Copy Markdown
Contributor

@amueller I was working on this on #5104 for Scalers. So I think the answer is yes.
I have used a 2d-input validator so far (third point in the to do list), but I will remove it to be consistent with the DeprecationWarning

Comment thread sklearn/utils/extmath.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR but this should be changed to dtype=np.float64 to be explicitly not platform dependent.

Comment thread sklearn/utils/tests/test_validation.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is good. leave it.

Passing 1D arrays to check_array, without setting `ensure_2d` to false now
raises a deprecation warning before reshaping it. This will later throw an
error.

All Scaler classes also throw warnings when 1D arrays are passed.

All unit tests/doctests are modified to ensure that no 1D arrays are passed,
except in explicit 1D array tests where the warnings have been silenced.

Additional tests are also included which check for different 1D array cases.

2D array tests with one samples and one features are also added and where
they failed, `check_array` call has been modified to give a more useful error
message
@vighneshbirodkar vighneshbirodkar changed the title Warn on 1D arrays, addresses #4511 [WIP] Warn on 1D arrays, addresses #4511 [MRG] Sep 4, 2015
Comment thread doc/tutorial/basic/tutorial.rst Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be consistent and also use [-1:] here?

@amueller amueller changed the title Warn on 1D arrays, addresses #4511 [MRG] [MRG + 1] Warn on 1D arrays, addresses #4511 [MRG] Sep 4, 2015
@amueller

amueller commented Sep 4, 2015

Copy link
Copy Markdown
Member

LGTM apart from minor nitpicks

@amueller amueller changed the title [MRG + 1] Warn on 1D arrays, addresses #4511 [MRG] [MRG + 1] Warn on 1D arrays, addresses #4511 Sep 4, 2015
@amueller

amueller commented Sep 8, 2015

Copy link
Copy Markdown
Member

@ogrisel wdyt?

@ogrisel

ogrisel commented Sep 9, 2015

Copy link
Copy Markdown
Member

I will squash the commit, merge by rebase and document the change in whats_new.rst.

@ogrisel

ogrisel commented Sep 9, 2015

Copy link
Copy Markdown
Member

Merged as 2f09933. Thanks @vighneshbirodkar!

@ogrisel ogrisel closed this Sep 9, 2015
@amueller

amueller commented Sep 9, 2015

Copy link
Copy Markdown
Member

Thanks @vighneshbirodkar, this is really helpful. Thanks for the review @ogrisel.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants