将 scikit LinearRegression.predict 用于多功能标签
Using scikit LinearRegression.predict for multifeature labels
我正在尝试预测某个工作日 (0-6) 和某个时间某家餐厅 (=label) 的顾客数量,并将它们用作特征。我的数据框如下所示:
我的模型在第一个可用数据点(第一行)预测客户数量时的草图是:
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
不过,我得到了
ValueError: Expected 2D array, got 1D array instead: array=[1. 6.75]
这背后的原因是什么,将这两个特征输入函数的正确语法是什么predict()
?
据我所知,您的数据点的形状为 (2,),并且必须为 (2,1)。
尝试:
import numpy as np
datapoint=np.array([df.iloc[0]['weekday'], df.iloc[0]['acchour']]).reshape(-1,1)
我正在尝试预测某个工作日 (0-6) 和某个时间某家餐厅 (=label) 的顾客数量,并将它们用作特征。我的数据框如下所示:
我的模型在第一个可用数据点(第一行)预测客户数量时的草图是:
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
不过,我得到了
ValueError: Expected 2D array, got 1D array instead: array=[1. 6.75]
这背后的原因是什么,将这两个特征输入函数的正确语法是什么predict()
?
据我所知,您的数据点的形状为 (2,),并且必须为 (2,1)。 尝试:
import numpy as np
datapoint=np.array([df.iloc[0]['weekday'], df.iloc[0]['acchour']]).reshape(-1,1)