簡體   English   中英

statsmodels 引發 TypeError:輸入類型不支持 ufunc 'isfinite'

[英]statsmodels raises TypeError: ufunc 'isfinite' not supported for the input types

我正在使用 statsmodels.api 應用反向消除,並且代碼給出了這個錯誤 `TypeError: ufunc 'isfinite' not supported for the input types,並且輸入不能根據強制轉換規則''safe'安全地強制轉換為任何支持的類型'

我不知道如何解決它

這是代碼

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import  train_test_split
from sklearn.preprocessing import  LabelEncoder, OneHotEncoder
from sklearn.compose import  ColumnTransformer
import statsmodels.api as smf

data = pd.read_csv('F:/Py Projects/ML_Dataset/50_Startups.csv')
dataSlice = data.head(10)

#get data column
readX = data.iloc[:,:4].values
readY = data.iloc[:,4].values

#encoding c3
transformer = ColumnTransformer(
    transformers=[("OneHot",OneHotEncoder(),[3])],
    remainder='passthrough' )
readX = transformer.fit_transform(readX.tolist())
readX = readX[:,1:]

trainX, testX, trainY, testY = train_test_split(readX,readY,test_size=0.2,random_state=0)

lreg = LinearRegression()
lreg.fit(trainX, trainY)
predY = lreg.predict(testX)

readX = np.append(arr=np.ones((50,1),dtype=np.int),values=readX,axis=1)

optimisedX = readX[:,[0,1,2,3,4,5]]
ols = smf.OLS(endog=readX, exog=optimisedX).fit()
print(ols.summary())

這是錯誤消息

Traceback (most recent call last):
  File "F:/Py Projects/ml/BackwardElimination.py", line 33, in <module>
    ols = smf.OLS(endog=readX, exog=optimisedX).fit()
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\regression\linear_model.py", line 838, in __init__
    hasconst=hasconst, **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\regression\linear_model.py", line 684, in __init__
    weights=weights, hasconst=hasconst, **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\regression\linear_model.py", line 196, in __init__
    super(RegressionModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\model.py", line 216, in __init__
    super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\model.py", line 68, in __init__
    **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\model.py", line 91, in _handle_data
    data = handle_data(endog, exog, missing, hasconst, **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\data.py", line 635, in handle_data
    **kwargs)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\data.py", line 80, in __init__
    self._handle_constant(hasconst)
  File "C:\Users\udit\AppData\Local\Programs\Python\Python37\lib\site-packages\statsmodels\base\data.py", line 125, in _handle_constant
    if not np.isfinite(ptp_).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''

您需要使用 numpy 將 readX 的數據類型更改為 int 或 float64。 astype( ) function 在優化 X 被初始化之前。 也將 endog 更改為 readY

readX.astype('float64')
optimisedX = readX[:,[0,1,2,3,4,5]]
ols = smf.OLS(endog=readY, exog=optimisedX).fit()
print(ols.summary())

只需添加這一行,

X_opt = X[:, [0, 1, 2, 3, 4, 5]] 
X_opt = np.array(X_opt, dtype=float) # <-- this line 

將其轉換為數組並更改數據類型。

今天我收到了同樣的錯誤。
根本原因是將 numpy dtype object轉換為float64並為其分配新變量並在 function 中使用此變量。

在此處查看示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM