import numpy as np
from sklearn.linear_model import LinearRegression
# sample dummy data
# independent variables
time = np.arange(1,36)
price = np.random.randint(1,100,35)
ads = np.random.randint(1,10,35)
# dependent variable
y = np.random.randn(35)
# Reshape it into 35X3 where each row is an observation
train_X = np.vstack([time, price, ads]).T
# Fit the model
model = LinearRegression().fit(train_X, y)
# Sample observations for which
# forecast of dependent variable has to be made
time1 = np.arange(37, 47)
price1 = np.array([85]*len(time1))
ads1 = np.array([4]*len(time1))
# Reshape such that each row is an observation
test_X = np.vstack([time1, price1, ads1]).T
# make the predictions
print (model.predict(test_X))'
首先使用过去观测的训练数据训练模型。在您的情况下,列车数据由3个自变量和1个因变量组成
一旦一个像样的模型得到训练(使用超参数优化),你就可以用它来做预测
示例代码(内联文档化)
输出:
假设您的模型是在没有任何嵌套阵列的二维阵列上训练的,则问题如下:
Time1
本身就是一个数组,因此,您创建了一个嵌套数组:[Time1,Price1,Ads1]
您当前对predict的调用如下所示:
这看起来像:
您可以将其转换为所需格式,如下所示:
这看起来像:
你需要有一个二维数组
相关问题 更多 >
编程相关推荐