本文介绍了statsmodels引发TypeError:输入类型不支持ufunc'isfinite'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用statsmodels.api应用向后消除,并且代码给出此错误`TypeError:输入类型不支持ufunc'isfinite',并且根据转换规则,无法将输入安全地强制转换为任何受支持的类型'安全"
I am applying backward elimination using statsmodels.api and the code gives this error `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''
我不知道如何解决
这是代码
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.optimisedX初始化之前的astype()函数.也将endog更改为readY
U need to change the datatype of the readX to int or float64 using numpy. astype( ) function before optimisedX is initialize. Also change endog to readY
readX.astype('float64')
optimisedX = readX[:,[0,1,2,3,4,5]]
ols = smf.OLS(endog=readY, exog=optimisedX).fit()
print(ols.summary())
这篇关于statsmodels引发TypeError:输入类型不支持ufunc'isfinite'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!