gpt4 book ai didi

python - 值错误 : Must pass 2-d input when trying to return statsmodels MNLogit confidence intervals

转载 作者:行者123 更新时间:2023-11-28 18:22:03 25 4
gpt4 key购买 nike

以下代码应运行 MNLogit 模型并返回置信区间。它成功返回摘要,您可以在那里看到置信区间,但是当尝试通过 conf_int() 返回置信区间时,我得到一个 ValueError:必须通过二维输入。

import pandas as pd
import statsmodels.api as sm

tmp = pd.read_csv('http://surveyanalysis.org/images/8/82/TrickedLogitMaxDiffExample.csv')
model = sm.MNLogit.from_formula('Choice ~ 1+B+C+D+E+F', tmp, missing='drop')
res = model.fit(method='ncg')
print(res.summary())
res.conf_int()

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-255-a332500964e4> in <module>()
6 res = model.fit(method='ncg')
7 print(res.summary())
----> 8 res.conf_int()

~/anaconda/lib/python3.6/site-packages/statsmodels/base/wrapper.py in wrapper(self, *args, **kwargs)
93 obj = data.wrap_output(func(results, *args, **kwargs), how[0], how[1:])
94 elif how:
---> 95 obj = data.wrap_output(func(results, *args, **kwargs), how)
96 return obj
97

~/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in wrap_output(self, obj, how, names)
405 def wrap_output(self, obj, how='columns', names=None):
406 if how == 'columns':
--> 407 return self.attach_columns(obj)
408 elif how == 'rows':
409 return self.attach_rows(obj)

~/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in attach_columns(self, result)
522 return Series(result, index=self.param_names)
523 else: # for e.g., confidence intervals
--> 524 return DataFrame(result, index=self.param_names)
525
526 def attach_columns_eq(self, result):

~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
304 else:
305 mgr = self._init_ndarray(data, index, columns, dtype=dtype,
--> 306 copy=copy)
307 elif isinstance(data, (list, types.GeneratorType)):
308 if isinstance(data, types.GeneratorType):

~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _init_ndarray(self, values, index, columns, dtype, copy)
461 # by definition an array here
462 # the dtypes will be coerced to a single dtype
--> 463 values = _prep_ndarray(values, copy=copy)
464
465 if dtype is not None:

~/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _prep_ndarray(values, copy)
5686 return arrays, arr_columns
5687
-> 5688
5689 def _list_to_arrays(data, columns, coerce_float=False, dtype=None):
5690 if len(data) > 0 and isinstance(data[0], tuple):

ValueError: Must pass 2-d input

最佳答案

这是一个已修复的错误,显然将在里程碑 0.10 中修复:https://github.com/statsmodels/statsmodels/issues/3651#issuecomment-300511723

但是,有一个变通办法。您可以从结果对象中获取置信区间的数组版本:

res = model.fit(method='ncg')
res._results.conf_int()

这并不完全出色,因为您看不到哪个 CI 属于哪个。但是,如果将其与 res.summary() 报告进行比较,您会发现 CI 的顺序相同,这很有帮助。

感谢 josef-pkt:https://github.com/statsmodels/statsmodels/issues/3883

关于python - 值错误 : Must pass 2-d input when trying to return statsmodels MNLogit confidence intervals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com