In numpy
, some of the operations return in shape (R, 1)
but some return (R,)
. This will make matrix multiplication more tedious since
There are a lot of good answers here already. But for me it was hard to find some example, where the shape or array can break all the program.
So here is the one:
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
from sklearn.linear_model import LinearRegression
regr = LinearRegression()
regr.fit(a,b)
This will fail with error:
ValueError: Expected 2D array, got 1D array instead
but if we add reshape
to a
:
a = np.array([1,2,3,4]).reshape(-1,1)
this works correctly!