Back to questions
JuniorTheoryCommon
What happens when this program runs?
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([1, 2, 3, 4])
y = np.array([3, 5, 7, 9])
model = LinearRegression().fit(x, y)
print(model.coef_)fit raises ValueError: it needs a 2-D X shaped (n_samples, n_features)
- Expecting scikit-learn to reshape a 1-D
xitself:fitrefuses it withValueError: Expected 2D array; passx.reshape(-1, 1), one column with one row per sample. - Expecting a
TypeErrorover integer data: scikit-learn convertsintarrays tofloaton its own; the only problem here is the missing second dimension ofX.
- What shapes do
x.reshape(-1, 1)andx.reshape(1, -1)give here, and which one doesfitneed? - Why may the target
ystay 1-D while the feature matrixXmust be 2-D?