微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

分位数回归预测

如何解决分位数回归预测

我已经使用statsmodels.formula.api.quantreg对测试集进行了预测。运行此方法时,出现意外错误

AttributeError                            Traceback (most recent call last)
<ipython-input-34-12e0d345b0fc> in <module>
----> 1 test['ypredL'] = model1.predict( test ).values
      2 test['FVC']    = model2.predict( test ).values
      3 test['ypredH'] = model3.predict( test ).values
      4 test['Confidence'] = np.abs(test['ypredH'] - test['ypredL']) / 2

~\anaconda3\envs\knk\lib\site-packages\statsmodels\base\model.py in predict(self,exog,transform,*args,**kwargs)
   1081                        '\n\nThe original error message returned by patsy is:\n'
   1082                        '{0}'.format(str(str(exc))))
-> 1083                 raise exc.__class__(msg)
   1084             if orig_exog_len > len(exog) and not is_dict:
   1085                 import warnings

AttributeError: predict requires that you use a DataFrame when predicting from a model
that was created using the formula api.

The original error message returned by patsy is:
'DataFrame' object has no attribute 'dtype'

有趣的是,在训练集上运行了相同的预测,并且效果很好!这是培训部分的代码

model1 = quantreg('FVC ~ Weeks+Percent+Age+Sex+SmokingStatus',train).fit(q = 0.25)
model2 = quantreg('FVC ~ Weeks+Percent+Age+Sex+SmokingStatus',train).fit(q = 0.5)
model3 = quantreg('FVC ~ Weeks+Percent+Age+Sex+SmokingStatus',train).fit(q = 0.75)

train['y_predL'] = model1.predict(train).values
train['y_pred'] = model2.predict(train).values
train['y_predH'] = model3.predict(train).values

输出

enter image description here

解决方法

错误'DataFrame'对象没有属性'dtype'是正确的,但是很难理解。因此,真正的含义是,训练和测试集之间的dtype必须存在冲突。 在问题中,训练集中的 Weeks 与测试集中的dtype不匹配。

Train-Weeks的

dtype是 int ,而Test-Weeks的dtype是 str

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。