statsmodels.discrete.discrete_model.NegativeBinomial.fit()引发LinAlgError:“奇异矩阵”

时间:2018-06-23 15:39:27

标签: python numpy statsmodels

我正在使用statsmodels.discrete.discrete_model.NegativeBinomial进行负二项式回归操作,因此我使用以下脚本创建了一个模型:

from statsmodels.discrete.discrete_model import NegativeBinomial
#create a model
regr = NegativeBinomial(y_train, X_train)

我的y_trainX_train的类型分别为<class 'numpy.ndarray'>(276,)(276, 252)

我的问题是,当我致电regr.fit()时,会引发numpy.linalg.linalg.LinAlgError: Singular matrix错误。这是我的堆栈跟踪:

Traceback (most recent call last):
  File "/home/vajira/PycharmProjects/dengAI/neg_binomial_custom.py", line 137, in <module>
    regr_iq = regr_run(nptrain_iq, degree_iq, exploring=True)
  File "/home/vajira/PycharmProjects/dengAI/neg_binomial_custom.py", line 92, in regr_run
    regr.fit()
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py", line 2756, in fit
    res_poi = mod_poi.fit(**optim_kwds_prelim)
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py", line 1034, in fit
    disp=disp, callback=callback, **kwargs)
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py", line 220, in fit
    disp=disp, callback=callback, **kwargs)
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/base/model.py", line 466, in fit
    full_output=full_output)
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/base/optimizer.py", line 191, in _fit
    hess=hessian)
  File "/home/vajira/ipython/lib/python3.6/site-packages/statsmodels/base/optimizer.py", line 278, in _fit_newton
    newparams = oldparams - np.dot(np.linalg.inv(H),
  File "/home/vajira/ipython/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 528, in inv
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
  File "/home/vajira/ipython/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 89, in _raise_linalgerror_singular
    raise LinAlgError("Singular matrix")
numpy.linalg.linalg.LinAlgError: Singular matrix

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我认为这是一个过度参数化的问题。看来您有276个具有252个特征的样本,这表明对于小样本而言,模型过于复杂。 Singular matrix警告表明该模型未找到与此模型的最佳收敛。

我会回过头来找出您对建模感兴趣的功能少得多的功能。