statsmodels引发TypeError:优化输入中的输入类型不支持ufunc'isfinite'

时间:2020-04-15 10:09:24

标签: python python-3.x numpy machine-learning data-science

我在运行代码时需要帮助,它显示错误:-“ TypeError:输入类型不支持ufunc'isfinite',并且根据转换规则,不能将输入安全地强制转换为任何受支持的类型'安全”“

我发现很少的解决方案(statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types将数据类型更改为float或int仍然无法正常工作。任何人都可以让我知道下面这段代码做错了什么:

import statsmodels.api as sm

X = np.append(arr = np.ones((50,1)).astype(int),values=X,axis=1)

X.astype('float64')

X_opt = X[:,[0,1,2,3,4,5]]

regressor_ols = sm.OLS(endog=y,exog=X_opt).fit()

OR

import statsmodels.regression.linear_model as lm

X = np.append(arr = np.ones((50,1)).astype(int),values=X,axis=1)

X.astype('float64')

X_opt = X[:,[0,1,2,3,4,5]]

regressor_ols = lm.OLS(endog=y,exog=X_opt).fit()


regressor_ols = lm.OLS(endog=y,exog=X_opt).fit()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\regression\linear_model.py", line 858, in __init__
    super(OLS, self).__init__(endog, exog, missing=missing,
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\regression\linear_model.py", line 701, in __init__
    super(WLS, self).__init__(endog, exog, missing=missing,
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\regression\linear_model.py", line 190, in __init__
    super(RegressionModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\model.py", line 236, in __init__
    super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\model.py", line 76, in __init__
    self.data = self._handle_data(endog, exog, missing, hasconst,
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\model.py", line 100, in _handle_data
    data = handle_data(endog, exog, missing, hasconst, **kwargs)
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\data.py", line 671, in handle_data
    return klass(endog, exog=exog, missing=missing, hasconst=hasconst,
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\data.py", line 87, in __init__
    self._handle_constant(hasconst)
  File "C:\Users\hp\PycharmProjects\DataScientist\venv\lib\site-packages\statsmodels\base\data.py", line 132, in _handle_constant
    if not np.isfinite(exog_max).all():
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

1 个答案:

答案 0 :(得分:0)

此:

X = np.append(arr = np.ones((50,1)).astype(int),values=X,axis=1)

创建dtype int的数组,但是您的分类器需要浮点值。看来您想通过以下方式纠正此问题:

X.astype('float64')

但这没什么用,因为您从不分配给它(正确的名称为X = X.astype('float64'))。

我建议您从数组创建中删除astype(int)

X = np.append(arr=np.ones((50,1)), values=X, axis=1)