2 回答

TA贡献1876条经验 获得超6个赞
model.predict是一种预测值的方法,因此您可以为其提供一个看不见的数据集:
import statsmodels.formula.api as smf
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(100,2),columns=['X','Y'])
model = smf.ols('Y ~ X',data=df).fit()
model.predict(exog=pd.DataFrame({'X':[1,2,3]}))
如果您不提供 exog 参数,它会通过调用存储在对象下的数据返回预测,您可以在源代码下看到:
def predict(self, params, exog=None):
"""
Return linear predicted values from a design matrix.
Parameters
----------
params : array_like
Parameters of a linear model.
exog : array_like, optional
Design / exogenous data. Model exog is used if None.
Returns
-------
array_like
An array of fitted values.
Notes
-----
If the model has not yet been fit, params is not optional.
"""
# JP: this does not look correct for GLMAR
# SS: it needs its own predict method
if exog is None:
exog = self.exog
return np.dot(exog, params)
另一方面,model.fittedvalues是一个属性,它是存储的拟合值。由于上面解释的原因,它将与 model.predict() 完全相同。
您也可以查看此类型的方法。

TA贡献1807条经验 获得超9个赞
调用时smf.ols(....).fit()
,您将模型与数据相匹配。即对于数据集中的每个数据点,模型会尝试对其进行解释并为其计算一个值。在这一点上,模型只是试图解释你的历史数据,还没有预测任何东西。另请注意,这fittedvalues
是模型的属性(或属性)。
model.predict()
是模型实际预测未见值的一种方法。
添加回答
举报