Statsmodels给出错误:exog包含inf或nans,但我的数据中没有任何inf的nan

问题描述 投票:0回答:1

我在使用 Statsmodels 进行回归分析时遇到问题。

我有 4 个不同的因变量,我想逐个测试它们。对于其中 3 个,一切正常,但当我使用最后一个时,出现以下错误: 但我尝试了一切方法来找出数据中是否有任何 nan`s 或 inf,并且分析显示没有。还有其他潜在的错误原因吗?

为了检查 nan 和 inf,我使用了:

x.isna().sum()

给出 0

if (x.isin([np.inf, -np.inf, np.nan]).any()):
    print("Series contains infinite values")
else:
    print("Series does not contain infinite values")

哪些版画系列不包含无限值

----> 5     result = sm.OLS(y,sm.add_constant(x_adj)).fit()
      6     return result

~\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py in __init__(self, endog, exog, missing, hasconst, **kwargs)
    870     def __init__(self, endog, exog=None, missing='none', hasconst=None,
    871                  **kwargs):
--> 872         super(OLS, self).__init__(endog, exog, missing=missing,
    873                                   hasconst=hasconst, **kwargs)
    874         if "weights" in self._init_keys:

~\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py in __init__(self, endog, exog, weights, missing, hasconst, **kwargs)
    701         else:
    702             weights = weights.squeeze()
--> 703         super(WLS, self).__init__(endog, exog, missing=missing,
    704                                   weights=weights, hasconst=hasconst, **kwargs)
    705         nobs = self.exog.shape[0]

~\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py in __init__(self, endog, exog, **kwargs)
    188     """
    189     def __init__(self, endog, exog, **kwargs):
--> 190         super(RegressionModel, self).__init__(endog, exog, **kwargs)
    191         self._data_attr.extend(['pinv_wexog', 'weights'])
    192 

~\anaconda3\lib\site-packages\statsmodels\base\model.py in __init__(self, endog, exog, **kwargs)
    235 
    236     def __init__(self, endog, exog=None, **kwargs):
--> 237         super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
    238         self.initialize()
    239 

~\anaconda3\lib\site-packages\statsmodels\base\model.py in __init__(self, endog, exog, **kwargs)
     75         missing = kwargs.pop('missing', 'none')
     76         hasconst = kwargs.pop('hasconst', None)
---> 77         self.data = self._handle_data(endog, exog, missing, hasconst,
     78                                       **kwargs)
     79         self.k_constant = self.data.k_constant

~\anaconda3\lib\site-packages\statsmodels\base\model.py in _handle_data(self, endog, exog, missing, hasconst, **kwargs)
     99 
    100     def _handle_data(self, endog, exog, missing, hasconst, **kwargs):
--> 101         data = handle_data(endog, exog, missing, hasconst, **kwargs)
    102         # kwargs arrays could have changed, easier to just attach here
    103         for key in kwargs:

~\anaconda3\lib\site-packages\statsmodels\base\data.py in handle_data(endog, exog, missing, hasconst, **kwargs)
    670 
    671     klass = handle_data_class_factory(endog, exog)
--> 672     return klass(endog, exog=exog, missing=missing, hasconst=hasconst,
    673                  **kwargs)

~\anaconda3\lib\site-packages\statsmodels\base\data.py in __init__(self, endog, exog, missing, hasconst, **kwargs)
     85         self.const_idx = None
     86         self.k_constant = 0
---> 87         self._handle_constant(hasconst)
     88         self._check_integrity()
     89         self._cache = {}

~\anaconda3\lib\site-packages\statsmodels\base\data.py in _handle_constant(self, hasconst)
    131             exog_max = np.max(self.exog, axis=0)
    132             if not np.isfinite(exog_max).all():
--> 133                 raise MissingDataError('exog contains inf or nans')
    134             exog_min = np.min(self.exog, axis=0)
    135             const_idx = np.where(exog_max == exog_min)[0].squeeze()

MissingDataError: exog contains inf or nans
python regression linear-regression statsmodels
1个回答
0
投票

我遇到了同样的问题。正如 Josef 在相关讨论中所建议的,如果您确定数据集中没有

NaN
Inf
值,则问题很可能与您输入的数据类型 (
dtype
) 有关。

具体来说,检查您的任何变量(列)是否使用无符号整数类型,例如

UInt32
。 statsmodels 在使用无符号整数时有时会遇到问题,因为它们可能与其内部数字处理不一致。

因此,解决方法是将数据中的所有列转换为兼容的数字类型(例如,

float64
int64
)。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.