Using scikit LinearRegression.predict for multifeature labels

Viewed 43

I'm trying to make predictions about the number of customers at a restaurant (=label) at a certain weekday (0-6) and a time, using them as features. My dataframe looks like this:

enter image description here

My sketch at making my model predict the number of customers at the first data point available, the first row, is this:

deg = 10 #the chosen polynomial degree
lin_regr = LinearRegression(fit_intercept=False) #linregr object
poly = PolynomialFeatures(degree=deg)    #polynomial model
X_train_poly = poly.fit_transform(X_train) # fit the features
lin_regr.fit(X_train_poly, y_train)
print(lin_regr.predict([df.iloc[0]['weekday'], df.iloc[0]['acchour']])) 
#The last line is causing the error

However, I'm getting

ValueError: Expected 2D array, got 1D array instead: array=[1.   6.75]

What is the reason behind this, and what is the proper syntax to feed the two features into the function predict()?

1 Answers

From what I can see your datapoint has the shape (2,) and it needs to be (2,1). Try:

import numpy as np
datapoint=np.array([df.iloc[0]['weekday'], df.iloc[0]['acchour']]).reshape(-1,1)
Related