简体   繁体   English

如何修复statsmodels中的.predict()函数?

[英]How to fix .predict() function in statsmodels?

I'm trying to predict temperature at 12 UTC tomorrow in 1 location. 我正在尝试预测明天1点在UTC的温度。 To forecast, I use a basic linear regression model with the statmodels module. 为了进行预测,我将基本线性回归模型与statmodels模块一起使用。 My code is hereafter: 我的代码如下:

x = ds_main
X = sm.add_constant(x)
y = ds_target_t
model = sm.OLS(y,X,missing='drop')
results = model.fit()

The summary shows that the fit is "good": 摘要显示拟合度为“良好”:

在此处输入图片说明

But the problem appears when I try to predict values with a new dataset that I consider to be my testset. 但是,当我尝试使用我认为是测试集的新数据集预测值时,就会出现问题。 The latter has the same columns number and the same variables names, but the .predict() function returns an array of NaN, although my testset has values ... 后者具有相同的列号和相同的变量名称,但是.predict()函数返回NaN数组,尽管我的测试集具有值...

xnew = ts_main
Xnew = sm.add_constant(xnew)
ynewpred = results.predict(Xnew)

I really don't understand where the problem is ... 我真的不明白问题出在哪里...

UPDATE : I think I have an explanation: my Xnew dataframe contains NaN values. 更新 :我想我有一个解释:我的Xnew数据框包含NaN值。 Statmodels function .fit() allows to drop missing values (NaN) but not .predict() function. Statmodels函数.fit()允许删除缺失值(NaN),但不允许删除.predict()函数。 Thus, it returns a NaN values array ... 因此,它返回一个NaN值数组...

But this is the "why", but I still don't get the "how" reason to fix it... 但这是“为什么”,但是我仍然不知道“如何”解决它的原因...

statsmodels.api.OLS be default will not accept the data with NA values. 默认情况下statsmodels.api.OLS将不接受具有NA值的数据。 So if you use this, then you need to drop your NA values first. 因此,如果使用此参数,则需要先删除NA值。

However, if you use statsmodels.formula.api.ols, then it will automatically drop the NA values to run regression and make predictions for you. 但是,如果您使用statsmodels.formula.api.ols,则它将自动删除NA值以运行回归并为您做出预测。

so you can try this: 因此您可以尝试以下操作:

import statsmodels.formula.api as smf
lm = smf.ols(formula = "y~X", pd.concat([y, X], axis = 1)).fit()
lm.predict(Xnew)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确设置statsmodels.predict函数的开始/结束参数 - How to properly set start/end params of statsmodels.predict function 传递给StatsModels预测函数的第一个值是什么? - What is first value that is passed into StatsModels predict function? Statsmodels“预测”功能 - 为什么我不能预测样本外? :( - Statsmodels "predict" function - why cant I predict out of sample? :( 如何修复Python中statsmodel的Holt和Holt-Winters函数中的“TypeError” - How to fix “TypeError” in Holt and Holt-Winters function of statsmodels in Python 如何使用 statsmodels 的 ARMA 来预测外生变量? - How to use statsmodels' ARMA to predict with exogenous variables? 如何正确设置statsmodels.tsa.ar_model.AR.predict函数的开始/结束参数 - How to properly set start/end params of statsmodels.tsa.ar_model.AR.predict function python statsmodels:用于预测arima模型功能的“params”参数 - python statsmodels: “params” parameter for predict function of arima models 如何使用 statsmodels Holt-Winters 预测时间序列集 - How to predict a time series set with statsmodels Holt-Winters 如何使用 statsmodels.formula.api (python) 预测新值 - How to predict new values using statsmodels.formula.api (python) python statsmodels ARIMA plot_predict:如何预测数据? - python statsmodels ARIMA plot_predict: How to get the data predicted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM