You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用pmdarima的ARIMA模型预测时报NaN输入错误的问题排查

使用pmdarima的ARIMA预测时触发ValueError: Input contains NaN,但数据无空值

问题场景

使用pmdarima库的ARIMA模型预测序列下一个值时,触发ValueError: Input contains NaN,但确认输入数据中不存在空值。

复现代码

from pmdarima.arima import ARIMA
import pandas as pd

tmp_series = pd.Series([0.8867208063423082, 0.4969678051201152, -0.35079875681211814, 0.07156197743204402, 0.6888394890593726, 0.6136916470350972, 0.9020102952782968, 0.38539523911177426, -0.02211092685162178, 0.7051282791422511, -0.21841121961990842, 0.003262841037836234, 0.3970253153400027, 0.8187445259415379, -0.525847439014037, 0.3039480910711944, 0.0279240073596233, 0.8238419467739897, 0.8234157376839023, 0.5897892005398399, 0.8333118174945449])
model_211 = ARIMA(order=(2, 1, 1), out_of_sample_size=0, mle_regression=True, suppress_warnings=True)
model_211.fit(tmp_series[:-1])
print(model_211.predict())

错误堆栈

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [7], in <cell line: 7>()
      5 display(model_211.params())
      6 display(model_211.aic())
----> 7 display(model_211.predict())

File /usr/local/lib/python3.8/dist-packages/pmdarima/arima/arima.py:793, in ARIMA.predict(self, n_periods, X, return_conf_int, alpha, **kwargs)
    790 arima = self.arima_res_
    791 end = arima.nobs + n_periods - 1
--> 793 f, conf_int = _seasonal_prediction_with_confidence(
    794     arima_res=arima,
    795     start=arima.nobs,
    796     end=end,
    797     X=X,
    798     alpha=alpha)
    800 if return_conf_int:
    801     # The confidence intervals may be a Pandas frame if it comes from
    802     # SARIMAX & we want Numpy. We will to duck type it so we don't add
    803     # new explicit requirements for the package
    804     return f, check_array(conf_int, force_all_finite=False)

File /usr/local/lib/python3.8/dist-packages/pmdarima/arima/arima.py:205, in _seasonal_prediction_with_confidence(arima_res, start, end, X, alpha, **kwargs)
    202     conf_int[:, 1] = f + q * np.sqrt(var)
    204 y_pred = check_endog(f, dtype=None, copy=False, preserve_series=True)
--> 205 conf_int = check_array(conf_int, copy=False, dtype=None)
    207 return y_pred, conf_int

File /usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py:899, in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)
    893         raise ValueError(
    894             "Found array with dim %d. %s expected <= 2."
    895             % (array.ndim, estimator_name)
    896         )
    898     if force_all_finite:
--> 899         _assert_all_finite(
    900             array,
    901             input_name=input_name,
    902             estimator_name=estimator_name,
    903             allow_nan=force_all_finite == "allow-nan",
    904         )
    906 if ensure_min_samples > 0:
    907     n_samples = _num_samples(array)

File /usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py:146, in _assert_all_finite(X, allow_nan, msg_dtype, estimator_name, input_name)
    124         if (
    125             not allow_nan
    126             and estimator_name
   (...)
    130             # Improve the error message on how to handle missing values in
    131             # scikit-learn.
    132             msg_err += (
    133                 f"\n{estimator_name} does not accept missing values"
    134                 " encoded as NaN natively. For supervised learning, you might want"
   (...)
    144                 "#estimators-that-handle-nan-values"
    145             )
--> 146         raise ValueError(msg_err)
    148 # for object dtype data, we only check for NaNs (GH-13254)
    149 elif X.dtype == np.dtype("object") and not allow_nan:

ValueError: Input contains NaN.

环境信息

  • 运行环境:Docker容器(Ubuntu 20.04.4 LTS)
  • Python版本:3.8.10
  • 依赖包版本:
    numpy                        1.22.4
    pandas                       1.4.3 
    pmdarima                     2.0.1   
    scikit-learn                 1.1.1                           
    scipy                        1.8.1
    statsmodels                  0.13.2 
    

问题

  1. 是否有参数可以设置来避免这个错误?
  2. 发现类似未解决问题,不确定是否为同一原因,若是,有哪些ARIMA模型替代库可以推荐?

回答

1. 规避错误的参数设置

从错误堆栈看,NaN出现在置信区间计算阶段,而非原始数据。可通过以下方式规避:

  • 关闭置信区间返回:调用predict时设置return_conf_int=False(默认值为True),跳过置信区间计算:
    print(model_211.predict(return_conf_int=False))
    
  • 调整模型估计方式:关闭mle_regression,改用CSS-ML混合估计方法,避免MLE计算中出现数值不稳定:
    model_211 = ARIMA(order=(2,1,1), mle_regression=False, suppress_warnings=True)
    
  • 升级pmdarima版本:pmdarima 2.0.1存在已知的置信区间计算bug,升级到2.0.3及以上稳定版可能修复该问题。

2. 替代库推荐

若确认是pmdarima的未解决bug,可选择以下替代库:

  • statsmodels:Python时间序列分析的核心库,直接实现ARIMA/SARIMA模型,稳定性强,是pmdarima的底层依赖。示例代码:
    from statsmodels.tsa.arima.model import ARIMA
    model = ARIMA(tmp_series[:-1], order=(2,1,1))
    res = model.fit()
    print(res.predict(start=len(tmp_series[:-1]), end=len(tmp_series[:-1])))
    
  • Prophet:Facebook开源的时间序列预测库,自动处理趋势、季节性,无需手动调参,适合快速业务建模。
  • StatsForecast:Nixtla开源的高效时间序列库,支持ARIMA、ETS等多种模型,性能优于传统库,适配大规模数据场景。

内容的提问来源于stack exchange,提问作者theabc50111

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:45:35