利用statsmodels线性回归预测特定向量x的结果

2024-04-25 09:18:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地用OLS建立了一个模型来处理大量的数据。你知道吗

results = smf.ols(formula='ind ~ Age + C(County) + C(Class)',  data=df).fit()

我想实现一个方法,允许用户输入一个向量X,并让它基于回归返回一个y。我研究了statsmodels的“predict”和“forecast”特性,但它似乎不是我想要的。你知道吗

例如,我想做的是:

 ## Although the following is wrong, It shows what I'm trying to do:
 def forecast_y(X):
      return results.forecast(X) 


 ## example:
 print forecast_y([1, 3, 4]) 
 # the model should return
 4.53

Tags: the数据模型agereturnresultsclassind
1条回答
网友
1楼 · 发布于 2024-04-25 09:18:56

如果我说得对,你就不需要样本内预测y,这就是为什么你不想使用predict方法;相反,你只想插入任意的x值,并根据预测的系数得到y的值?你知道吗

如果是这样的话,继续你的例子:

params = results.params   #vector of your coefficients
arbitrary_x = np.array([.5, .5, .5...]) #whatever x values you want to test, with the constant first

assert(len(params) == len(arbitrary_x))

arbitrary_y = (params * arbitrary_x).sum()

我将把理解这一点的含义留给读者,但一定要小心使用。你知道吗

相关问题 更多 >