Home > Mobile >  Expected 2D array, got 1D array instead when trying to make predictions
Expected 2D array, got 1D array instead when trying to make predictions

Time:09-27

m = np.array([[1,5,3,6,10,20]])
c = np.array([[100,500,300,600,1000,2000]])

model = LinearRegression()
model.fit(m,c)

I get the error when trying to make the prediction.

model.predict([5])

If I add double brackets, it forces me to pass 6 inputs, but the idea is that it receives only one or as many as I want.

CodePudding user response:

You trained the wrong model. You train 6 models, each with 6 features (and your dataset is only 1-point).

# should be this
m = np.array([[1,5,3,6,10,20]]).T
c = np.array([100,500,300,600,1000,2000])

model = LinearRegression()
model.fit(m,c)

modelo.predict([[5]])
# array([500])
  • Related