我正在尝试将一组特征拟合到statsmodel的OLS线性回归模型中。
我一次添加几个功能。有了前两个特性,它工作得很好。但当我不断添加新功能时,它会给我一个错误。
Traceback (most recent call last):
File "read_xml.py", line 337, in <module>
model = sm.OLS(Y, X).fit()
...
File "D:\pythonprojects\testproj\test_env\lib\site-packages\statsmodels\base\data.py", line 132, 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''因此,我使用以下命令更改了输入类型
X = X.astype(float)然后弹出一个不同的错误。
Traceback (most recent call last):
File "read_xml.py", line 339, in <module>
print(model.summary())
...
File "D:\pythonprojects\testproj\test_env\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 1824, in sf
place(output, (1-cond0)+np.isnan(x), self.badvalue)
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''我的代码如下所示。
new_df0 = pd.concat([df_lex[0], summary_df[0]], axis = 0, join = 'inner')
new_df1 = pd.concat([df_lex[1], summary_df[1]], axis = 0, join = 'inner')
data = pd.concat([new_df0, new_df1], axis = 1)
print(data.shape)
X = data.values[0:6,:]
Y = data.values[6,:]
Y = Y.reshape(1,88)
X = X.T
Y = Y.T
X = X.astype(float)
model = sm.OLS(Y, X).fit()
predictions = model.predict(X)
print(model.summary())在model = sm.OLS(Y,X).fit()中触发第一个错误在model.summary()中触发第二个错误
但是有了其他一些功能,就没有错误了。
new_df0 = pd.concat([df_len[0], summary_df[0]], axis = 0, join = 'inner')
new_df1 = pd.concat([df_len[1], summary_df[1]], axis = 0, join = 'inner')
data = pd.concat([new_df0, new_df1], axis = 1)
print(data.shape)
X = data.values[0:2,:]
Y = data.values[2,:]
Y = Y.reshape(1,88)
X = X.T
Y = Y.T
X = X.astype(float)
print(X.shape)
print(Y.shape)
model = sm.OLS(Y, X).fit()
predictions = model.predict(X)
print(model.summary())看起来,当我只有两个功能时,它就可以工作了。但当添加不同的6个功能时,它会给出错误。我主要关心的是理解错误。因为我读过与python中的绘图相关的类似问题。但这是在内置函数中触发的,而不是在我的代码中。任何对调试的建议都是非常感谢的。
发布于 2018-11-19 02:37:42
Y.astype(float)成功了。
发布于 2020-04-08 07:07:14
检查X_opt和y的类型。可能是float64,因为计算的精确度。所以,试一试:
X_opt = X_opt.astype(np.float64)
y = y.astype(np.float64)我也犯了同样的错误,并以这种方式修复了它。
发布于 2018-11-15 21:17:35
请使用
model=sm.OLS(df.Y,df.X, missing='drop').fit()它看起来像是在某个变量中有一个NaN值。缺省情况下,missing为none,这可能是原因。
https://stackoverflow.com/questions/53307308
复制相似问题