python线性回归提示MissingDataError: exog contains inf or nans

本文介绍了一种解决statsmodels.api在处理包含nan和inf的数据时出现的问题的方法。通过将这些特殊值设置为0,可以确保statsmodels.api正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现该报错是statsmodels.api不允许有nan和inf值

此时,需要清除inf和nan

具体操作如下:

import statsmodels.api as sm
rets[np.isnan(rets)] = 0
rets[np.isinf(rets)] = 0

#sm.add_constant(rets['EUROSTOXX'])

rets”即程序中的数据,这里定义的是“rets”。

Traceback (most recent call last): File "C:\Users\A\Desktop\数模校赛(B题)\demo3.py", line 145, in <module> stationary_after_diff = check_stationarity(df, f'{indicator}_diff') File "C:\Users\A\Desktop\数模校赛(B题)\demo3.py", line 49, in check_stationarity result = adfuller(data_series[column_name]) File "D:\Python\Python313\Lib\site-packages\statsmodels\tsa\stattools.py", line 326, in adfuller icbest, bestlag = _autolag( ~~~~~~~~^ OLS, xdshort, fullRHS, startlag, maxlag, autolag ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\Python\Python313\Lib\site-packages\statsmodels\tsa\stattools.py", line 132, in _autolag mod_instance = mod(endog, exog[:, :lag], *modargs) File "D:\Python\Python313\Lib\site-packages\statsmodels\regression\linear_model.py", line 921, in __init__ super().__init__(endog, exog, missing=missing, ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hasconst=hasconst, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\regression\linear_model.py", line 746, in __init__ super().__init__(endog, exog, missing=missing, ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ weights=weights, hasconst=hasconst, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\regression\linear_model.py", line 200, in __init__ super().__init__(endog, exog, **kwargs) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\base\model.py", line 270, in __init__ super().__init__(endog, exog, **kwargs) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\base\model.py", line 95, in __init__ self.data = self._handle_data(endog, exog, missing, hasconst, ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **kwargs) ^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\base\model.py", line 135, in _handle_data data = handle_data(endog, exog, missing, hasconst, **kwargs) File "D:\Python\Python313\Lib\site-packages\statsmodels\base\data.py", line 675, in handle_data return klass(endog, exog=exog, missing=missing, hasconst=hasconst, **kwargs) File "D:\Python\Python313\Lib\site-packages\statsmodels\base\data.py", line 88, in __init__ self._handle_constant(hasconst) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "D:\Python\Python313\Lib\site-packages\statsmodels\base\data.py", line 134, in _handle_constant raise MissingDataError('exog contains inf or nans') statsmodels.tools.sm_exceptions.MissingDataError: exog contains inf or nans
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子建莫敌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值