【问题标题】:Quantile regression prediction分位数回归预测
【发布时间】:2020-12-24 21:39:57
【问题描述】:

我使用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

输出:

【问题讨论】:

  • test 是一个 DataFrame,它的结构是否与train 相同?显示数据时,最好复制粘贴而不是发布屏幕截图。
  • 是的,test 是一个 DataFrame,它具有类似的训练结构。下次我会复制粘贴。谢谢。

标签: python machine-learning statsmodels quantile-regression


【解决方案1】:

错误'DataFrame' object has no attribute 'dtype'是对的,但是很难理解。所以,它真正的意思是训练集和测试集之间的 dtypes 必须存在冲突。 在问题中,训练集中的 Weeks 与测试集中的 dtype 不匹配。

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

【讨论】:

  • 好点你回答了你的问题。因此,如果您希望其他人可以帮助您,则需要minimal reproducible example,因为如果没有重现您的问题的能力,就很难判断出了什么问题。祝你有美好的一天。干杯。
猜你喜欢
  • 1970-01-01
  • 2021-09-28
  • 2020-10-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2018-02-20
  • 2017-06-27
相关资源
最近更新 更多