![](/img/trans.png)
[英]statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types
[英]statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types in Optimising Input
我在運行我的代碼時需要幫助,它顯示錯誤:-“類型錯誤:輸入類型不支持 ufunc 'isfinite',並且根據強制轉換規則“安全”,輸入無法安全地強制轉換為任何支持的類型"
我找到了幾個解決方案( statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types changed the datatype to float or 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()
或者
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''
這個:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.