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
Vague Error Message for Linear Regression when X is 1D #4466
I was recently trying to do just a very simple linear regression on x vs. y -- I got this error message:
IndexError: tuple index out of range
I wasted 3 minutes trying to figure out if my x and y vectors didn't have the same lengths (they did), whether the problem was that they were lists, not numpy arrays (they were). Then I found the culprit, through stackoverflow:
I know this is only a small inconvenience for me, but I think this needs to be addressed for the sake of usability. This provides an unnecessarily difficult entry barrier to new users.
A related thought/comment: Do the inputs have to be numpy arrays? Why not try to salvage the input if the user passes lists?
We to try our best to salvage what the user gave us. I'd agree we failed here. We want consistent treatment of X of 1d shape, and we don't have that yet.
From looking at the code for check_array(), it looks like the intention is, by default, to cast the input. If check_X_y() shouldn't do this, then it should probably call check_array() with the ensure_2d flag as False or perform additional validation after np.atleast_2d() returns, raising a useful error if shape[1] is still Null. I'm not sure why NumPy considers a matrix with a null dimension is considered any more 2D than a 1D list or ndarray, mathematically speaking.
Actually check_X_y does cast to 2d, as this was the default behavior before. It does so in the same odd way that atleast2d did before, which is X[np.newaxis, :]. @GaelVaroquaux we have been accepting it inconsistently for a long time, and we can't really go back to not accepting it anywhere. We should accept it as number of samples everywhere, which would be the transpose of what is currently happening.
I didn't change that when introducing check_X_y because it was backward-incompatible and I had enough common tests to add / fix.
Activity
amueller commentedon Mar 30, 2015
We to try our best to salvage what the user gave us. I'd agree we failed here. We want consistent treatment of X of 1d shape, and we don't have that yet.
ahwillia commentedon Mar 30, 2015
Someone pointed out to me that
fit()should be callingnumpy.atleast_2d(), specifically on this line:scikit-learn/sklearn/utils/validation.py
Line 334 in 69827c4
So this is either a numpy problem (seems unlikely to me) or the
check_X_yfunction is handling the inputs in an unexpected way.GaelVaroquaux commentedon Mar 30, 2015
ahwillia commentedon Mar 30, 2015
Why not cast to 2D and print a warning? The
x[:,np.newaxis]fix seems cumbersome.Also, it seems like the intention of check_X_y was to cast the shape to 2D... Has the thinking changed?
GaelVaroquaux commentedon Mar 30, 2015
GrantRVD commentedon Mar 30, 2015
From looking at the code for check_array(), it looks like the intention is, by default, to cast the input. If
check_X_y()shouldn't do this, then it should probably callcheck_array()with the ensure_2d flag asFalseor perform additional validation afternp.atleast_2d()returns, raising a useful error ifshape[1]is still Null. I'm not sure why NumPy considers a matrix with a null dimension is considered any more 2D than a 1D list or ndarray, mathematically speaking.amueller commentedon Mar 30, 2015
Actually
check_X_ydoes cast to 2d, as this was the default behavior before. It does so in the same odd way thatatleast2ddid before, which isX[np.newaxis, :].@GaelVaroquaux we have been accepting it inconsistently for a long time, and we can't really go back to not accepting it anywhere. We should accept it as number of samples everywhere, which would be the transpose of what is currently happening.
I didn't change that when introducing
check_X_ybecause it was backward-incompatible and I had enough common tests to add / fix.amueller commentedon Sep 9, 2015
Fixed by #5152.