naisuu42
naisuu42

Reputation: 278

Why does the fit method in sklearn's LinearRegression only accept 2D array for the x-values, but 1D arrays for the y-values?

Basically just the title. It just struck me as odd when I was dipped my toes into the sklearn library. Is there an explanation for this?

Upvotes: 1

Views: 4053

Answers (1)

yatu
yatu

Reputation: 88305

This is just the way it is by design choice for the fit methods of ML models in scikit-learn afaik. It's mostly to stay consistent with the specification of the input shape: (n_samples, n_features):

X : {array-like, sparse matrix} of shape (n_samples, n_features)
     Training vector, where n_samples is the number of samples and
     n_features is the number of features.

Which is also made clear right at the top of check_array, the validation step where the error is raised:

Input validation on an array, list, sparse matrix or similar.
By default, the input is checked to be a non-empty 2D array containing
only finite values. If the dtype of the array is object, attempt
converting to float, raising on failure.

LinearRegression does actually accept 2D target arrays though, in which case it will perform a multiple linear regression.

Upvotes: 2

Related Questions