将statsmodels预测结果添加到 pandas 数据框 [英] Adding statsmodels 'predict' results to a Pandas dataframe

查看:404
本文介绍了将statsmodels预测结果添加到 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常要将预测结果附加到用于进行预测的数据集中,但statsmodels 预测函数返回(非索引)结果可能与预测所依据的数据集的长度不同。

It is common to want to append the results of predictions to the dataset used to make the predictions, but the statsmodels predict function returns (non-indexed) results of a potentially different length than the dataset on which predictions are based.

例如,如果测试数据集 test 包含任何空条目,然后

For example, if the test dataset, test, contains any null entries, then

mod_fit = sm.Logit.from_formula('Y ~ A B C', train).fit()
press = mod_fit.predict(test)

将产生一个比 test 的长度,不能用

will produce an array that is shorter than the length of test, and cannot be usefully appended with

test['preds'] = preds

由于的结果预测没有索引,没有办法恢复结果应该附加到的行。

And since the result of predict is not indexed, there is no way to recover the rows to which the results should be attached.

结果与其生成的行相关联的成语是什么?是否有可能获得预测以返回保留其参数的索引的数据帧?

What is the idiom for associating predict results to the rows from which they were generated? Is there, perhaps, a way to get predict to return a dataframe that preserves the indices of its argument?

推荐答案

Predict不应该丢弃任何行。你能发布一个最小的工作示例,发生这种情况吗?保存大熊猫指数是我的雷达,应该很快固定在主人。

Predict shouldn't drop any rows. Can you post a minimal working example where this happens? Preserving the pandas index is on my radar and should be fixed in master soon.

https://github.com/statsmodels/statsmodels/issues/1501

修改 : 没关系。这是一个已知的问题。 https://github.com/statsmodels/statsmodels/issues/1352

Edit: Nevermind. This is a known issue. https://github.com/statsmodels/statsmodels/issues/1352

这篇关于将statsmodels预测结果添加到 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆